Write a program that reads an unspecified number (no more than 20) of positive i
ID: 3541408 • Letter: W
Question
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
any questions just ask
#include <iostream>
using namespace std;
//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;
//read input until 0 is read
intNum=0;
cout<<"Enter value "<<intNum+1<<" (0 to exit): ";
cin>>value[intNum];
while(value[intNum]!=0)
{intNum++;
cout<<"Enter value "<<intNum+1<<" (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;
cout << "The array after calculation contains: " << endl;
//call the calculateDiff function to get the differences
calculateDiff (value,intNum);
//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 sum=0,avg;
//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.
for(int i=0;i<intNum;i++)
sum+=value[i];
avg=sum/intNum;
for(int i=0;i<intNum;i++)
value[i]-=avg;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.