290 likes | 359 Views
Chapter 12-2 2-Dimensional Array. Arrays. float Scores[9]; . // one dimensional array . index: 0 1 2 3 4 5 6 7 8. element. Two Dimensional Arrays. float allScores [5][9]; . column index. 0 1 2 3 4 5 6 7 8. row index. 0 1 2
E N D
Arrays float Scores[9]; // one dimensional array index: 0 1 2 3 4 5 6 7 8 element
Two Dimensional Arrays float allScores[5][9]; column index 0 1 2 3 4 5 6 7 8 row index 0 1 2 3 4
Accessing Array Elements float allScores[5][9]; allScores[0][0] allScores[0][8] column index 0 1 2 3 4 5 6 7 8 row index 0 1 2 3 4 allScores[2][5] allScores[4][8]
Accessing Array Elements float allScores[5][9]; // Each array element is // the same as a variable cin >> allScores[0][0]; allScores[0][0] += 5.0; cout << allScores[0][0];
For Loop and 2-D Arrays float allScores[5][9]; // Input values to 1st row // Row index of 1st row: 0 // Column index: 0 to 8 (size – 1) for (int col = 0; col < 9; col ++) cin >> allScores[0][col];
For Loop and 2-D Arrays const int MAX_ROWS = 10; const int MAX_COLS = 35; float allScores[MAX_ROWS][MAX_COLS]; // Input values to the last column // Column index of last column: 34 (MAX_COLS – 1) // Row index: from 0 to 9 (MAX_ROWS – 1) for (int row = 0; row < MAX_ROWS; row ++) cin >> allScores[row][MAX_COLS - 1];
For Loop and 2-D Arrays const int MAX_ROWS = 10; const int MAX_COLS = 35; float allScores[MAX_ROWS][MAX_COLS]; // Input values to the entire 2-D array for (int row = 0; row < MAX_ROWS; row ++) for (int col = 0; col < MAX_COLS; col ++) cin >> allScores[row][col];
For Loop and 2-D Arrays const int MAX_ROWS = 10; const int MAX_COLS = 35; float allScores[MAX_ROWS][MAX_COLS]; int numRows, numCols; cout << “Enter No. of rows and No. of cols: ”; cin >> numRows >> numCols; if ( numRows<= MAX_ROWS && numCols<= MAX_COLS ) for (int row = 0; row < numRows; row ++) for (int col = 0; col < numCols; col ++) cin>> allScores[row][col];
Two Dimensional Arrays as Function Parameters //---------------------------------------------- // The function inputs values to all array // elements within the first numRows rows and // the first numCols columns. // Parameters: (Out, In, In) //---------------------------------------------- void GetData(float a[][MAX_COLS], int numRows, int numCols) { for (int row = 0; row < numRows; row ++) for (int col = 0; col < numCols; col ++) cin >> a[row][col]; return; } // Have to specify MAX_COLS! // float a[][MAX_COLS]
Two Dimensional Arrays as Function Parameters //---------------------------------------------------- // The function inputs values for numRows and numCols, // then inputs values to all array elements within // the first numRows rows and the first numCols // columns. // Parameters: (Out, Out, Out) //---------------------------------------------------- void GetAllData(float a[][MAX_COLS], int& numRows,int& numCols) { cin >> numRows >> numCols; for (int row = 0; row < numRows; row ++) for (int col = 0; col < numCols; col ++) cin >> a[row][col]; return; }
Two Dimensional Arrays as Function Parameters //---------------------------------------------------- // The function has 3 parameters: // a[][MAX_COLS]: 2-D array of float // colIndex, numRows: int // The function computes and returns the average // value of the column at index colIndex. // Params: ( ? , ? , ? ) //---------------------------------------------------- float ColumnAvg( ) { }
Two Dimensional Arrays float ColumnAvg( ); colIndex numRows - 1
Two Dimensional Arrays as Function Parameters //---------------------------------------------------- // The function has 3 parameters: // a[][MAX_COLS]: 2-D array of float // colIndex, numRows: int // The function computes and returns the average // value of the column at index colIndex. // Params: ( In , In , In ) //---------------------------------------------------- float ColumnAvg(const float a[][MAX_COLS], int colIndex, int numRows) { float total = 0; for (int row = 0; row < numRows; row ++) total += a[row][colIndex]; return (total / numRows); }
Two Dimensional Arrays as Function Parameters //------------------------------------------------- // The function has 3 parameters: // a[][MAX_COLS]: 2-D array of float // numCols, rowIndex: int // The function finds and returns the column index // of row at index rowIndex that has the max // value among all elements of row rowIndex. // Params: ( ? , ? , ? ) //------------------------------------------------- int IndexOfRowMax( ) { }
Two Dimensional Arrays int IndexOfRowMax(); numCols - 1 rowIndex numRows - 1
Two Dimensional Arrays as Function Parameters //---------------------------------------------------- // The function has 3 parameters: // a[][MAX_COLS]: 2-D array of float // numCols, rowIndex: int // The function finds and returns the column index // of row at index rowIndex that has the max // value among all elements of the row. // Params: ( In , In , In ) //---------------------------------------------------- int IndexOfRowMax(const float a[][MAX_COLS], int rowIndex, int numCols) { int index = 0; for (int col = 1; col < numCols; col ++) if (a[rowIndex][col] > a[rowIndex][index]) index = col; return index; }
Function Prototypes void GetData(float a[][MAX_COLS], int numRows, int numCols); void GetAllData(float a[][MAX_COLS], int& numRows, int& numCols); float ColumnAvg(const float a[][MAX_COLS], int colIndex, int numRows); int IndexOfRowMax(const float a[][MAX_COLS], int rowIndex, int numCols); // Must specify column MAX_SIZE!
Two Dimensional Arrays as Function Parameters const int MAX_ROWS = 10; const int MAX_COLS = 35; int main() { float allScores[MAX_ROWS][MAX_COLS]; int rows, cols; // Input sizes in main() function. cin >> rows >> cols; // Call function GetData(). GetData(allScores, rows, cols); // No [] for allScores as an acutual parameter! ... return; }
Call Functions int main() { float allScores[MAX_COLS][MAX_ROWS]; int rows, cols, row, col, index; float average; GetAllData(allScores, rows, cols); cout << "Enter column index: "; cin >> col; average = ColumnAvg(allScores, col, rows); cout << "The average value of column " << col << " is " << average; cout << "Enter row index: "; cin >> row; index = IndexOfRowMax(allScores, row, cols); cout << "The largest value of row " << row << " is " << allScores[row][index]; // << allScores[row][IndexOfColMax(allScores, row, cols)]; return 0; } // Array actual parameters should use name only!
Function Prototypes and Function Calls // Function prototypes void GetAllData(float a[][MAX_COLS], int& numRows, int& numCols); float ColumnAvg(const float a[][MAX_COLS], int colIndex, int numRows); int IndexOfRowMax(const float a[][MAX_COLS], int rowIndex, int numCols); // Function calls GetAllData(allScores, rows, cols); average = ColumnAvg(allScores, col, rows); index = IndexOfRowMax(allScores, row, cols);
Array and typedef float Scores[9]; // an array variable typedef float arrayType[9]; // a (new) data type // a nick name for array of // 9 float elements
2-D Array and typedef typedef float arrayType[9]; arrayType Scores; // Array of float with 9 elements arrayTypeallScores[10]; // Array of arrays // 2-D array // Same as // float allScores[10][9];
Array of array float allScores[5][9]; base address: 7000 7036 7072 7108 7144 allScores[0] allScores[1] allScores[2] allScores[3] allScores[4] float ColumnAvg(const float a[][MAX_COLS], int colIndex, int numRows) // Must specify MAX_COLS to know the base address of // other rows: allScores[1], allScores[2], etc.
2-D Array as Function Parameter const int MAX_COLS = 9; typedef float arrayType[MAX_COLS]; float RowAverage(const arrayTypenums[], int rowIndex, int numCols); // nums[] is an array // Data Type of array elements: arrayType // an array of MAX_COLS floats float RowAverage(const float nums[][MAX_COLS], int rowIndex, int numCols); // Must specify MAX_COLS! // Must specify data type of array elements! float RowAverage(const float a[], int numCols); // Pass one row of the array as a parameter! // For example: average = RowAverage(allScores[2], 5);
Summary • 2 dimensional array: a table • access one element • access one row • access one column • 2 dimensional array: array of arrays • typedef and 2-D array • 2-D array as function parameters • pass the whole array: must include MAX_COLS • pass one row: treat it as a one dimensional array
Activation Record • There is one activation record for each function call. • An activation record contains: • Local variables of the function called • Return address • Parameters for the function called Locals Return Address Parameters
Activation Record Example Out InOut In ? ? // Parameters; ( , , ) void Final(int& x, int& y, int z) { int temp; temp = z / x; z = 2 * temp - 5; x += z; y = z - x; return; } ? int main() { int num1 = 5, num2 = 9, x = 10; Final(num1, num2, x + num2); return 0; } x : &num1 (num1) y : &num2 (num2) z : 19 temp: Return Address 3 1 num1: 5 num2: 9 X : 10 6 -5 Activation Record for Final function
Error Types • Compiler / syntax errors – will not compile • Runtime errors – terminates with an error while running: • array out of boundary • variable used before assigned a value- • ... • Logic errors – runs and might not produce an error while running, but produces wrong answer • Fails to terminate – endless looping