An Introduction to Programming with C++ Sixth Edition Chapter 12 Two-Dimensional Arrays

Embed Size (px)

DESCRIPTION

3 Using Two-Dimensional Arrays Recall that an array is a group of related variables that have the same data type You can visualize a one-dimensional array as a column of variables in memory You can visualize a two-dimensional array as a table in that the variables are in rows and columns You can determine the number of variables in a two-dimensional array by multiplying the number of rows by the number of columns

Citation preview

An Introduction to Programming with C++ Sixth Edition Chapter 12 Two-Dimensional Arrays Objectives Declare and initialize a two-dimensional array Enter data into a two-dimensional array Display the contents of a two-dimensional array Sum the values in a two-dimensional array Search a two-dimensional array Pass a two-dimensional array to a function An Introduction to Programming with C++, Sixth Edition2 3 Using Two-Dimensional Arrays Recall that an array is a group of related variables that have the same data type You can visualize a one-dimensional array as a column of variables in memory You can visualize a two-dimensional array as a table in that the variables are in rows and columns You can determine the number of variables in a two-dimensional array by multiplying the number of rows by the number of columns An Introduction to Programming with C++, Sixth Edition4 Using Two-Dimensional Arrays (contd.) Each variable in a two-dimensional array is identified by a unique combination of two subscripts specifying the variables row and column position Variables located in the first, second, etc. row of a two-dimensional array are assigned a row subscript of 0, 1, etc. Similarly, variables located in the first, second, etc. column of a two-dimensional array are assigned a column subscript of 0, 1, etc. Using Two-Dimensional Arrays (contd.) You refer to each variable in a two-dimensional array by the arrays name immediately followed by the variables row and column subscripts (in that order), each enclosed in its own set of square brackets As with one-dimensional arrays, the last row and column subscripts of a two-dimensional array will always be one less than the number of rows and columns (respectively) in the array, since the subscripts begin at 0 An Introduction to Programming with C++, Sixth Edition5 6 Figure 12-1 Names of some of the variables in the two-dimensional orders array An Introduction to Programming with C++, Sixth Edition7 Figure 12-2 Problem specification for the Caldwell Companys orders program Using Two-Dimensional Arrays (contd.) An Introduction to Programming with C++, Sixth Edition8 Figure 12-2 IPO chart for the Caldwell Companys orders program Declaring and Initializing a Two- Dimensional Array You must declare a two-dimensional array before you use it As with one-dimensional arrays, it is good practice to initialize the array variables Syntax for declaring a two-dimensional array: dataType arrayName [numberOfRows] [numberOfColumns] = {{initialValues}, {initialValues }, {initialValues }}; dataType specifies the data type of elements in the array (all elements must have same data type) numberOfRows and numberOfColumns specify number of rows and columns in array, respectively An Introduction to Programming with C++, Sixth Edition9 Declaring and Initializing a Two- Dimensional Array (contd.) You initialize elements in a two-dimensional array by entering a separate initialValues section, enclosed in braces, for each row of the array Maximum number of initialValues sections you may enter is the number of rows in the array Within individual initialValues sections, you enter one or more values separated by commas Maximum number of values you may enter in a row is the number of columns An Introduction to Programming with C++, Sixth Edition10 An Introduction to Programming with C++, Sixth Edition11 Figure 12-3 How to declare and initialize a two-dimensional array An Introduction to Programming with C++, Sixth Edition12 Declaring and Initializing a Two- Dimensional Array (contd.) Figure 12-4 Illustration of the two-dimensional grades array An Introduction to Programming with C++, Sixth Edition13 Entering Data into a Two-Dimensional Array You can use either an assignment statement or the extraction operator to enter data into the elements of a two-dimensional array Syntax for assignment statement: arrayName [rowSubscript] [columnSubscript ] = expression; expression can include any combination of constants, variables, and operators Must match data type of array An Introduction to Programming with C++, Sixth Edition14 Entering Data into a Two-Dimensional Array (contd.) Syntax for extraction operator: cin >> arrayName [rowSubscript][columnSubscript ]; An Introduction to Programming with C++, Sixth Edition15 Figure 12-5 How to use an assignment statement to assign data to a two-dimensional array An Introduction to Programming with C++, Sixth Edition16 Figure 12-5 How to use an assignment statement to assign data to a two-dimensional array (contd.) An Introduction to Programming with C++, Sixth Edition17 Figure 12-6 How to use the extraction operator to store data in a two-dimensional array An Introduction to Programming with C++, Sixth Edition18 Figure 12-6 How to use the extraction operator to store data in a two-dimensional array (contd.) An Introduction to Programming with C++, Sixth Edition19 Displaying the Contents of a Two- Dimensional Array To display the contents of a two-dimensional array, you must access its individual elements You can use two counter-controlled loops One to keep track of the row subscript One to keep track of the column subscript An Introduction to Programming with C++, Sixth Edition20 Figure 12-7 How to display the contents of a two-dimensional array An Introduction to Programming with C++, Sixth Edition21 Figure 12-7 How to display the contents of a two-dimensional array (contd.) An Introduction to Programming with C++, Sixth Edition22 Coding the Caldwell Companys Orders Program Program that uses a two-dimensional array to store the number of orders received from each of the Caldwell Companys four sales regions during the first three months of the year Each row in the array represents a region, and each column represents a month Program allows the user to enter the order amounts and then displays the amounts on the computer screen An Introduction to Programming with C++, Sixth Edition23 Coding the Caldwell Companys Orders Program Figure 12-8 IPO chart information and C++ instructions for the Caldwell Companys orders program An Introduction to Programming with C++, Sixth Edition24 Figure 12-8 IPO chart information and C++ instructions for the Caldwell Companys orders program (contd.) An Introduction to Programming with C++, Sixth Edition25 Figure 12-9 The Caldwell Companys orders program An Introduction to Programming with C++, Sixth Edition26 Figure Sample run of the Caldwell Companys orders program An Introduction to Programming with C++, Sixth Edition27 Accumulating the Values Stored in a Two-Dimensional Array Program for the Jenko Booksellers company uses a two-dimensional array to store the sales made in each of the companys three bookstores Array contains three rows and two columns First column contains sales amount for paperback books sold in each of three stores Second column contains sales amounts for hardcover books in the three stores Program calculates total sales by accumulating amounts stored in array and displays total sales An Introduction to Programming with C++, Sixth Edition28 Figure Problem specification, IPO chart information, and C++ instructions for the Jenko Booksellers program An Introduction to Programming with C++, Sixth Edition29 Figure Problem specification, IPO chart information, and C++ instructions for the Jenko Booksellers program (contd.) Accumulating the Value Stored in a Two-Dimensional Array (contd.) An Introduction to Programming with C++, Sixth Edition30 Figure The Jenko Booksellers program An Introduction to Programming with C++, Sixth Edition31 Figure Result of running the Jenko Booksellers program Accumulating the Value Stored in a Two-Dimensional Array (contd.) An Introduction to Programming with C++, Sixth Edition32 Searching a Two-Dimensional Array Program for the Wilson Company uses a four-row, two-column array to store the companys four pay codes and their corresponding pay rates Pay codes are stored in first column of each row Pay rate associated with each code is stored in the same row as its pay code, in the second column Program gets a pay code from user and searches for pay code in the arrays first column If pay code is found, corresponding pay rate is displayed; otherwise, Invalid pay code is displayed An Introduction to Programming with C++, Sixth Edition33 Figure Problem specification, IPO chart information, and C++ instructions for the Wilson Company program An Introduction to Programming with C++, Sixth Edition34 Figure Problem specification, IPO chart information, and C++ instructions for the Wilson Company program (contd.) An Introduction to Programming with C++, Sixth Edition35 Figure Problem specification, IPO chart information, and C++ instructions for the Wilson Company program (contd.) An Introduction to Programming with C++, Sixth Edition36 Figure Sample run of the Wilson Company program Figure Another sample run of the Wilson Company program An Introduction to Programming with C++, Sixth Edition37 Figure Wilson Company program An Introduction to Programming with C++, Sixth Edition38 Figure Wilson Company program (contd.) An Introduction to Programming with C++, Sixth Edition39 Figure Desk-check table after the statements on lines 11 through 21 are processed Searching a Two-Dimensional Array (contd.) An Introduction to Programming with C++, Sixth Edition40 Figure Desk-check table after the nested loop is processed the first time Searching a Two-Dimensional Array (contd.) An Introduction to Programming with C++, Sixth Edition41 Figure Desk-check table after the nested loop is processed the second time Searching a Two-Dimensional Array (contd.) An Introduction to Programming with C++, Sixth Edition42 Figure Desk-check table after the nested loop ends during the second search Searching a Two-Dimensional Array (contd.) An Introduction to Programming with C++, Sixth Edition43 Passing a Two-Dimensional Array to a Function Like one-dimensional arrays, two-dimensional arrays are passed automatically by reference When passing a two-dimensional array to a function, function header and prototype should contain formal parameter consisting of name of array followed by two sets of square brackets containing the number of rows and columns, respectively Number of rows is not required (first set of brackets may be left empty) An Introduction to Programming with C++, Sixth Edition44 Figure Modified Caldwell Companys orders program An Introduction to Programming with C++, Sixth Edition45 Figure Modified Caldwell Companys orders program (contd.) Summary A two-dimensional array resembles a table in that elements are in rows and columns Each element has the same data type You can determine the number of elements in a two- dimensional array by multiplying the number of rows by the number of columns Each element in a two-dimensional array is identified by a unique combination of two subscripts representing the elements row and column, respectively An Introduction to Programming with C++, Sixth Edition46 Summary (contd.) You refer to each element in a two-dimensional array by the arrays name immediately followed by the elements subscripts in two sets of square brackets First row and column subscripts in a two- dimensional array are 0 Last row and column subscripts are always one number less than the number of rows and columns, respectively You must declare a two-dimensional array before you can use it An Introduction to Programming with C++, Sixth Edition47 Summary (contd.) When declaring a two-dimensional array, you must provide the number of rows as well as the number of columns You can use an assignment statement or the extraction operator to enter data into an array You need to use two loops to access every element in a two-dimensional array One of the loops keeps track of the row subscript, and the other keeps track of the column subscript An Introduction to Programming with C++, Sixth Edition48 Summary (contd.) To pass a two-dimensional array to a function, you include the arrays name in the statement that calls the function Arrays corresponding formal parameter in the function header must specify the formal parameters data type and name, followed by two sets of square brackets First bracket contains the number of rows, and the second bracket contains the number of columns An Introduction to Programming with C++, Sixth Edition49 Lab 12-1: Stop and Analyze Study the program in Figure and then answer the questions The company array contains the amounts the company sold both domestically and internationally during the months January through June The first row contains the domestic sales for the three months The second row contains the international sales during the same period An Introduction to Programming with C++, Sixth Edition50 Lab 12-2: Plan and Create Plan and create a program for Falcon Incorporated Program should display a shipping charge based on number of items ordered (entered by user) An Introduction to Programming with C++, Sixth Edition51 Figure Problem specification for Lab 12-2 Lab 12-3: Modify Modify the program in Lab 12-2 Replace the maximum amounts in the shipCharges array with the minimum amounts Make the appropriate modifications to the program Run and test the program appropriately An Introduction to Programming with C++, Sixth Edition52 Lab 12-4: Desk-Check Desk-check the Jenko Booksellers program in Figure An Introduction to Programming with C++, Sixth Edition53 Lab 12-5: Debug Open the program in the Lab12-5.cpp file Debug the program An Introduction to Programming with C++, Sixth Edition54