

Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!


Multi-dimensional Arrays 2-D arrays Allow you to store a table of data Example: float examGrades[3]; //1 dimensional array - lets you store 3 grades for one student //To store several grades for many students using one identifier – use a 2D array const int MAX_PEOPLE = 10; const int NUM_GRADES = 3; float examGrades[MAX_PEOPLE][NUM_GRADES]; //list row first, col second //How to use it – requires 2 loops to read data into individual array cells for (i =0; i < MAX_PEOPLE; i++) { for (j = 0; j < NUM_GRADES; j++) { cin >> examGrade[i][j]; } } When passed as a parameter, you don’t specify the first array size, but you need to specify the second (or others if there are more.) why? C++ stores in row order in memory: first row…. second row…..third row ….. To reference an element you need to know where each row begins Ex: //prototype void readData(int [][NUM_GRADES], int); //function heading void readData(int grades[][NUM_GRADES], int number) { … }
Example Program: #include
}//end CalculateAvgDive