|
|
|
One Dimensional Array Problemm - Determine the Number of Elements in an
Array that are Greater than the Average Value
% cat average.cpp
// Determine the number of elements in an array
// greater than the average
#include<iostream>
using namespace std;
int main()
{
const int NEL = 10;
int i, count = 0;
double avg = 0.0;
//declare and initialize an array
double y[] = {88.7, 77.9, 84.3, 99.2, 56.7,
65.5, 70.2, 80.5, 91.5, 55.8};
// display the array
for(i=0; i < NEL; i++)
{
cout << y[i] << " " ;
}
cout << endl;
// get the average
for(i=0; i < NEL; i++)
{
avg += y[i];
}
avg = avg / NEL;
cout << "Average = " << avg << endl;
// determine the number greater than the average
for(i=0; i < NEL; i++)
{
if( y[i] >= avg )
count++;
}
cout << "There are " << count
<< " elements greater than the
average\n";
return 0;
}
% g++ average.cpp
% a.out
88.7 77.9 84.3 99.2 56.7 65.5 70.2 80.5 91.5 55.8
Average = 77.03
There are 6 elements greater than the average
|
|