Presentation is loading. Please wait.

Presentation is loading. Please wait.

Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved. 0136097200 1 Chapter 8 Multidimensional.

Similar presentations


Presentation on theme: "Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved. 0136097200 1 Chapter 8 Multidimensional."— Presentation transcript:

1 Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved. 0136097200 1 Chapter 8 Multidimensional Arrays

2 Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved. 0136097200 2 Objectives F To give examples of representing data using two- dimensional arrays (§8.1). F To declare two-dimensional arrays and access array elements in a two-dimensional array using row and column indexes (§8.2). F To process two-dimensional arrays (§8.3). F To pass two-dimensional arrays to functions (§8.4). F To write a program for grading multiple-choice questions using two-dimensional arrays (§8.5). F To solve the closest-pair problem using two-dimensional arrays (§8.6). F To solve the Sudoku problem using two-dimensional arrays (§8.7). F To declare multidimensional arrays (§8.8).

3 Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved. 0136097200 3 Two-dimensional Arrays // Declare array ref var elementType arrayName[rowSize][columnSize]; int matrix[5][5];

4 Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved. 0136097200 4 Two-dimensional Array Illustration

5 Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved. 0136097200 5 Declaring, Creating, and Initializing Using Shorthand Notations You can also use an array initializer to declare, create and initialize a two-dimensional array. For example,

