DOC PREVIEW
Saddleback CS 1C - Topic 4 – Arrays Continued Multi-Dimensional Arrays

This preview shows page 1-2-3 out of 8 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 8 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 8 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 8 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 8 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

CS1C – Advanced Programming in C++Topic 4 – Arrays ContinuedMulti-Dimensional ArraysChapter 8 in the shrinkwrapChapter 9 in MalikCS1C – Saddleback CollegeMulti-Dimensional Arrays So far, we’ve discussed one-dimensional arrays It is possible to have arrays of more than 1 dimension  multi-dimensional arrays–There is one subscript for each dimension–There is one subscript for each dimension• A 2-dimensional array has 2 subscripts• A 3-dimensional array has 3 subscripts … and so on– Think of them as an array of arrays– For example, an array of c-strings • c-strings are an array2 Topic 4 - ch 8 - Arrays - Multi-DimensionalCS1C – Saddleback CollegeExample - Chessboard Creating a chessboard program – You want to track the pieces We could do a one dimensional arrayint board[64]; A two-dimensional array would make more sense for this applicationapplicationint board[8][8]; A 2-dimensional array for this application better corresponds to the real-world application3 Topic 4 - ch 8 - Arrays - Multi-DimensionalCS1C – Saddleback College This way we could think of the array in terms of (x, y) pairs.x could represent the rows andy could represent the columnsint board[8][8]; 012123456704 Topic 4 - ch 8 - Arrays - Multi-Dimensional# Of Rows # Of Columns01234567CS1C – Saddleback CollegeChessboard Exampleint b [8][8]; Rows ColsThis way we can reference our array with respect to the rows and columns12345670b[0][0]1230b[0][1] b[0][2] b[0][3] b[0][4] b[0][5] b[0][6] b[0][7]b[1][0]b[2][0]b[3][0]b[1][1]this is how we would accessrow 2, col 25 Topic 4 - ch 8 - Arrays - Multi-DimensionalSo we can say b[1][1] instead of b[9]34567b[3][0]b[4][0]b[5][0]b[6][0]b[7][0] b[7][2]b[7][4] b[7][7]CS1C – Saddleback CollegeExample 2 – Scores Let’s say you are tracking a group of scores from different peopleLet’s say we are tracking 2 people with 3 items to scoreWe declare our array like this  int scores[2][3];1stPersonscores[0][0] = 752nd3rd1stscores[0][0] = 75scores[0][1] = 65scores[0][2] = 952ndPersonscores[1][0] = 45scores[1][1] = 85scores[1][2] = 1006 Topic 4 - ch 8 - Arrays - Multi-Dimensional75 65 9545 85 1002ndScore3rdScore1stScoreCS1C – Saddleback CollegeInitializing Multidimensional Arrays We initialize multidimensional arrays a little differentlyint score[2][3] = { 75, 65, 95, 45, 85, 100 }; Int score[2][3] = { { 75, 65, 95 },{ 45, 85, 100 } }; Although these are equivalent the 2ndis easier to read– The compiler ignores the extra brackets, but needs the commas Or we can initialize all values to 0 like this:int score[2][3] = {0}; Again.. We should use constants where we can:const int TOTAL_PLAYERS = 2;const int TOTAL_SCORES = 3;int score[TOTAL_PLAYERS][TOTAL_SCORES] = {0};7 Topic 4 - ch 8 - Arrays - Multi-DimensionalGenerally speaking we should always initialize arraysCS1C – Saddleback CollegeUsing For loops Initializing multi-dimensional array with a for loop requires using nested for loops  The outer loop controls the row; the inner loop controls the column Example:intscore[2][3];intscore[2][3];for (int i = 0; i < 2; i++){cout << “Enter scores for player #” << i + 1 << “ : “;for (intj = 0; j < 3; j++){cout << “Enter score #” << j + 1 << “ : “;cin >> score[i][j];}}8 Topic 4 - ch 8 - Arrays - Multi-DimensionalCS1C – Saddleback CollegeUsing For loops…const int TOTAL_PLAYERS = 2;const int TOTAL_SCORES = 3;int scores[TOTAL_PLAYERS][TOTAL_SCORES] = {0};int sum;float avg;for (int player = 0; player < TOTAL_PLAYERS; player++){What does this do?Do a desk check for the array Containing{50, 70, 90},{60,80,100} {sum=0;for (intscore = 0; score < TOTAL_SCORES; score++){sum = sum + scores[player][score];}avg = sum / TOTAL_SCORES;cout << “Average for player #” << player + 1 << “ = “ << avg << endl;}9 Topic 4 - ch 8 - Arrays - Multi-DimensionalCS1C – Saddleback CollegeInitializing an array of characterschar alpha[NUM_ROWS][NUM_COLS];for (int rowCnt = 0; rowCnt < NUM_ROWS; rowCnt++){for (int colCnt = 0; colCnt < NUM_COLS; colCnt++){alpha[rowCnt][colCnt]=‘ ‘;}}10 Topic 4 - ch 8 - Arrays - Multi-DimensionalCS1C – Saddleback CollegePassing 2-D arrays as Parametersint scores[TOTAL_PLAYERS][TOTAL_SCORES] = {0};float avg;for (inti = 0; i < TOTAL_PLAYERS; i++){cout << “Average for player #” << i + 1 << “ = “;cout << AverageArray(scores, i);} What else is wrong withthis parameter?It should be passed by Const referenceHow shouldTOTAL_SCORES bedeclared?float AverageArray(int arrayValues[][TOTAL_SCORES], int player){int sum;sum=0;for (intscore = 0; score < TOTAL_SCORES; score++)sum = sum + arrayValues[player][score];return (sum / NumVals);}11C++ doesn’t need to know how many rows, just the size of each row.You do not need to specify the 1stdimension. You must specify the 2nddimension.CS1C – Saddleback CollegeExerciseWrite a function for a tic tac toe game. To determine which spot the user wants to play in they must type in the row, column. The function should obtain the input and verify that the row & column # are within range AND that the spot is not taken.Assume that all elements in the array were initialized to a blank space. The array should be 3 x 3 2-dimensional array of type char.const int NUM_ROWS = 3;const intNUM_COLS = 3;1 2 3[1][1] | [1][2] | [1][3]| |const intNUM_COLS = 3;char board[NUM_ROWS][NUM_COLS];The following parameterswill be used for the function.– board[][NUM_COLS]– char token12 Topic 4 - ch 8 - Arrays - Multi-Dimensional| |1 | | | |-------------------------------[2][1] | [2][2] | [2][3]| |2 | | | |-------------------------------[3][1] | [3][2] | [3][3]| |3 | | | |CS1C – Saddleback Collegevoid GetAndCheckInp(char board[][NUM_COLS],char token){// include the appropriate declaration section heredo{13} while (??);}CS1C – Saddleback CollegeGet and Check Input - Modified Now, modify the previous code segment to obtain two players names and prompt the user by nameLet’s associate player1 with token ‘X’ and player2 with Let’s associate player1 with token ‘X’ and player2 with token ‘O’14 Topic 4 - ch 8 - Arrays - Multi-DimensionalCS1C – Saddleback Collegevoid GetAndCheckInp(char board[][NUM_COLS],char token, string player1, string player2){// include


View Full Document
Download Topic 4 – Arrays Continued Multi-Dimensional Arrays
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Topic 4 – Arrays Continued Multi-Dimensional Arrays and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Topic 4 – Arrays Continued Multi-Dimensional Arrays 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?