I\'m trying to finish this program thanks... Write a program that reads an unspe
ID: 3620070 • Letter: I
Question
I'm trying to finish this program thanks...Write a program that reads an unspecified number (no more than 20) of positive integers,
calculate the integer average of these positive numbers (not including 0),
and replace these integer values with the differences between them and the average value.
For example, if you entered 1, 10, 20, 2, and 0, the average value is
(1 + 10 + 20 + 2)/4 = 8. So the array becomes
{1-8, 10-8, 20-8, 2-8}, i.e., {-7, 2, 12, -6}.
*/
#include <iostream>
using namespace std;
//prototype of a function calculating the difference
prototype of a function calculating the difference
int main( )
{
const int CAPACITY = 20; //the array capacity
int value[CAPACITY]; // the array that holds integer values
int intNum;
//read input until 0 is read
read input until 0 is read
//output the array
cout << "The array after input contains: " << endl;
for( int i=0; i<intNum; i++ )
cout << value[i] << " ";
cout << endl<< endl;
cout << "The array after calculation contains: " << endl;
//call the calculateDiff function to get the differences
call the function to get the differences
//output the array
for( int i=0; i<intNum; i++ )
cout << value[i] << " ";
cout << endl;
return 0;
}
// function calculateDiff: calculate the integer average of values stored in the array
// and replace these integer values with the differences between them and the average value.
void calculateDiff ( )
{
function calculateDiff: calculate the integer average of values stored in the arrayand replace these integer values with the differences between them and the average value.
}
Explanation / Answer
please rate - thanks #include <iostream>using namespace std;
//prototype of a function calculating the difference
//prototype of a function calculating the difference
void calculateDiff (int[],int);
int main( )
{
const int CAPACITY = 20; //the array capacity
int value[CAPACITY]; // the array that holds integer values
int intNum=0,avg;
//read input until 0 is read
cout<<"Enter a number, 0 to exit: ";
cin>>value[intNum];
while(value[intNum]!=0)
{intNum++;
cout<<"Enter a number, 0 to exit: ";
cin>>value[intNum];
}
//output the array
cout << "The array after input contains: " << endl;
for( int i=0; i<intNum; i++ )
cout << value[i] << " ";
cout << endl<< endl;
calculateDiff (value,intNum);
cout << "The array after calculation contains: " << endl;
//call the calculateDiff function to get the differences
//call the function to get the differences
//output the array
for( int i=0; i<intNum; i++ )
cout << value[i] << " ";
cout << endl;
system("pause");
return 0;
}
// function calculateDiff: calculate the integer average of values stored in the array
// and replace these integer values with the differences between them and the average value.
void calculateDiff (int value[],int intnum )
{int i,sum=0,average;
for(i=0;i<intnum;i++)
sum+=value[i];
average=sum/intnum;
for(i=0;i<intnum;i++)
value[i]=value[i]-average;
//function calculateDiff: calculate the integer average of values stored in the arrayand replace these integer values with the differences between them and the average value.
}