Gold University of Minnesota M. Skip to main content.University of Minnesota. Home page.
 
 
 

What's inside.

Ta Email

Download Compiler

Final Project

Lab Notes

Office Hours

Schedule

Syllabus

Announcements

Check Grades

 

CSci 1113 Home

 
 

Printer-friendly version

 
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

 
The University of Minnesota is an equal opportunity educator and employer.
CSci 1113: C++ Programming