Okay, So I have this program that spits out the sum and average of a list of numbers that the user inputs. When ever I run it, though, it gives me crazy numbers. For example, when I put the values 1, 2 and 3, it gives me 3.957e+10 as the sum. Here is the code:
#include <iostream>
#include <cmath>
using namespace std;
// function prototype
double compute_average(const int list[], const int numElements);
double compute_sum(const int list[], const int numElements);
void subtract_average(int list[], const int numElements);
int main()
{
int ARRAY_SIZE(100);
int list1[ARRAY_SIZE];
double sum(0.0);
double average(0.0);
int x(0);
int numElements(0);
cout << "Enter a list of up to 100 non-zero numbers. You must enter a zero at the end of your input for the program to work" << endl;
numElements=0;
cin >> x;
do {
list1[numElements]=x;
numElements= numElements+1;
cin >> x;
} while (numElements < ARRAY_SIZE && x!=0);
sum = compute_sum(list1, ARRAY_SIZE);
cout << "The sum of your inputs is : " << sum << endl;
average = compute_average(list1, ARRAY_SIZE);
cout << "The average of your inputs is : " << average << endl;
return 0;
}
double compute_sum (const int list1[], const int numElements)
{
double sum(0.0);
for (int i=0; i< numElements; i++)
{ sum = sum + list1[i]; };
return (sum);
}
double compute_average( const int list1[], const int numElements)
{
double sum(0.0);
double average (0.0);
sum = compute_sum(list1, numElements);
average = double(sum)/numElements;
return (average);
}