Write one dimensional array java program that ask to the user to enter the array
size and reads n integers, compute their average, and find out how many
numbers are above the average. Your program first enter the array size and reads
the numbers and computes their average, then compares each number with the
average to determine whether it is above the average.
Sample Input/Output:
How many numbers do you have?6
Enter a value for array [1]:100
Enter a value for array [2]:10
Enter a value for array [3]:1000
Enter a value for array [4]: 500
Enter a value for array [5]: 200
Enter a value for array [6]: 77
List of elements: 100 10 1000 500 200 77
The sum of the 6 elements: 1887.00
The average of the 5 elements: 314.50
Numbers above average: 4
#include<iostream>
#include <iomanip>
using namespace std;
int main()
{
int n;
cout<<"How many numbers do you have? ";
cin>>n;
int* numbers=new int[n];
for(int i=0;i<n;i++){
cout<<"Enter a value for array ["<<(i+1)<<"]: ";
cin>>numbers[i];
}
int sum=0;
int numbersAboveAverage=0;
cout<<"\nList of elements: ";
for(int i=0;i<n;i++){
cout<<numbers[i]<<" ";
sum+=numbers[i];
}
float average=(float)sum/(float)n;
for(int i=0;i<n;i++){
if(numbers[i]>average){
numbersAboveAverage++;
}
}
cout<<fixed<<"\nThe sum of the 6 elements: "<<setprecision(2)<<(float)sum<<"\n";
cout<<"The average of the 5 elements: "<<setprecision(2)<<average<<"\n";
cout<<"Numbers above average: "<<numbersAboveAverage<<"\n\n";
delete numbers;
system("pause");
return 0;
}
Comments
Leave a comment