C++ Read 10 integers into an array. Calculate the average. Display those array e
ID: 3940891 • Letter: C
Question
C++
Read 10 integers into an array. Calculate the average. Display those array elements that are greater than the average. For example:
Code so far is:
#include <iostream>
using namespace std;
int main()
{
int arr[10],sum=0;
double average;
cout<<"Reading: " << endl;
for(int i=0; i<10; i++)
cin>>arr[i];
for(int i=0; i<10; i++)
sum+=arr[i];
average=(double)sum/10;
cout << "The average is "<<average << endl;
cout << "The following are above average: "<< endl;
for(int i=0; i<10; i++)
{
if(average<arr[i])
cout<<arr[i]<<endl;
}
return 0;
}
Everything seems to be correct, I need it to output the numbers that are entered as the inputs
It should look exactly like this:
Reading: 1,2,3,4,5,6,7,8,9,10
The average is 5.5
The following are above average:
6
7
8
9
10
Explanation / Answer
#include <iostream>
using namespace std;
int main()
{
int arr[10],sum=0;
double average;
cout<<"Reading: " << endl;
for(int i=0; i<10; i++)
cin>>arr[i];
for(int i=0; i<10; i++)
sum+=arr[i];
average=(double)sum/10;
cout<<"Reading: "; //print reading
for(int i=0;i<9;i++) //loop to print values entered by the user
cout<<arr[i]<<","; //using ","
cout<<arr[9]<<endl; //print the last element outside of loop as we dont need "," after that
cout << "The average is "<<average << endl;
cout << "The following are above average: "<< endl;
for(int i=0; i<10; i++)
{
if(average<arr[i])
cout<<arr[i]<<endl;
}
return 0;
}
********OUTPUT*******
Reading:
1 2 3 4 5 6 7 8 9 10
Reading: 1,2,3,4,5,6,7,8,9,10
The average is 5.5
The following are above average:
6
7
8
9
10
********OUTPUT*******
Note:Code has been tested on g++ compiler,please ask in case of any doubt,Thanks.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.