Ok. First off, here is the question I am answering

Problem 3: Write a program that will read monthly sales into a dynamically allocated array. The program will input the size of the array from the user. It will call a function that will find the yearly sum (the sum of all the sales). It will also call another function that will find the average.

The problem I am having is that the program will run, and go through the first loop, but when it comes to the point of calling the functions, it just skips over them. I have run it in Dev C++ and Visual 2008, and can't get it to work. Here is the code.

#include <iostream>
using namespace std;

float sum(float[], int);
float average(int, float);

int main()
{
    int size;
    int count;
    float *sales;
    float userInfo;
    
    cout << "Please enter the size you want the array to be\n";
    cin >> size;
    
    sales = new float[size];
    
    for (count = 0; count < size; count++)
	{    
		cout << "Please enter the information for array position" << count << endl;
        cin >> userInfo;
        sales[count] = userInfo;
	}    
    
	float sum(int sales[], int size);
	cout << "The sum of the Figures you entered is " << sum << endl;
    float average (int size, float sum);
	cout << "The average of the figures you entered is " << average << endl;
    return 0;
}

float sum(float sales[], int size)
{
    float sum;
    float holder;
    int count;
    
    for (count = 0; count < size; count++)
        holder += sales[count];
    
    sum = holder; 
	
    return sum;
}

float average(int size, int sum)
{
     int average;
     average = sum / size;
	 return average
}

Recommended Answers

All 2 Replies

The problem I am having is that the program will run, and go through the first loop, but when it comes to the point of calling the functions, it just skips over them.

Look at these lines carefully:

float sum(int sales[], int size);
cout << "The sum of the Figures you entered is " << sum << endl;
float average (int size, float sum);
cout << "The average of the figures you entered is " << average << endl;

Those are declarations. Your code never actually calls the functions. You should also see some warnings generated around here, too (for what it's worth, GCC complains about the cout lines). Pay attention to compiler warnings; they're usually trying to tell you something important. What does your compiler say about this code?

Ok. First off, here is the question I am answering

Problem 3: Write a program that will read monthly sales into a dynamically allocated array. The program will input the size of the array from the user. It will call a function that will find the yearly sum (the sum of all the sales). It will also call another function that will find the average.

The problem I am having is that the program will run, and go through the first loop, but when it comes to the point of calling the functions, it just skips over them. I have run it in Dev C++ and Visual 2008, and can't get it to work. Here is the code.

#include <iostream>
using namespace std;

float sum(float[], int);
float average(int, float);

int main()
{
    int size;
    int count;
    float *sales;
    float userInfo;
    
    cout << "Please enter the size you want the array to be\n";
    cin >> size;
    
    sales = new float[size];
    
    for (count = 0; count < size; count++)
	{    
		cout << "Please enter the information for array position" << count << endl;
        cin >> userInfo;
        sales[count] = userInfo;
	}    
    
	float sum(int sales[], int size);
	cout << "The sum of the Figures you entered is " << sum << endl;
    float average (int size, float sum);
	cout << "The average of the figures you entered is " << average << endl;
    return 0;
}

float sum(float sales[], int size)
{
    float sum;
    float holder;
    int count;
    
    for (count = 0; count < size; count++)
        holder += sales[count];
    
    sum = holder; 
	
    return sum;
}

float average(int size, int sum)
{
     int average;
     average = sum / size;
	 return average
}

Hi first of all change your functions as following

float sum(float sales[], int size)
{
    float holder = 0;// you must do that
    int count;
 
    for (count = 0; count < size; count++)
        holder += sales[count];
 
    return holder; 
}
 
float average(int size, float sum)
{
     
     return  sum / size;
}

And in your main replace

float sum(int sales[], int size);
	cout << "The sum of the Figures you entered is " << sum << endl;
    float average (int size, float sum);
	cout << "The average of the figures you entered is " << average << endl;

wtih

float total = sum(sales, size);
	cout << "The sum of the Figures you entered is " << total << endl;
    
	cout << "The average of the figures you entered is " << average(size, total) << endl;

hope it help

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.