0% found this document useful (0 votes)
40 views47 pages

Chapter 12 Two Dimensional Arrays

Chapter 12 of 'Introduction to Programming in C++' covers two-dimensional arrays, including their declaration, initialization, data entry, display, summation, searching, and passing to functions. It provides practical examples, such as programs for the Chapton Company and Jenko Booksellers, demonstrating how to manage and manipulate data within two-dimensional arrays. The chapter emphasizes the structure and syntax necessary for effectively using two-dimensional arrays in C++ programming.

Uploaded by

aianacana
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
40 views47 pages

Chapter 12 Two Dimensional Arrays

Chapter 12 of 'Introduction to Programming in C++' covers two-dimensional arrays, including their declaration, initialization, data entry, display, summation, searching, and passing to functions. It provides practical examples, such as programs for the Chapton Company and Jenko Booksellers, demonstrating how to manage and manipulate data within two-dimensional arrays. The chapter emphasizes the structure and syntax necessary for effectively using two-dimensional arrays in C++ programming.

Uploaded by

aianacana
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 47

Introduction to Programming in C++

Eighth Edition

Chapter 12: Two-Dimensional Arrays

© 2016 Cengage Learning®. May not be scanned, copied or


duplicated, or posted to a publicly accessible website, in
whole or in part.
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

© 2016 Cengage Learning®. May not be scanned, copied or


An Introduction to Programming with C++, Eighth Edition duplicated, or posted to a publicly accessible website, in 2
whole or in part.
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

© 2016 Cengage Learning®. May not be scanned, copied or


An Introduction to Programming with C++, Eighth Edition duplicated, or posted to a publicly accessible website, in 3
whole or in part.
Using Two-Dimensional Arrays
(cont’d.)
• Each variable in a two-dimensional array is identified by
a unique combination of two subscripts specifying the
variable’s 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.

© 2016 Cengage Learning®. May not be scanned, copied or


An Introduction to Programming with C++, Eighth Edition duplicated, or posted to a publicly accessible website, in 4
whole or in part.
Using Two-Dimensional Arrays
(cont’d.)
• You refer to each variable in a two-dimensional array by
the array’s name immediately followed by the variable’s
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

© 2016 Cengage Learning®. May not be scanned, copied or


An Introduction to Programming with C++, Eighth Edition duplicated, or posted to a publicly accessible website, in 5
whole or in part.
Using Two-Dimensional Arrays
(cont’d.)

Figure 12-1 Examples of items that use the two-dimensional array concept
© 2016 Cengage Learning®. May not be scanned, copied or
An Introduction to Programming with C++, Eighth Edition duplicated, or posted to a publicly accessible website, in 6
whole or in part.
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

© 2016 Cengage Learning®. May not be scanned, copied or


An Introduction to Programming with C++, Eighth Edition duplicated, or posted to a publicly accessible website, in 7
whole or in part.
Declaring and Initializing a Two-
Dimensional Array (cont’d.)
• 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

© 2016 Cengage Learning®. May not be scanned, copied or


An Introduction to Programming with C++, Eighth Edition duplicated, or posted to a publicly accessible website, in 8
whole or in part.
Declaring and Initializing a Two-
Dimensional Array (cont’d.)

Figure 12-2 How to declare and initialize a two-dimensional array


© 2016 Cengage Learning®. May not be scanned, copied or
An Introduction to Programming with C++, Eighth Edition duplicated, or posted to a publicly accessible website, in 9
whole or in part.
Declaring and Initializing a Two-
Dimensional Array (cont’d.)

Figure 12-2 How to declare and initialize a two-dimensional array

© 2016 Cengage Learning®. May not be scanned, copied or


An Introduction to Programming with C++, Eighth Edition duplicated, or posted to a publicly accessible website, in 10
whole or in part.
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

© 2016 Cengage Learning®. May not be scanned, copied or


An Introduction to Programming with C++, Eighth Edition duplicated, or posted to a publicly accessible website, in 11
whole or in part.
Entering Data into a Two-
Dimensional Array (cont’d.)

Figure 12-3 How to use an assignment statement


to assign data to a two-dimensional array
© 2016 Cengage Learning®. May not be scanned, copied or
An Introduction to Programming with C++, Eighth Edition duplicated, or posted to a publicly accessible website, in 12
whole or in part.
Entering Data into a Two-
Dimensional Array (cont’d.)
• Syntax for extraction operator:
cin >> arrayName [rowSubscript][columnSubscript ];

© 2016 Cengage Learning®. May not be scanned, copied or


An Introduction to Programming with C++, Eighth Edition duplicated, or posted to a publicly accessible website, in 13
whole or in part.
Entering Data into a Two-
Dimensional Array (cont’d.)

Figure 12-4 How to use the extraction operator to


store data in a two-dimensional array
© 2016 Cengage Learning®. May not be scanned, copied or
An Introduction to Programming with C++, Eighth Edition duplicated, or posted to a publicly accessible website, in 14
whole or in part.
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

© 2016 Cengage Learning®. May not be scanned, copied or


