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

Recommended Answers

All 5 Replies

The problem is that you are sending ARRAY_SIZE to you sum and average functions. The issue is that the array may not be filled up all the way to ARRAY_SIZE. You need to pass numElements to those functions instead.

okay, so now I have to get it so that the the average is subtracted from the array and a new array is printed it. I have this code so far:

//File name: array.cpp
//Created by: Ricardo Renta
//Created on: 11/16/11

#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 list1[], 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, numElements);
  cout << "The sum of your inputs is : " << sum <<  endl;

  average = compute_average(list1, numElements);
  cout << "The average of your inputs is : " << average <<  endl;

  cout << "New Array : ";
  subtract_average(list1, numElements);
   
  

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

void subtract_average( int list1[], const int numElements)
{
  double average(0.0);
  
  average=compute_average(list1, numElements);

  for(int j=0; j<numElements; j++)
    {
      list1[j]=list1[numElements]-average;
      cout << " " << list1[j];
    }
 
}

The problem is that the new array is just the first value repeated (for example, when the input is 1,2, and 3, the new array is -2 -2 -2, which is obviously not right). Is there something I'm missing?

Nevermind! Got it! :)

Hey, Please tag the thread to be solved once it is solved....

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.