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

Slides:



Advertisements
Similar presentations
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Fall 2013.
Advertisements

Topic 9C – Multiple Dimension Arrays. CISC105 – Topic 9C Multiple Dimension Arrays A multiple dimension array is an array that has two or more dimensions.
Case studies over structure types and strings
Arrays H&K Chapter 8 Instructor – Gokcen Cilingir Cpt S 121 (July 13, 2011) Washington State University.
Recursion H&K Chapter 10 Instructor – Gokcen Cilingir Cpt S 121 (July 21, 2011) Washington State University.
Modular Programming (2) H&K Chapter 6 Instructor – Gokcen Cilingir Cpt S 121 (July 8, 2011) Washington State University.
C Language Elements (II) H&K Chapter 2 Instructor – Gokcen Cilingir Cpt S 121 (June 22, 2011) Washington State University.
Strings (II) H&K Chapter 9 Instructor – Gokcen Cilingir Cpt S 121 (July 20, 2011) Washington State University.
Recursion (II) H&K Chapter 10 Instructor – Gokcen Cilingir Cpt S 121 (July 25, 2011) Washington State University.
Case studies over control structures and iterative structures Instructor – Gokcen Cilingir Cpt S 121 (July 6, 2011) Washington State University.
Structs H&K Chapter 11 Instructor – Gokcen Cilingir Cpt S 121 (July 18, 2011) Washington State University.
Iteration in C H&K Chapter 5 Instructor – Gokcen Cilingir Cpt S 121 (July 1, 2011) Washington State University.
Selection structures – logical expressions and if statements H&K Chapter 4 Instructor – Gokcen Cilingir Cpt S 121 (June 28, 2011) Washington State University.
Data Types H&K Chapter 7 Instructor – Gokcen Cilingir Cpt S 121 (July 12, 2011) Washington State University.
Selection structures in C (II) H&K Chapter 4 Instructor – Gokcen Cilingir Cpt S 121 (June 30, 2011) Washington State University.
Arrays (II) H&K Chapter 8 Instructor – Gokcen Cilingir Cpt S 121 (July 14, 2011) Washington State University.
Dynamic Data Structures H&K Chapter 14 Instructor – Gokcen Cilingir Cpt S 121 (July 26, 2011) Washington State University.
Computer Science 1620 Multi-Dimensional Arrays. we used arrays to store a set of data of the same type e.g. store the assignment grades for a particular.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 8 Multidimensional.
Building Java Programs Chapter 7.5
1 CSCE 1030 Computer Science 1 Arrays Chapter 7 in Small Java.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 7 Multidimensional.
Copyright © 2012 Pearson Education, Inc. Chapter 8 Two Dimensional Arrays.
A First Book of ANSI C Fourth Edition
(4-2) Selection Structures in C H&K Chapter 4 Instructor - Andrew S. O’Fallon CptS 121 (September 16, 2015) Washington State University.
1 Chapter 8 Multi-Dimensional Arrays. 2 1-Dimentional and 2-Dimentional Arrays In the previous chapter we used 1-dimensional arrays to model linear collections.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2009 Pearson Education, Inc., Upper.
Programming Fundamentals I (COSC-1336), Lecture 8 (prepared after Chapter 7 of Liang’s 2011 textbook) Stefan Andrei 4/23/2017 COSC-1336, Lecture 8.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 8 Multidimensional Arrays.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 7 Multidimensional.
Two dimensional arrays in Java Computer Science 3 Gerb Objective: Use matrices in Java.
C++ Programming: From Problem Analysis to Program Design, Second Edition1 Objectives In this chapter you will: Learn about the pointer data type and pointer.
CHAPTER: 12. Array is a collection of variables of the same data type that are referenced by a common name. An Array of 10 Elements of type double.
© Copyright 2013 by Pearson Education, Inc. All Rights Reserved. 1 Chapter 8 Multidimensional Arrays.
Computer Science: A Structured Programming Approach Using C1 8-7 Two-Dimensional Arrays The arrays we have discussed so far are known as one- dimensional.
Class Opener:. Identifying Matrices Student Check:
(5-1) Selection Structures III in C H&K Chapter 4 Instructor - Andrew S. O’Fallon CptS 121 (September 21, 2015) Washington State University.
Two-Dimensional Arrays That’s 2-D Arrays Girls & Boys! One-Dimensional Arrays on Steroids!
(4-3) Selection Structures II in C H&K Chapter 4 Instructor - Andrew S. O’Fallon CptS 121 (September 18, 2015) Washington State University.
Section 5 - Arrays. Problem solving often requires information be viewed as a “list” List may be one-dimensional or multidimensional List is implemented.
(9-1) Strings I H&K Chapter 8 Instructor - Andrew S. O’Fallon CptS 121 (October 19, 2015) Washington State University.
(6-3) Modular Programming H&K Chapter 6 Instructor - Andrew S. O’Fallon CptS 121 (October 2, 2015) Washington State University.
(13-1) Exception Handling in C++ D & D Chapter 17 Instructor - Andrew S. O’Fallon CptS 122 Washington State University.
(3-1) Functions II H&K Chapter 3 Instructor - Andrew S. O’Fallon CptS 121 (September 9, 2015) Washington State University.
(3-2) File Processing with Functions Instructor - Andrew S. O’Fallon CptS 121 (September 11, 2015) Washington State University.
Operator Overloading D & D Chapter 10 Instructor - Andrew S. O’Fallon CptS 122 Washington State University.
(7-2) Arrays I H&K Chapter 7 Instructor - Andrew S. O’Fallon CptS 121 (October 9, 2015) Washington State University.
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
UniMAP Sem2-10/11 DKT121: Fundamental of Computer Programming1 Arrays.
Computer Science: A Structured Programming Approach Using C1 8-7 Two-Dimensional Arrays The arrays we have discussed so far are known as one- dimensional.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
1 Arrays of Arrays Quick review … arrays Arrays of arrays ≡ multidimensional array Example: times table Representation in memory Ragged arrays Example:
Arrays.
Module 1: Array ITEI222 - Advance Programming Language.
Introduction to Classes in C++ Instructor - Andrew S. O’Fallon CptS 122 Washington State University.
Streams and File Processing in C++ Instructor - Andrew S. O’Fallon CptS 122 Washington State University.
MULTI-DIMENSIONAL ARRAYS 1. Multi-dimensional Arrays The types of arrays discussed so far are all linear arrays. That is, they all dealt with a single.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 6 Multidimensional.
Operator Overloading D & D Chapter 10 Instructor - Andrew S. O’Fallon CptS 122 Washington State University.
(6-2) Iteration in C II H&K Chapter 5 Instructor - Andrew S. O’Fallon CptS 121 (February 19, 2016) Washington State University.
Data Types H&K Chapter 7 Instructor - Andrew S. O’Fallon CptS 121 (March 4, 2016) Washington State University.
Classes: A Deeper Look D & D Chapter 9 Instructor - Andrew S. O’Fallon CptS 122 Washington State University.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 7 Multidimensional Arrays.
KUKUM-06/07 EKT120: Computer Programming 1 Week 6 Arrays-Part 1.
(1-3) Basics of a Linked List I Instructor - Andrew S. O’Fallon CptS 122 (June 9, 2016) Washington State University.
(2-1) Data Structures & The Basics of a Linked List I
(2-1) Data Structures & The Basics of a Linked List I
Multidimensional Arrays
(4 – 2) Introduction to Classes in C++
Presentation transcript:

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

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

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

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 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'

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.

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

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.

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:

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.