An Introduction to Programming with C++, Eighth Edition duplicated, or posted to a publicly accessible website, in 15
whole or in part.
Displaying the Contents of a Two-
Dimensional Array (cont’d.)

Figure 12-5 How to display the contents of a two-dimensional array

© 2016 Cengage Learning®. May not be scanned, copied or


An Introduction to Programming with C++, Eighth Edition duplicated, or posted to a publicly accessible website, in 16
whole or in part.
Displaying the Contents of a Two-
Dimensional Array (cont’d.)

Figure 12-5 How to display the contents of a two-dimensional array

© 2016 Cengage Learning®. May not be scanned, copied or


An Introduction to Programming with C++, Eighth Edition duplicated, or posted to a publicly accessible website, in 17
whole or in part.
The Chapton Company Program

• Program that uses a two-dimensional array to store the


number of orders received from each of the Chapton
Company’s 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

© 2016 Cengage Learning®. May not be scanned, copied or


An Introduction to Programming with C++, Eighth Edition duplicated, or posted to a publicly accessible website, in 18
whole or in part.
The Chapton Company Program
(cont’d.)

Figure 12-6 Chapton Company program specification and beginning of program

© 2016 Cengage Learning®. May not be scanned, copied or


An Introduction to Programming with C++, Eighth Edition duplicated, or posted to a publicly accessible website, in 19
whole or in part.
The Chapton Company Program
(cont’d.)

Figure 12-6 Remainder of Chapton Company Program and sample run

© 2016 Cengage Learning®. May not be scanned, copied or


An Introduction to Programming with C++, Eighth Edition duplicated, or posted to a publicly accessible website, in 20
whole or in part.
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
company’s 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.

© 2016 Cengage Learning®. May not be scanned, copied or


An Introduction to Programming with C++, Eighth Edition duplicated, or posted to a publicly accessible website, in 21
whole or in part.
Accumulating the Values Stored in
a Two-Dimensional Array (cont’d.)

Figure 12-7 Problem specification for the Jenko Booksellers Program

© 2016 Cengage Learning®. May not be scanned, copied or


An Introduction to Programming with C++, Eighth Edition duplicated, or posted to a publicly accessible website, in 22
whole or in part.
Accumulating the Values Stored in
a Two-Dimensional Array (cont’d.)

Figure 12-7 IPO chart information and C++ instructions for the
Jenko Booksellers Program
© 2016 Cengage Learning®. May not be scanned, copied or
An Introduction to Programming with C++, Eighth Edition duplicated, or posted to a publicly accessible website, in 23
whole or in part.
Accumulating the Values Stored in
a Two-Dimensional Array (cont’d.)

Figure 12-8 The Jenko Booksellers Program and sample run


© 2016 Cengage Learning®. May not be scanned, copied or
An Introduction to Programming with C++, Eighth Edition duplicated, or posted to a publicly accessible website, in 24
whole or in part.
Searching a Two-Dimensional
Array
• Program for the Wilson Company uses a four-row, two-
column array to store the company’s 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 array’s first column
• If pay code is found, corresponding pay rate is displayed;
otherwise, “Invalid pay code” is displayed

© 2016 Cengage Learning®. May not be scanned, copied or


An Introduction to Programming with C++, Eighth Edition duplicated, or posted to a publicly accessible website, in 25
whole or in part.
Searching a Two-Dimensional
Array (cont’d.)

Figure 12-9 Problem specification, IPO chart information, and


C++ instructions for the Wilson Company program
© 2016 Cengage Learning®. May not be scanned, copied or
An Introduction to Programming with C++, Eighth Edition duplicated, or posted to a publicly accessible website, in 26
whole or in part.
Searching a Two-Dimensional
Array (cont’d.)

Figure 12-9 Problem specification, IPO chart information, and


C++ instructions for the Wilson Company program
© 2016 Cengage Learning®. May not be scanned, copied or
An Introduction to Programming with C++, Eighth Edition duplicated, or posted to a publicly accessible website, in 27
whole or in part.
Searching a Two-Dimensional
Array (cont’d.)

Figure 12-9 Problem specification, IPO chart information, and


C++ instructions for the Wilson Company program

© 2016 Cengage Learning®. May not be scanned, copied or


An Introduction to Programming with C++, Eighth Edition duplicated, or posted to a publicly accessible website, in 28
whole or in part.
Searching a Two-Dimensional
Array (cont’d.)

Figure 12-9 Problem specification, IPO chart information,


and C++ instructions for the Wilson Company program

© 2016 Cengage Learning®. May not be scanned, copied or


An Introduction to Programming with C++, Eighth Edition duplicated, or posted to a publicly accessible website, in 29
whole or in part.
Searching a Two-Dimensional
Array (cont’d.)

Figure 12-10 Beginning of Wilson Company program


© 2016 Cengage Learning®. May not be scanned, copied or
An Introduction to Programming with C++, Eighth Edition duplicated, or posted to a publicly accessible website, in 30
whole or in part.
Searching a Two-Dimensional
Array (cont’d.)

Figure 12-10 Remainder of Wilson Company program and sample run


