Presentation is loading. Please wait.

Presentation is loading. Please wait.

Arrays float Scores[9]; 55.559.047.039.050.560.059.040.0? index: 0 1 2 3 4 5 6 7 8 element // one dimensional array 1.

Similar presentations


Presentation on theme: "Arrays float Scores[9]; 55.559.047.039.050.560.059.040.0? index: 0 1 2 3 4 5 6 7 8 element // one dimensional array 1."— Presentation transcript:

1 Arrays float Scores[9]; 55.559.047.039.050.560.059.040.0? index: 0 1 2 3 4 5 6 7 8 element // one dimensional array 1

2 Two Dimensional Arrays float allScores[5][9]; row index 0123401234 column index 0 1 2 3 4 5 6 7 8 2

3 Accessing Array Elements float allScores[5][9]; 3.53.9 3.2 3.0 row index 0123401234 column index 0 1 2 3 4 5 6 7 8 allScores[0][0] allScores[4][8] allScores[0][8] allScores[2][5] 3

4 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]; 4

5 For Loop and 2-D Arrays float allScores[5][9]; // Input values to 1 st row // Row index of 1 st row: 0 // Column index: 0 to 8 (size – 1) for (int col = 0; col < 9; col ++) cin >> allScores[0][col]; // inFile >> allScores[0][col]; 5

6 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]; // inFile >> allScores[row][MAX_COLS - 1]; 6

7 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]; // inFile >> allScores[row][col]; 7

8 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; // numRows <= MAX_ROWS // numCols <= MAX_COLS for (int row = 0; row < numRows; row ++) for (int col = 0; col < numCols; col ++) cin >> allScores[row][col]; 8

9 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] 9

10 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; } 10

11 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( ) { } 11

12 Two Dimensional Arrays numRows - 1 colIndex float columnAvg( ); 12

13 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); } 13

14 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( ) { } 14

15 Two Dimensional Arrays rowIndex numCols - 1 int IndexOfRowMax(); 15

16 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; } 16

17 Function Prototypes void GetData(float a[][MAX_COLS], int numRows, int numCols); void GetAllData(float a[][COL_SIZE], 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! 17

18 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... return; } 18

19 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; } 19

20 Function Prototypes and Function Calls // Function prototypes void GetAllData(float a[][COL_SIZE], 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); 20

21 Activation Record int main() { float allScores[MAX_ROWS][MAX_COLS]; int rows, cols; GetAllData(allScores, rows, cols);... } 21 void GetAllData(float a[][MAX_COLS], int& numRows, int& numCols) { cin >> numRows >> numCols; for (...) cin >> a[i][j]; } Rows: cols: allScores[][]: ?????? 24 ?????? &numRows: &numCols: a[][]: Return Address 5 24 … 5 …

22 Schedule Lab 11 Grace: 5 pm today Prog 6 Due 9:30 pm Today Thursday No Lab We Have Class! In Lab 009 22

23 Programming Grand Rules You must include a comment block before every class declaration. //--------------------------------------------------------------------- // The following class represents a day of the year. //--------------------------------------------------------------------- class Date {... } 23

24 Programming Grand Rules Every function and method, except the main function, must have a comment which describes what it does and whether the parameters are input, output, or input and output. Be sure to document the purpose of any parameter which isn't obvious from its name alone. //--------------------------------------------------------------------- // This function/method uses hours and rate to compute the gross pay // and the net pay of an employee and pass them back to the calling // function/method. // params: (in, in, out, out) //--------------------------------------------------------------------- void ComputePay(int hours, float rate, float& grossPay, float& netPay) {... } 24

25 Programming Grand Rules Every program must start with a comment block of five sections: Name, Course (section and semester), Purpose, Input, and Output. //--------------------------------------------------------------------- // Name: your name // // Course: CS 143, Section X, Spring 2011 // // Purpose: General description of what the program does. This should // be at least several lines long and should discuss the // purpose of the program. This information should be // from a user's perspective (what the program does) as // opposed to the programmer's perspective (how it works). // // Input: The input to the program and the format of the input. // // Output: The output the program produces. //--------------------------------------------------------------------- 25


Download ppt "Arrays float Scores[9]; 55.559.047.039.050.560.059.040.0? index: 0 1 2 3 4 5 6 7 8 element // one dimensional array 1."

Similar presentations


Ads by Google