Presentation is loading. Please wait.

Presentation is loading. Please wait.

Arrays (III) H&K Chapter 8 Instructor – Gokcen Cilingir Cpt S 121 (July 15, 2011) Washington State University.

Similar presentations


Presentation on theme: "Arrays (III) H&K Chapter 8 Instructor – Gokcen Cilingir Cpt S 121 (July 15, 2011) Washington State University."— Presentation transcript:

1 Arrays (III) H&K Chapter 8 Instructor – Gokcen Cilingir Cpt S 121 (July 15, 2011) Washington State University

2 C. Hundhausen, A. O’Fallon2 Multidimensional Arrays (1) Thus far, we've focused on single dimensional arrays ◦ We declare them as follows: int my_array[6]; ◦ And we visualize them as follows: ◦ Essentially, they are a single row of values Many applications, however, call for not just a single row, but a two-dimensional matrix of values ◦ Examples: A tic-tac-toe board, A table of financial data, a grid of train connections ◦ We can use two-dimensional arrays to represent such objects 1012089091

3 C. Hundhausen, A. O’Fallon3 Multidimensional Arrays (2) Declaring a multidimensional array ◦ The following code declares a 3  3 array that could be used to represent a tic-tac-toe board: char tic_tac_toe_board[3][3]; ◦ We'll represent the three possible values on a board as characters: 'B' = blank, 'X' = x player, and 'O' = o player. ◦ A sample board: XBO BOX XBB Row Column 0 1 2 012

4 C. Hundhausen, A. O’Fallon4 Multidimensional Arrays (3) Referencing array cells ◦ We use double bracket notation to reference a cell ◦ For example, assuming the previous board : XBO BOX XBB Row Column 0 1 2 012 The following are true: board[0][0] == 'X‘ board[0][1] == 'B‘ board[0][2] == 'O' board[1][0] == 'B‘ board[1][1] == 'O‘ board[1][2] == 'X‘ board[2][0] == 'X‘ board[2][1] == 'B‘ board[2][2] == 'B'

5 Case study 1 Problem statement: Write a program that reads 3x3 matrices from files “input1.txt” and “input2.txt”, calculates and prints the sum and the difference of the matrices.

6 Case study 1 (cont’d) Top-down design: void readMatrix (int matrix[][MATRIX_SIZE], FILE *outfile); void printMatrix (const int matrix[][MATRIX_SIZE]); void subtract (const int op1[][MATRIX_SIZE], const int op2[][MATRIX_SIZE], int result[][MATRIX_SIZE]); void add (const int op1[][MATRIX_SIZE], const int op2[][MATRIX_SIZE], int result[][MATRIX_SIZE]);

7 Case study 2 Problem statement: (H&K 8.5) A barcode scanner verifies a 12-digit code by comparing the code's last digit to a check digit check computed from the first 11 barcode digits as follows: ◦ Calculate the sum of barcode digits in the odd-numbered positions (the first, third,..., eleventh digits) and multiply this sum by 3. ◦ Calculate the sum of the digits in the even-numbered positions (the second, fourth,..., tenth digits), then add this to the previous result. ◦ Let the last digit of the result from step 2 be last, if last is 0, then the check digit check is 0. Otherwise, check = 10- last. ◦ If check equals to the code’s last digit, the code is correct.

8 Case study 2 (cont’d) Write a program that reads arbitrary number of 12 digits barcodes with digits and barcodes separated by whitespace. For each barcode read, ◦ the program stores the digits in an integer array, calculates the check digit, and compare it to the final barcode digit. ◦ If the barcode is correct, displays the message "validated." If not, displays the message " error in barcode." Try your program on the following barcodes: 0 7 9 4 0 0 8 0 4 5 0 1 0 2 4 0 0 0 1 6 2 8 6 0 0 1 1 1 1 0 8 5 6 8 0 7 0 5 1 0 0 0 1 3 8 1 0 1

9 C. Hundhausen, A. O’Fallon9 References J.R. Hanly & E.B. Koffman, Problem Solving and Program Design in C (6 th Ed.), Addison- Wesley, 2010 P.J. Deitel & H.M. Deitel, C How to Program (5 th Ed.), Pearson Education, Inc., 2007.


Download ppt "Arrays (III) H&K Chapter 8 Instructor – Gokcen Cilingir Cpt S 121 (July 15, 2011) Washington State University."

Similar presentations


Ads by Google