Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved. 0136097200 1 Chapter 6 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.
Engineering Problem Solving with C++ An Object Based Approach Chapter 7 Two-Dimensional Arrays and Matrices.
Slides prepared by Rose Williams, Binghamton University Chapter 6 Arrays.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 6 Arrays.
#include using namespace std; void main() { int a[3]={10,11,23}; for(int i=0;i
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.
Chapter 4 Summation.
Multiple-Subscripted Array
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 7 Multidimensional.
What is the out put #include using namespace std; void main() { int i; for(i=1;i
Copyright © 2012 Pearson Education, Inc. Chapter 8 Two Dimensional Arrays.
JAVA: An Introduction to Problem Solving & Programming, 7 th Ed. By Walter Savitch ISBN © 2015 Pearson Education, Inc., Upper Saddle River,
1 DATA STRUCTURES: LISTS. 2 LISTS ARE USED TO WORK WITH A GROUP OF VALUES IN AN ORGANIZED MANNER. A SERIES OF MEMORY LOCATIONS CAN BE DIRECTLY REFERENCED.
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.
 2005 Pearson Education, Inc. All rights reserved. 1 Arrays.
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.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 3 Selections.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 4 Loops.
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.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter 6 Arrays.
Two-Dimensional Arrays ELEC 206 Computer Applications for Electrical Engineers Dr. Ron Hayne.
Section 5 - Arrays. Problem solving often requires information be viewed as a “list” List may be one-dimensional or multidimensional List is implemented.
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 X1 Chapter 4 Loops.
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter Array Basics.
CS Class 15 Today  More practice with arrays  Introduction to Multi-dimensional arrays Announcements  Programming project #4 due 10/23 by midnight.
Module 1: Array ITEI222 - Advance Programming Language.
© Copyright 2012 by Pearson Education, Inc. All Rights Reserved. 1 Chapter 11 Lists for Multi-dimensional Data.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 6 Arrays.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 4 Loops.
Lecture 9 – Array (Part 2) FTMK, UTeM – Sem /2014.
Two-Dimensional Data Class of 5 students Each student has 3 test scores Store this information in a two- dimensional array First dimension: which student.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 6 Arrays.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 7 Multidimensional Arrays.
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
Multi-dimensional Array
Case Study 2 – Marking a Multiple-choice Test
Chapter 4 Loops Case Studies
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 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 6 Multidimensional Arrays

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 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 int total = 0; for (int row = 0; row < rowSize; row++) { for (int column = 0; column < columnSize; column++) { total+=matrix[row][column]; }

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 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.cpp

Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved PassTwoDimensionalArray.cpp #include using namespace std; const int COLUMN_SIZE = 3; int sum(const int a[][COLUMN_SIZE], int rowSize) { int total = 0; for (int row = 0; row < rowSize; row++) { for (int column = 0; column < COLUMN_SIZE; column++) { total += a[row][column]; } return total; } int main() { int array[4][3] = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9}, {10, 11, 12} }; cout << "Sum of all elements is " << sum(array, 4) << endl; system("PAUSE"); return 0; }

Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved PassTwoDimensionalArray.cpp int main() { int array[4][3] = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9}, {10, 11, 12} }; cout << "Sum of all elements is " << sum(array, 4) << endl; system("PAUSE"); return 0; }

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. GradeExam.cpp

Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Example: Grading Multiple-Choice Test GradeExam.cpp #include using namespace std; int main() { const int NUMBER_OF_STUDENTS = 8; const int NUMBER_OF_QUESTIONS = 10; // Students' answers to the questions char answers[NUMBER_OF_STUDENTS][NUMBER_OF_QUESTIONS] = { {'A', 'B', 'A', 'C', 'C', 'D', 'E', 'E', 'A', 'D'}, {'D', 'B', 'A', 'B', 'C', 'A', 'E', 'E', 'A', 'D'}, {'E', 'D', 'D', 'A', 'C', 'B', 'E', 'E', 'A', 'D'}, {'C', 'B', 'A', 'E', 'D', 'C', 'E', 'E', 'A', 'D'}, {'A', 'B', 'D', 'C', 'C', 'D', 'E', 'E', 'A', 'D'}, {'B', 'B', 'E', 'C', 'C', 'D', 'E', 'E', 'A', 'D'}, {'B', 'B', 'A', 'C', 'C', 'D', 'E', 'E', 'A', 'D'}, {'E', 'B', 'E', 'C', 'C', 'D', 'E', 'E', 'A', 'D'} };

Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved GradeExam.cpp // Key to the questions char keys[] = {'D', 'B', 'D', 'C', 'C', 'D', 'A', 'E', 'A', 'D'}; // Grade all answers for (int i = 0; i < NUMBER_OF_STUDENTS; i++) { // Grade one student int correctCount = 0; for (int j = 0; j < NUMBER_OF_QUESTIONS; j++) { if (answers[i][j] == keys[j]) correctCount++; } cout << "Student " << i << "'s correct count is " << correctCount << endl; } return 0; }

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