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

Slides:



Advertisements
Similar presentations
Copyright © 2002 Pearson Education, Inc. Slide 1.
Advertisements

Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Fall 2013.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 7- 1 Overview 7.1 Introduction to Arrays 7.2 Arrays in Functions 7.3.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 14 – Student Grades Application: Introducing.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 6 Arrays.
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 Java Programming, Sixth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 3 Control Statements.
Chapter 8 Arrays and Strings
Chapter 9 Introduction to Arrays
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 7 Multidimensional.
INF120 Basics in JAVA Programming AUBG, COS dept, Spring 2014 Lecture 10 Title: Arrays, Part 2 Multidimensional Arrays Reference: MalikFarrell, chap 1,
Copyright © 2012 Pearson Education, Inc. Chapter 8 Two Dimensional Arrays.
Array Processing Simple Program Design Third Edition A Step-by-Step Approach 7.
© Copyright 2013 by Pearson Education, Inc. All Rights Reserved.1 Chapter 4 Mathematical Functions, Characters, and Strings.
JAVA: An Introduction to Problem Solving & Programming, 7 th Ed. By Walter Savitch ISBN © 2015 Pearson Education, Inc., Upper Saddle River,
Chapter 8 Arrays and Strings
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.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Searching Arrays Searching.
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, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 8 Multidimensional Arrays Lecture.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 7 Multidimensional.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved
Chapter 6 Arrays.
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.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Arrays.
Multidimensional Array Sample Projects. Grading Program Objective: write a program that grades multiple-choice test.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 7 Multidimensional.
© 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.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter 6 Arrays.
Section 5 - Arrays. Problem solving often requires information be viewed as a “list” List may be one-dimensional or multidimensional List is implemented.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Tutorial 18 – Student Grades Application Introducing.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 6 Arrays.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter Array Basics.
An Introduction to Programming with C++ Sixth Edition Chapter 12 Two-Dimensional Arrays.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Student Grades Application Introducing Two-Dimensional Arrays and RadioButton.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
© Copyright 2012 by Pearson Education, Inc. All Rights Reserved. 1 Chapter 11 Lists for Multi-dimensional Data.
Arrays Declaring arrays Passing arrays to functions Searching arrays with linear search Sorting arrays with insertion sort Multidimensional arrays Programming.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 6 Multidimensional.
Chapter 9 Introduction to Arrays Fundamentals of Java.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 7 Multidimensional Arrays.
LESSON 8: INTRODUCTION TO ARRAYS. Lesson 8: Introduction To Arrays Objectives: Write programs that handle collections of similar items. Declare array.
1 Chapter 7 Multidimensional Arrays. 2 Motivations You can use a two-dimensional array to represent a matrix or a table.
Chapter 8 Multidimensional Arrays
Chapter 7 Multidimensional Arrays
Chapter 8 Multidimensional Arrays
Chapter 6 Arrays DDC 2133 Programming II.
Chapter 6 Arrays Lecturer: Mrs Rohani Hassan
Motivations Thus far, you have used one-dimensional arrays to model linear collections of elements. You can use a two-dimensional array to represent a.
Case Study 2 – Marking a Multiple-choice Test
Chapter 8 Multidimensional Arrays
Chapter 8 Multidimensional Arrays
Chapter 8 Multi-Dimensional Arrays
Chapter 7 Multidimensional Arrays
Chapter 8 Multidimensional Arrays
Chapter 8 Multidimensional Arrays
Multidimensional Arrays
Chapter 8 Multidimensional Arrays
Multidimensional Arrays
Chapter 8 Multidimensional Arrays
Chapter 8 Multidimensional Arrays
Chapter 7 Multidimensional Arrays
Introducing Arrays Array is a data structure that represents a collection of the same types of data. From resourses of Y. Daniel Liang, “Introduction.
Chapter 7 Multidimensional Arrays
Chapter 8 Multidimensional Arrays
Chapter 7 Multidimensional Arrays
Chapter 8 Multidimensional Arrays
Presentation transcript:

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

Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved 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).

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

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

Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved 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,

Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved 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; }

Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved 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; }

Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved 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; }

Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved 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; }

Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved 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.

Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved 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

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

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

Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved 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).

Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved 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.

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

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

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

Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Every 3×3 box contains the numbers 1 to

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

Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved 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.

Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved 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.

Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved 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.

Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved 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.

Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved 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.

Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved 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.

Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved 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.

Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved 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.

Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved 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.

Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved 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.

Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved 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.

Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved 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

Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved 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.

Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved 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

Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Identify All Free Cells

Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved 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.

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

Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved 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.

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

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

Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved 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];

Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved 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.

Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved 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