© 2016 Cengage Learning®. May not be scanned, copied or
An Introduction to Programming with C++, Eighth Edition duplicated, or posted to a publicly accessible website, in 31
whole or in part.
Searching a Two-Dimensional
Array (cont’d.)

Figure 12-11 Desk-check table after the statements


on lines 11 through 21 are processed
© 2016 Cengage Learning®. May not be scanned, copied or
An Introduction to Programming with C++, Eighth Edition duplicated, or posted to a publicly accessible website, in 32
whole or in part.
Searching a Two-Dimensional
Array (cont’d.)

Figure 12-12 Desk-check table after the


nested loop is processed the first time
© 2016 Cengage Learning®. May not be scanned, copied or
An Introduction to Programming with C++, Eighth Edition duplicated, or posted to a publicly accessible website, in 33
whole or in part.
Searching a Two-Dimensional
Array (cont’d.)

Figure 12-13 Desk-check table after the nested


loop ends and the pay code is not located
© 2016 Cengage Learning®. May not be scanned, copied or
An Introduction to Programming with C++, Eighth Edition duplicated, or posted to a publicly accessible website, in 34
whole or in part.
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)

© 2016 Cengage Learning®. May not be scanned, copied or


An Introduction to Programming with C++, Eighth Edition duplicated, or posted to a publicly accessible website, in 35
whole or in part.
Passing a Two-Dimensional Array
to a Function (cont’d.)

Figure 12-14 Modified Chapton Company Program


© 2016 Cengage Learning®. May not be scanned, copied or
An Introduction to Programming with C++, Eighth Edition duplicated, or posted to a publicly accessible website, in 36
whole or in part.
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
element’s row and column, respectively

© 2016 Cengage Learning®. May not be scanned, copied or


An Introduction to Programming with C++, Eighth Edition duplicated, or posted to a publicly accessible website, in 37
whole or in part.
Summary (cont’d.)

• You refer to each element in a two-dimensional array by


the array’s name immediately followed by the element’s
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

© 2016 Cengage Learning®. May not be scanned, copied or


An Introduction to Programming with C++, Eighth Edition duplicated, or posted to a publicly accessible website, in 38
whole or in part.
Summary (cont’d.)

• 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

© 2016 Cengage Learning®. May not be scanned, copied or


An Introduction to Programming with C++, Eighth Edition duplicated, or posted to a publicly accessible website, in 39
whole or in part.
Summary (cont’d.)

• To pass a two-dimensional array to a function, you


include the array’s name in the statement that calls the
function
• Array’s corresponding formal parameter in the function
header must specify the formal parameter’s 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

© 2016 Cengage Learning®. May not be scanned, copied or


An Introduction to Programming with C++, Eighth Edition duplicated, or posted to a publicly accessible website, in 40
whole or in part.
Lab 12-1: Stop and Analyze

• Study the program in Figure 12-15 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

© 2016 Cengage Learning®. May not be scanned, copied or


An Introduction to Programming with C++, Eighth Edition duplicated, or posted to a publicly accessible website, in 41
whole or in part.
Lab 12-1: Stop and Analyze
(cont’d.)

Figure 12-15 Code for Lab 12-1


© 2016 Cengage Learning®. May not be scanned, copied or
An Introduction to Programming with C++, Eighth Edition duplicated, or posted to a publicly accessible website, in 42
whole or in part.
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)

Figure 12-16 Problem specification for Lab 12-2

© 2016 Cengage Learning®. May not be scanned, copied or


An Introduction to Programming with C++, Eighth Edition duplicated, or posted to a publicly accessible website, in 43
whole or in part.
Lab 12-3: Modify

• Modify the program in Lab12-2.cpp by replacing the


maximum amounts in the shipCharges array with
the minimum amounts. Save the modified program as
Lab12-3.cpp
• Save, run, and test the program.

© 2016 Cengage Learning®. May not be scanned, copied or


An Introduction to Programming with C++, Eighth Edition duplicated, or posted to a publicly accessible website, in 44
whole or in part.
Lab 12-4: What’s Missing?

• The program is this lab should display the average stock


price.
• Follow the instructions for starting C++ and opening the
Lab12-4.cpp file. Put the C++ instructions in the proper
order, and then determine the one or more missing
instructions.
• Test the program appropriately.

© 2016 Cengage Learning®. May not be scanned, copied or


An Introduction to Programming with C++, Eighth Edition duplicated, or posted to a publicly accessible website, in 45
whole or in part.
Lab 12-5: Desk-Check

• Desk-check the Jenko Booksellers program in Figure 12-


8 in the chapter.

© 2016 Cengage Learning®. May not be scanned, copied or


An Introduction to Programming with C++, Eighth Edition duplicated, or posted to a publicly accessible website, in 46
whole or in part.
Lab 12-6: Debug

• Open the program in the Lab12-6.cpp file.


• Debug the program.

© 2016 Cengage Learning®. May not be scanned, copied or


An Introduction to Programming with C++, Eighth Edition duplicated, or posted to a publicly accessible website, in 47
whole or in part.

You might also like