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

 
Maximum Value in a Two-Dimensional Array
Using a 2D Array as a Parameter


% cat array_max_2D.cpp
// Using arrays as arguments in a function call
// Function maxi2 returns the maximum element in the 2D array.

#include<iostream>
using namespace std;

const int NROWS=4, NCOLS=3;

int maxi2(int results[][NCOLS] ); // declaration

int main()
{
   int max, score[NROWS][NCOLS] =
     {7,4,2,11,9,8,2,2,0,5,6,7};

   max = maxi2( score ); // call

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

   return 0;

}

int maxi2( int results[][NCOLS] )
{
   int i, j, max = results[0][0];

   for( i = 0; i < NROWS; i++)
     for(j = 0; j < NCOLS; j++)
     {
     if( max < results[i][j] )
       max = results[i][j];
     }

   return max;
}
% g++ array_max_2D.cpp
% a.out
The maximum number is 11

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