Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Understanding Multi-dimensional Arrays: 2-D Arrays and Memory Layout, Study notes of Computer Science

How to use 2-d arrays in c++ to store tables of data for multiple students and their corresponding grades. It covers the memory layout of multi-dimensional arrays, the need to specify the second dimension when passing them as function parameters, and provides an example program to calculate the average dive score for each diver.

Typology: Study notes

Pre 2010

Uploaded on 08/13/2009

koofers-user-u40
koofers-user-u40 🇺🇸

10 documents

1 / 2

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
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) { … }
pf2

Partial preview of the text

Download Understanding Multi-dimensional Arrays: 2-D Arrays and Memory Layout and more Study notes Computer Science in PDF only on Docsity!

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 const int NUM_PEOPLE = 3; //MUST BE GLOBAL const int NUM_DIVES = 3; int main ( ) { void CalculateAvgDive ( float [ ] [NUM_DIVES+1] ); float score[NUM_PEOPLE][NUM_DIVES+1]; for (int row = 0; row < NUM_PEOPLE; row++) { for (int col = 0; col < NUM_DIVES; col++) { cin >> score[row][col]; }//end for col }//end for row CalculateAvgDive ( score ); cout << “DIVER NUMBER DIVE1 DIVE2 DIVE3 AVERAGE” << endl; for (int row = 0; row < NUM_PEOPLE; row++) { for (int col = 0; col < NUM_DIVES+1; col++) { cout << score[row][col] << “ “; } //end for col cout << endl; //print carriage return at end of each line }//end for row return 0; }//end main void CalculateAvgDive ( int dive[ ][NUM_DIVES + 1] ) // purpose determine the average dive score for each diver // that result is stored int eh the last entry of each row { float sum, average;

// Complete the implementation of this function

}//end CalculateAvgDive