6 Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved. 0136097200 6 Initializing Arrays with Random Values The following loop initializes the array with random values between 0 and 99: for (int row = 0; row < rowSize; row++) { for (int column = 0; column < columnSize; column++) { matrix[row][column] = rand() % 100; }

7 Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved. 0136097200 7 Printing Arrays To print a two-dimensional array, you have to print each element in the array using a loop like the following: for (int row = 0; row < rowSize; row++) { for (int column = 0; column < columnSize; column++) { cout << matrix[row][column] << " "; } cout << endl; }

8 Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved. 0136097200 8 Summing All Elements To print a two-dimensional array, you have to print each element in the array using a loop like the following: for (int row = 0; row < rowSize; row++) { for (int column = 0; column < columnSize; column++) { cout << matrix[row][column] << " "; } cout << endl; }

9 Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved. 0136097200 9 Summing Elements by Column For each column, use a variable named total to store its sum. Add each element in the column to total using a loop like this: for (int column = 0; column < columnSize; column++) { int total = 0; for (int row = 0; row < rowSize; row++) total += matrix[row][column]; cout << "Sum for column " << column << " is " << total << endl; }

10 Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved. 0136097200 10 Which row has the largest sum? Use variables maxRow and indexOfMaxRow to track the largest sum and index of the row. For each row, compute its sum and update maxRow and indexOfMaxRow if the new sum is greater.

11 Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved. 0136097200 11 Passing Two-Dimensional Arrays to Functions You can pass a two-dimensional array to a function; however, C++ requires that the column size to be specified in the function declaration. Listing 8.1 gives an example with a function that returns the sum of all the elements in a matrix. PassTwoDimensionalArray Run

12 Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved. 0136097200 12 Example: Grading Multiple- Choice Test F Objective: write a program that grades multiple-choice test. GradeExamRun

13 Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved. 0136097200 13 Problem: Finding Two Points Nearest to Each Other FindNearestPoints Run

14 Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved. 0136097200 14 Case Study: Sudoku The objective is to fill the grid (see Figure 8.3(a)) so that every row, every column, and every 3×3 box contain the numbers 1 to 9, as shown in Figure 8.3(b).

15 Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved. 0136097200 15 Case Study: Sudoku Rule 1: Fill in an empty cell from the first to the last. Rule 2: Fill in a smallest number possible. Rule 3: If no number can fill in a cell, backtrack.

16 Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved. 0136097200 16 What is Sudoku?

17 Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved. 0136097200 17 Every row contains the numbers 1 to 9

18 Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved. 0136097200 18 Every column contains the numbers 1 to 9 5 34 6 78 9 1 2 67 2 1 9 5 3 4 8 1 9 83 4 2 5 67 85 9 7 6 1 4 2 3 42 6 85 37 9 1 71 3 9 24 8 5 6 9 61 5 3 7 2 8 4 2 8 7 4 1 9 6 3 5 3 4 5 2 86 1 7 9

19 Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved. 0136097200 19 Every 3×3 box contains the numbers 1 to 9 5 34 6 78 9 1 2 67 2 1 9 5 3 4 8 1 9 83 4 2 5 67 85 9 7 6 1 4 2 3 42 6 85 37 9 1 71 3 9 24 8 5 6 9 61 5 3 7 2 8 4 2 8 7 4 1 9 6 3 5 3 4 5 2 86 1 7 9

20 Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved. 0136097200 20 Solve a Sudoku Puzzle Run

21 Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved. 0136097200 21 Strategy Rule 1: Fill in an empty cell from the first to the last. Rule 2: Fill in a smallest number possible. Rule 3: If no number can fill in a cell, backtrack.

22 Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved. 0136097200 22 Strategy Rule 1: Fill in an empty cell from the first to the last. Rule 2: Fill in a smallest number possible. Rule 3: If no number can fill in a cell, backtrack.

23 Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved. 0136097200 23 Strategy Rule 1: Fill in an empty cell from the first to the last. Rule 2: Fill in a smallest number possible. Rule 3: If no number can fill in a cell, backtrack.

24 Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved. 0136097200 24 Strategy Rule 1: Fill in an empty cell from the first to the last. Rule 2: Fill in a smallest number possible. Rule 3: If no number can fill in a cell, backtrack.

25 Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved. 0136097200 25 Strategy Rule 1: Fill in an empty cell from the first to the last. Rule 2: Fill in a smallest number possible. Rule 3: If no number can fill in a cell, backtrack.

26 Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved. 0136097200 26 Strategy Rule 1: Fill in an empty cell from the first to the last. Rule 2: Fill in a smallest number possible. Rule 3: If no number can fill in a cell, backtrack.

27 Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved. 0136097200 27 Strategy Rule 1: Fill in an empty cell from the first to the last. Rule 2: Fill in a smallest number possible. Rule 3: If no number can fill in a cell, backtrack.

28 Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved. 0136097200 28 Strategy Rule 1: Fill in an empty cell from the first to the last. Rule 2: Fill in a smallest number possible. Rule 3: If no number can fill in a cell, backtrack.

29 Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved. 0136097200 29 Strategy Rule 1: Fill in an empty cell from the first to the last. Rule 2: Fill in a smallest number possible. Rule 3: If no number can fill in a cell, backtrack. Try 1, 2, …, 7, not valid, 8 is OK.

30 Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved. 0136097200 30 Strategy Rule 1: Fill in an empty cell from the first to the last. Rule 2: Fill in a smallest number possible. Rule 3: If no number can fill in a cell, backtrack. Try 1, 2, …, 7, 8, not valid, 9 is OK.

31 Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved. 0136097200 31 Strategy Rule 1: Fill in an empty cell from the first to the last. Rule 2: Fill in a smallest number possible. Rule 3: If no number can fill in a cell, backtrack. Try 1, 2, …, 7, 8, 9, none is valid, so you have to backtrack.

32 Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved. 0136097200 32 Strategy Rule 1: Fill in an empty cell from the first to the last. Rule 2: Fill in a smallest number possible. Rule 3: If no number can fill in a cell, backtrack. Backtrack to the preceding free cell. It is 9, you have to backtrack again to the preceding free cell

33 Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved. 0136097200 33 Strategy Rule 1: Fill in an empty cell from the first to the last. Rule 2: Fill in a smallest number possible. Rule 3: If no number can fill in a cell, backtrack. No try number 9. It is valid.

34 Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved. 0136097200 34 Strategy Rule 1: Fill in an empty cell from the first to the last. Rule 2: Fill in a smallest number possible. Rule 3: If no number can fill in a cell, backtrack. Simulation of the Search Process

35 Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved. 0136097200 35 Identify All Free Cells 0 1 2 3 4 5 6 7 8 012345678012345678

36 Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved. 0136097200 36 Search for a solution The search(int grid[][9]) function starts search from the first free cell with k = 0, where k is the position of the current free cell being considered in the free cell list.

37 Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved. 0136097200 37 Search ends successfully The search function returns true when the search advances but no more free cells are left. A solution is found.

38 Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved. 0136097200 38 Search ends with no solution The search returns false when the search is backtracked to the first cell and all possible values are exhausted for the cell. No solution can be found.

39 Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved. 0136097200 39 Example of no Solution

40 Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved. 0136097200 40 Source Code Sudoku Run Run with prepared input

41 Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved. 0136097200 41 Multidimensional Arrays In the preceding section, you used a two-dimensional array to represent a matrix or a table. Occasionally, you will need to represent n-dimensional data structures. In C++, you can create n-dimensional arrays for any integer n. The way to declare two-dimensional array can be generalized to declare n-dimensional array for n >= 3. For example, the following syntax declares a three- dimensional array scores. double scores[10][5][2];

42 Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved. 0136097200 42 Problem: Daily Temperature and Humidity Suppose a meteorology station records the temperature and humidity at each hour of every day and stores the data for the past ten days in a text file named weather.txt. Each line of the file consists of four numbers that indicates the day, hour, temperature, and humidity. The contents of the file may look like the one in (a): Weather Run Your task is to write a program that calculates the average daily temperature and humidity for the 10 days.

43 Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved. 0136097200 43 Problem: Guessing Birth Date Listing 3.2, GuessBirthDate.cpp, gives a program that guesses a birth date. The program can be simplified by storing the numbers in five sets in a three dimensional array and prompts the user for the answers using a loop. GuessBirthDateUsingArrayRun


Download ppt "Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved. 0136097200 1 Chapter 8 Multidimensional."

Similar presentations


Ads by Google