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);
}

"sum" in your function and sum in main() are in completely different scopes. Functions have their own set of variables and cannot see anything declared in main or another function.

You have a couple of options, either pass both the average and sum variables into your function by pointer or reference (so using a * or a & before the parameter, and having to dereference the * if you do it by pointer), or pass one and return the other.

Can u please show me what this looks like I'm still a little confused

Your function definition would look something like: void solution( int a[], int b,int & sum,double & average) -or- double solution(int a[],int b,int & sum) It's probably better to call your function average, have it return a double, and have sum passed in by reference (so the second case above).

Your sum function is only taking the last value entered by the user as stated below:

sum=a[x];

The code comes after the for loop. At this point, the value of 'X' would have been 10 and its surprising that you are actually able to execute your program without problem. In fact, it would have crashed because you are trying to access invalid array index.
A simple modification to your for loop in main function would have solved the problem.

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

Your sum function is only taking the last value entered by the user as stated below:

sum=a[x];

The code comes after the for loop. At this point, the value of 'X' would have been 10 and its surprising that you are actually able to execute your program without problem. In fact, it would have crashed because you are trying to access invalid array index.
A simple modification to your for loop in main function would have solved the problem.

This is true. I think the OP is trying to relegate the sum portion to his function, so he/she should probably just delete the sum=a[x] line altogether.

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.