DOC PREVIEW
Saddleback CS 1B - Topic 4 - Arrays

This preview shows page 1 out of 3 pages.

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

Unformatted text preview:

Multi Dimensional Arrays Topic 4 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 A 2 dimensional array has 2 subscripts A 3 dimensional array has 3 subscripts and so on Chapter 8 in the shrinkwrap Chapter 9 in Malik Think of them as an array of arrays For example an array of c strings Multi Dimensional Arrays c strings are an array 2 Topic 4 ch 8 Arrays Multi Dimensional Example Chessboard Creating a chessboard program We could do a one dimensional array You want to track the pieces This way we could think of the array in terms of x y pairs x could represent the rows and y could represent the columns int board 64 A two dimensional array would make more sense for this application int board 8 8 int board 8 8 Of Rows Of Columns A 2 dimensional array for this application better corresponds to the real world application 3 Topic 4 ch 8 Arrays Multi Dimensional Chessboard Example int b 8 8 Rows Cols 0 1 2 3 4 5 6 2 b 2 0 3 b 3 0 4 b 4 0 5 b 5 0 6 b 6 0 7 b 7 0 this is how we would access row 2 col 2 Topic 4 ch 8 Arrays Multi Dimensional Let s say you are tracking a group of scores from different people Let s say we are tracking 2 people with 3 items to score We declare our array like this 1st Person scoresAr 0 0 75 scoresAr 0 1 65 scoresAr 0 2 95 b 7 2 b 7 4 4 Topic 4 ch 8 Arrays Multi Dimensional Example 2 Scores 7 This way we can 0 b 0 0 b 0 1 b 0 2 b 0 3 b 0 4 b 0 5 b 0 6 b 0 7 reference our array with respect to the b 1 0 b 1 1 rows and columns 1 So we can say b 1 1 instead of b 9 0 1 2 3 4 5 6 7 0 1 2 3 4 5 6 7 int scoresAr 2 3 1st 2nd 3rd Score Score Score 75 65 95 45 85 100 2nd Person scoresAr 1 0 45 scoresAr 1 1 85 scoresAr 1 2 100 b 7 7 5 Topic 4 ch 8 Arrays Multi Dimensional 6 1 Initializing Multidimensional Arrays We initialize multidimensional arrays a little differently int scoresAr 2 3 int scoresAr 2 3 75 65 95 45 85 100 int scoresAr 2 3 75 65 95 45 85 100 Although these are equivalent the 2nd is easier to read Or we can initialize all values to 0 like this Using For loops for int i 0 i 2 i cout Enter scores for player i 1 for int j 0 j 3 j cout Enter score j 1 cin scoresAr i j The compiler ignores the extra brackets but needs the commas int scoresAr 2 3 0 Again We should use constants where we can const int TOTAL PLAYERS 2 const int TOTAL SCORES 3 int scoresAr TOTAL PLAYERS TOTAL SCORES 0 Generally speaking we should always initialize arrays 7 Topic 4 ch 8 Arrays Multi Dimensional Using For loops const int TOTAL PLAYERS 2 const int TOTAL SCORES 3 8 Topic 4 ch 8 Arrays Multi Dimensional Initializing an array of characters What does this do Do a desk check for the array Containing 50 70 90 60 80 100 char alphaAr NUM ROWS NUM COLS int scoresAr TOTAL PLAYERS TOTAL SCORES 0 int sum player score float avg int rowCnt int colCnt for player 0 player TOTAL PLAYERS player sum 0 for score 0 score TOTAL SCORES score sum sum scoresAr player score avg sum TOTAL SCORES for rowCnt 0 rowCnt NUM ROWS rowCnt for colCnt 0 colCnt NUM COLS colCnt alphaAr rowCnt colCnt cout Average for player player 1 avg endl 9 Topic 4 ch 8 Arrays Multi Dimensional Passing 2 D arrays as Parameters int scoresAr TOTAL PLAYERS TOTAL SCORES 0 float avg int player score How should TOTAL SCORES be declared for player 0 player TOTAL PLAYERS player cout Average for player player 1 cout AverageArray scoresAr i What It should elsebe is wrong passedwith by Const this parameter reference float AverageArray int arrayValues TOTAL SCORES int player You do not need to specify int sum the 1st dimension You do need to specify the 2nd dimension sum 0 for score 0 score TOTAL SCORES score sum sum arrayValues player score return sum NumVals C doesn t need to know how many rows just the size of each row 11 10 Topic 4 ch 8 Arrays Multi Dimensional Exercise Write 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 1 2 3 1 1 1 2 1 3 const int NUM ROWS 3 const int NUM COLS 3 1 char boardAr NUM ROWS NUM COLS The following parameters will be used for the function boardAr NUM COLS char token Topic 4 ch 8 Arrays Multi Dimensional 2 1 2 2 2 3 2 3 1 3 2 3 3 3 12 2 void GetAndCheckInp char board Ar 3 char token include the appropriate declaration section here Get and Check Input Modified do 13 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 token O Topic 4 ch 8 Arrays Multi Dimensional 14 Topic 4 ch 8 Arrays Multi Dimensional void GetAndCheckInp char boardAr NUM COLS char token string player1 string player2 include the appropriate declaration section here do if row NUM ROWS row 1 while valid 15 Topic 4 ch 8 Arrays Multi Dimensional 3


Saddleback CS 1B - Topic 4 - Arrays

Download Topic 4 - 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 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 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?