okay, another array question. I have to modify the input and outit as modified array. here is the code.
//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);
The program is supposed to find the average of the inputs and subtract it from each element in the array, then print out the modified array. For example, an input of 1, 2 and 3 would yield the modified input of -2 0 and 1, The problem is that the modified array prints only the first value. For example, if I were to put in 1, 2, and 3, it gives me -2 -2 -2. Whats wrong with it?
for every j you are using the assignment operator gives always the same value. list1[j]=list1[numElements]-average;
should become list1[j]=list1[j]-average;
Please, learn how to use the tag "code" before posting.
And learn also how to use a debugger, it is a really easy error to trap with that tool.