Two Dimensional Arrays

Slides:



Advertisements
Similar presentations
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 8: Arrays Starting Out with C++ Early Objects Seventh Edition by.
Advertisements

Engineering Problem Solving with C++ An Object Based Approach Chapter 7 Two-Dimensional Arrays and Matrices.
Maths for Computer Graphics
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 6 - Arrays Outline 6.1Introduction 6.2Arrays.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 8 Multidimensional.
Chapter 8. 2 Objectives You should be able to describe: One-Dimensional Arrays Array Initialization Arrays as Arguments Two-Dimensional Arrays Common.
1 Arrays  Arrays are objects that help us organize large amounts of information  Chapter 8 focuses on: array declaration and use passing arrays and array.
 2003 Prentice Hall, Inc. All rights reserved. 1 Arrays –Structures of related data items –Static entity (same size throughout program) A few types –Pointer-based.
Chapter 6 C Arrays Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc. Arrays are data structures.
C++ for Engineers and Scientists Third Edition
Chapter 8 Arrays and Strings
CE 311 K - Introduction to Computer Methods Daene C. McKinney
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 7.1 Solving Systems of Two Equations.
Modular Programming Chapter Value and Reference Parameters t Function declaration: void computesumave(float num1, float num2, float& sum, float&
Chapter 7: Arrays. In this chapter, you will learn about: One-dimensional arrays Array initialization Declaring and processing two-dimensional arrays.
Copyright © 2012 Pearson Education, Inc. Chapter 8 Two Dimensional Arrays.
 2006 Pearson Education, Inc. All rights reserved Arrays.
Modular Programming Chapter Value and Reference Parameters computeSumAve (x, y, sum, mean) ACTUALFORMAL xnum1(input) ynum2(input) sumsum(output)
Chapter 8 Arrays and Strings
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.
© Copyright 2013 by Pearson Education, Inc. All Rights Reserved. 1 Chapter 8 Multidimensional Arrays.
C++ for Engineers and Scientists Second Edition Chapter 11 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.
1 Chapter 7 Arrays. 2 Topics 7.1 Arrays Hold Multiple Values 7.2 Accessing Array Elements 7.3 No Bounds Checking in C Array Initialization 7.5 Processing.
A First Book of C++: From Here To There, Third Edition2 Objectives You should be able to describe: One-Dimensional Arrays Array Initialization Arrays.
Arrays. Related data items Collection of the same types of data. Static entity – Same size throughout program.
Slide Copyright © 2009 Pearson Education, Inc. 7.3 Matrices.
 2007 Pearson Education, Inc. All rights reserved C Arrays.
Copyright © 2002 W. A. Tucker1 Chapter 9 Lecture Notes Bill Tucker Austin Community College COSC 1315.
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 7 Arrays.
Two-Dimensional Arrays and Matrices ELEC 206 Computer Applications for Electrical Engineers.
SEQUENTIAL AND OBJECT ORIENTED PROGRAMMING Arrays.
Matrices. Variety of engineering problems lead to the need to solve systems of linear equations matrixcolumn vectors.
Arrays An array is a sequence of objects all of which have the same type. The objects are called the elements of the array and are numbered consecutively.
A FIRST BOOK OF C++ CHAPTER 7 ARRAYS. OBJECTIVES In this chapter, you will learn about: One-Dimensional Arrays Array Initialization Arrays as Arguments.
MTH108 Business Math I Lecture 20.
User-Written Functions
EGR 2261 Unit 10 Two-dimensional Arrays
Chapter 7 Matrix Mathematics
Chapter 8: Arrays Starting Out with C++ Early Objects Ninth Edition
Computer Programming BCT 1113
© 2016 Pearson Education, Ltd. All rights reserved.
Programming Fundamental
Multi-Dimension Arrays
Matrix Operations SpringSemester 2017.
Java Review: Reference Types
Lecture 6 C++ Programming
C Arrays.
7.3 Matrices.
Engineering Problem Solving with C++, Etter/Ingber
Array Data Structure Chapter 6
One-Dimensional Array Introduction Lesson xx
7 Arrays.
Array Data Structure B.Ramamurthy 11/21/2018 B.Ramamurthy.
Arrays Kingdom of Saudi Arabia
4.9 Multiple-Subscripted Arrays
2.2 Introduction to Matrices
Standard Version of Starting Out with C++, 4th Edition
Multidimensional Arrays
Engineering Problem Solving with C++, Etter
7 Arrays.
Engineering Problem Solving with C++ An Object Based Approach
Array Data Structure Chapter 6
Arrays Arrays A few types Structures of related data items
Matrix Operations SpringSemester 2017.
C++ Array 1.
Arrays.
Presentation transcript:

Two Dimensional Arrays Copyright © 2012 Pearson Education, Inc. Chapter 8 Two Dimensional Arrays

Copyright © 2012 Pearson Education, Inc. Outline Objectives Two Dimensional Arrays Problem Solving Applied: Terrain Navigation Two Dimensional Arrays and the vector Class Matrices Numerical technique: Solution to Simultaneous Equations Problem Solving Applied: Electrical-Circuit Analysis Higher Dimensional Arrays Copyright © 2012 Pearson Education, Inc.

Copyright © 2012 Pearson Education, Inc. Objectives Develop problem solutions in C++ containing: Matrix computations Input from Data Files Functions to Compute Sums and Averages Techniques for solving a system of simultaneous equations Vectors to implement the concept of two-dimensional arrays Copyright © 2012 Pearson Education, Inc.

Two Dimensional Arrays Copyright © 2012 Pearson Education, Inc.

Two Dimensional Arrays Declaration and Initialization Computation and Output Function Arguments Copyright © 2012 Pearson Education, Inc.

Two Dimensional Arrays A two dimensional array stores data as a logical collection of rows and columns. Each element of a two-dimensional array has a row position and a column position. To access an element in a two-dimensional array, you must specify the name of the array followed by: a row offset a column offset Copyright © 2012 Pearson Education, Inc.

Declaration and Initialization The declaration of a two-dimensional array requires a row size and a column size. A consecutive block of (row size)*(column size) memory locations are allocated. All array elements must be of the same type. Elements accessed by two offsets – a row offset and a column offset. The name of the array holds the address of the first byte of memory Copyright © 2012 Pearson Education, Inc.

Copyright © 2012 Pearson Education, Inc. Example //Declaration int data[2][3]; Memory Snapshot ? data Copyright © 2012 Pearson Education, Inc.

Copyright © 2012 Pearson Education, Inc. Example //Declaration int data[2][3]; row/column form: col 0 col 1 col 2 ? row 0 row 1 Copyright © 2012 Pearson Education, Inc.

2D Array Definition Syntax data_type identifier[ [row_size] ][column_size] [= initialization_list ]; //row_size and column_size must be integer constants Examples int data[2][5]; //allocates consecutive memory for 10 integer values double t[2][2] = {{3.0,5.0},{2.1,7.2}}; //allocates and initializes Valid References cout << data[1][3]; cout << t[1][0]; Invalid References cout << data[2][5]; //invalid offset. cout << t[-1][-1]; //invalid offset Copyright © 2012 Pearson Education, Inc.

Initialization Examples int temp[4][3] = {50, 70, 60, 48, 75, 62, 51, 69, 60, 52, 78, 63}; int temp[4][3] = {{50, 70, 60}, {48, 75, 62}, {51, 69, 60}, {52, 78, 63}}; int t2[7][4] = {{50, 70, 60}, {48, 75, 62}, {51, 69, 60}, {52, 78, 63}}; int temp[][3] = {{50, 70, 60}, {48, 75, 62}, {51, 69, 60}, {52, 78, 63}}; int temp[][3] = {50, 70, 60, 48, 75, 62, 51, 69, 60, 52, 78, 63}; Copyright © 2012 Pearson Education, Inc.

Example: Input Using cin Nested for loops are often used when inputting and assigning values to a two-dimensional array. Nested loops are generally useful for getting around the 2D arrays… //Declaration double table[RSIZE][CSIZE]; for (int i=0; i<RSIZE; ++i) //every row for (int j=0; j<CSIZE; ++j )//every col cin >> table[i][j]; Copyright © 2012 Pearson Education, Inc.

Copyright © 2012 Pearson Education, Inc. Example: Assignment //Declaration const int RSIZE(3),CSIZE(2); double v[RSIZE][CSIZE]; for (int i=0; i<RSIZE; ++i) //every row for (int j=0; j<CSIZE; ++j )//every col v[i][j] = i+j; V 1 2 3 Copyright © 2012 Pearson Education, Inc.

Example: Computations Compute the average value of an array with n rows and m columns. double sum(0), average; for (int i=0; i<n; ++i) //every row for (int j=0; j<m; ++j )//every col sum += array[i][j]; average = sum / (n*m); Copyright © 2012 Pearson Education, Inc.

Example: Computations Compute the average value of the nth row of a 2D array with r rows and c columns. double sum(0), rowAverage; for (int j=0; j<c; ++j ) //every col sum += array[n][j]; average = sum / c; Copyright © 2012 Pearson Education, Inc.

Copyright © 2012 Pearson Education, Inc. Modify! Modify the C++ statements on the previous slide to compute the average of the mth column. Copyright © 2012 Pearson Education, Inc.

Copyright © 2012 Pearson Education, Inc. Outputting 2D Arrays Two dimensional arrays are often printed in a row by row format, using nested for statements. When printing the row values of an array, be sure to print: whitespace between the values in a row. a newline character at the end of each row. Copyright © 2012 Pearson Education, Inc.

Copyright © 2012 Pearson Education, Inc. Example: Printing for (int i=0; i<n; ++i) {//every row for (int j=0; j<m; ++j )//every col cout << array[i][j] << ‘ ‘; cout << endl; //add end-of-line each row } Copyright © 2012 Pearson Education, Inc.

2D Arrays as Function Parameters 2-D arrays are always passed by reference. The column dimension must be specified. The leftmost dimension (row) may be empty []. Function prototype example: int rowAverage(int Arr[][COLSIZE], int whichRow); Array declaration in main: int table [ROWSIZE][COLSIZE]; Function invocation example: avg = rowAverage(table, 3); Copyright © 2012 Pearson Education, Inc.

Copyright © 2012 Pearson Education, Inc. Documentation Style Well-documented functions always include pre-conditions and post-conditions in the function’s comment header block. Pre-conditions describe the conditions assumed to be true at the time the function is called. If pre-conditions are not met, there is no guarantee that the function will work correctly. Post-conditions describe the changes that will be made to the arguments during the execution of the function. Copyright © 2012 Pearson Education, Inc.

Problem Solving Applied: Terrain Navigation Copyright © 2012 Pearson Education, Inc.

Problem Solving Applied: Terrain Navigation Copyright © 2012 Pearson Education, Inc.

Problem Solving Applied: Terrain Navigation Copyright © 2012 Pearson Education, Inc.

Problem Solving Applied: Terrain Navigation Copyright © 2012 Pearson Education, Inc.

Two Dimensional Arrays and the vector Class Copyright © 2012 Pearson Education, Inc.

2D Arrays using the STL vector Class Just as the vector may be used as a generic implementation of the array concept, the vector class may be used to provide an generic for 2D arrays. Dynamic sizing of array. Methods for determining and modifying size and capacity dynamically. Copyright © 2012 Pearson Education, Inc.

2D vector Definition Syntax vector< vector< type_specifier > > identifier[(row_size,column_size)]; Examples vector<vector<double> > temp(rows,cols); vector<vector<int> > table(10,4); vector<vector<char> > tags; vector<vector<Point> > image(height,width); Copyright © 2012 Pearson Education, Inc.

Passing vector Instances to Functions Unlike 2D arrays, objects of vector types are passed by value to functions. Thus a copy is made of the arguments to the function call, and changes made to the formal parameter will NOT affect the argument value. Pass vector objects by reference to allow the function to alter the argument. Pass vector objects by const reference to avoid copying potentially large datasets. Copyright © 2012 Pearson Education, Inc.

Copyright © 2012 Pearson Education, Inc. Matrices Copyright © 2012 Pearson Education, Inc.

Copyright © 2012 Pearson Education, Inc. Matrix A matrix is a set of numbers arranged in a rectangular grid with rows and columns. A square matrix has the same number of rows as columns. Row and Column offsets in Matrices are 1-based. 2D Arrays are useful for representing matrices. Remember that C++ uses 0-based offsets! Copyright © 2012 Pearson Education, Inc.

Matrix Computations and Operations The determinant of a matrix is a scalar value used in computing matrix inverses and solving systems of simultaneous equations. The transpose of a matrix is a new matrix in which the rows of the original matrix are the columns of the transpose. Matrices of the same size may be added or subtracted element-by-element. Matrix multiplication (M * N) is defined only when the number of columns of M is equal to the number of rows in N. Result has the size rows(M) x cols(N) Copyright © 2012 Pearson Education, Inc.

Numerical Technique: Solution to Simultaneous Equations Copyright © 2012 Pearson Education, Inc.

Graphical Interpretation Solving two simultaneous (linear) equations is finding the point at which the lines intersect. Not all lines intersect, therefore there is not always a solution. In 2D, parallel lines do not intersect unless they are identical lines. When the lines are identical, there is no single point at which they intersect. Copyright © 2012 Pearson Education, Inc.

Graphical Interpretation In high dimensional spaces, analogous situations may occur. Copyright © 2012 Pearson Education, Inc.

Copyright © 2012 Pearson Education, Inc. Solution Methods Simultaneous equations may be solved by a number of various methods, each of which has advantages and disadvantages. Gauss elimination Matrix Inverse and others… Copyright © 2012 Pearson Education, Inc.

Problem Solving Applied: Electrical-Circuit Analysis Copyright © 2012 Pearson Education, Inc.

Problem Solving Applied: Electrical-Circuit Analysis Copyright © 2012 Pearson Education, Inc.

Problem Solving Applied: Electrical-Circuit Analysis Copyright © 2012 Pearson Education, Inc.

Problem Solving Applied: Electrical-Circuit Analysis Copyright © 2012 Pearson Education, Inc.

Problem Solving Applied: Electrical-Circuit Analysis Copyright © 2012 Pearson Education, Inc.

Higher Dimensional Arrays Copyright © 2012 Pearson Education, Inc.

Higher Dimensional Arrays C++ allows arrays to be defined with more than two dimensions. E.g.: int b[3][4][2]; //declare a 3-D array An offset must be specified for each dimension. Same ‘rules’ apply as for 1D and 2D arrays. Offsets are unchecked by C++. Only left-most dimension may be omitted when passing a multidimensional array to a function. Copyright © 2012 Pearson Education, Inc.