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

 
Arrays as Arguments in a Function Call

% cat array_max.cpp
// Using arrays as arguments in a function call
// Determine the maximum element in an array

#include<iostream>
using namespace std;

int maxi( const int results[], int NUM_ITEMS ); // declaration

int main()
{
   const int NUM_ITEMS = 5;

   int max, score[NUM_ITEMS] = {22, 77, 44, 11, 33 };

   max = maxi( score, NUM_ITEMS ); // use the array name in the call

   cout << "The maximum number is " << max << endl;

   return 0;

}

int maxi( const int results[], int NUM_ITEMS )
{
   int i, max = results[0];
 
   for( i = 1; i < NUM_ITEMS; i++)
   {
     if( max < results[i] )
       max = results[i];
   }

   return max;

}

% g++ array_max.cpp
% a.out
The maximum number is 77



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