Thursday, November 3, 2011

Passing Array to Function

#include
#include
using namespace std;

double getAverage(int* score, int n)
{
  int sum = 0;
  int i = 0;
  for (i = 0; i < n; i++)
    sum += score[i];
  double average = double(sum) / n;
  return average;
} // getAverage

int main()
{
  int daysPerMonth[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
  cout << "Average = " << getAverage(daysPerMonth, 12) << endl;
  return 0;
} // main

No comments:

Post a Comment