average is correct but the sum is wrong. :)

I'm having a problem with my program. I'm able to input all 10 numbers in but when it out puts the sum I get the wrong answer but then when it outputs my average that information is correct. I know it's a simple fix but I can't see it. Can someone please put me in the right direction. I'm beginning to lose my mind.Here is the code

#include <iostream>
using namespace std;

double solution( int a[], int b) //average function
{
int x ;
double sum = 0;

for (x = 0; x < b; x++)
{
sum += a[x]; }

return sum / b; }
int main ()
{
double average=0;
int sum ;
int a [10];
int x ;


for (x = 0; x<10; x++)
{
cout << "Please Enter Your Number :" <<endl;
cin>> a[x];

}

sum=a[x];
cout<<"The sum of your numbers is"<<sum; // shows the sum
average = solution(a,10);
cout << "The Average is "<< average; // shows the average

return (0);
}
You know, average is sum/number of elements. You wrote that. You calculated the sum, then divided it by 10 yet when you had to output the sum, you chose to output a[10] instead and now you ask how to calculate the sum.
WHY?
In case you still need the answer...

Following main(), it looks like you go straight into a for() loop, counting x up to 9, then immediately assign sum to a[9], which would be the last digit you entered.

You could either make another function like solution(), except just for the sum, or try something like this...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <iostream>
using namespace std;

int sum;
int incoming;

int main()
{
	sum = 0;  //  Make sure sum is zeroed out before we start trying to make calculations.

	cout << "Please enter your numbers:" << endl;

	for( int i = 0; i < 10; i++ )  //  Begin input loop.
	{
		cout << i+1 << ": ";  //  This will keep track of which number we're on.
		cin >> incoming;  //  Temporary holder for the new number.

		sum += incoming;  //  Add them up as we go along.
	}

	cout << "The sum of your numbers is " << sum << endl;
	cout << "The average is " << (sum/10) << endl;

	return (0);
}


A few things to note. There's no need for an array if all we're doing is adding them up in the end, since we can do that on the fly.

Also, initializing the counter (most commonly "i" is used) inside the for() loop is good practice. That way, if I needed to make another loop down the road, I could once again use "i" without having to worry about what x is currently holding, having to reset it, etc. If you WANTED x to keep track of the last loop number, I'd still suggest this.

1
2
3
4
5
6
7
8
	int x = 0;

	for( int i = 0; i < 10; i++ )
	{
		//  Do stuff here...

		x++;
	}


This is how I'd write the program though...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#include <iostream>
using namespace std;

const int MAX_TOTAL_INPUT = 200;  //  Setting the maximum amount of numbers to take.

int add_numbers( int inputarray[], int inputtotal )  //  Function to add "inputtotal" integers from an inputarray[].
{
	int tempsum = 0;

	for( int i = 0; i < inputtotal; i++ )
	{
		tempsum += inputarray[i];
	}
	
	return tempsum;
}

int average_numbers( int inputsum, int inputtotal )  //  Function to divide two numbers.
{
	int tempaverage = ( inputsum/inputtotal );

	return tempaverage;
}

int main()
{
	int numberinput[MAX_TOTAL_INPUT];  //  Define our array.
	int numbertotal = -1;  //  Set our total number of inputs to something invalid for the loop.

	cout << "How many numbers (" << MAX_TOTAL_INPUT << " or less)?" << endl;

	while( numbertotal > MAX_TOTAL_INPUT || numbertotal < 1 )  //  Loop until you get a valid number, so you don't divide by zero or get a buffer overflow.
	{
		cin >> numbertotal;

		if( numbertotal > MAX_TOTAL_INPUT )
			cout << "I said 200 or less." << endl;

		if( numbertotal < 1 )
			cout << "Come on.  I need more than that." << endl;
	}

	cout << "Please enter your numbers:" << endl;

	for( int i = 0; i < numbertotal; i++ )  //  Begin input loop.
	{
		cout << i+1 << ": ";  //  This will keep track of which number we're on.
		cin >> numberinput[i];
	}

	int numbersum = add_numbers( numberinput, numbertotal );  //  Calculate the sum of all the numbers first.
	int numberaverage = average_numbers( numbersum, numbertotal );  //  Using the previously summed numbers, divide by the amount of numbers given.

	cout << "The sum of your numbers is " << numbersum << endl;
	cout << "The average is " << numberaverage << endl;

	return (0);
}


EDIT:
Whoops, forgot to mention one thing. It's also good practice to not write the input into your array (or what ever variable you will be using) until you've verified that it's not invalid, E.G. if they've entered a character, decimal, or even something longer than an int. I somewhat applied this to "numbertotal" in my program, but spaced it on the actual inputs. The rule of thumb is that you can NEVER trust the user to do what you ask of them.
Last edited on
Topic archived. No new replies allowed.