Chapter 13 Vector of Vectors (2D Arrays)

Slides:



Advertisements
Similar presentations
Matrices A matrix is a rectangular array of quantities (numbers, expressions or function), arranged in m rows and n columns x 3y.
Advertisements

Engineering Problem Solving with C++ An Object Based Approach Chapter 7 Two-Dimensional Arrays and Matrices.
Maths for Computer Graphics
11-1 Chapter 11 2D Arrays Asserting Java Rick Mercer.
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.
©2004 Brooks/Cole Chapter 8 Arrays. Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Sometimes we have lists of data values that all need to be.
Chapter 8. 2 Objectives You should be able to describe: One-Dimensional Arrays Array Initialization Arrays as Arguments Two-Dimensional Arrays Common.
Multiple-Subscripted Array
11-1 Chapter 11 2D Arrays Asserting Java Rick Mercer.
Little Linear Algebra Contents: Linear vector spaces Matrices Special Matrices Matrix & vector Norms.
CPS120: Introduction to Computer Science Arrays. Arrays: A Definition A list of variables accessed using a single identifier May be of any data type Can.
Copyright © 2012 Pearson Education, Inc. Chapter 8 Two Dimensional Arrays.
Chapter 8 Arrays and Strings
Scope When we create variables and functions, they are limited in where they are visible and where they can be referenced For the most part, the identifiers.
CPS120: Introduction to Computer Science Lecture 15 Arrays.
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.
POINTERS.
Matrices: Simplifying Algebraic Expressions Combining Like Terms & Distributive Property.
Pointers It provides a way of accessing a variable without referring to its name. The mechanism used for this is the address of the variable.
Arrays.
Chapter 9 Introduction to Arrays Fundamentals of Java.
Matrices. Variety of engineering problems lead to the need to solve systems of linear equations matrixcolumn vectors.
ENG College of Engineering Engineering Education Innovation Center 1 Arrays in MATLAB Topics Covered: 1.Creating arrays of numbers vectors matrices.
A FIRST BOOK OF C++ CHAPTER 7 ARRAYS. OBJECTIVES In this chapter, you will learn about: One-Dimensional Arrays Array Initialization Arrays as Arguments.
Lesson 43: Working with Matrices: Multiplication
12-1 Organizing Data Using Matrices
Multiplying Matrices.
ECE 1304 Introduction to Electrical and Computer Engineering
Matrices Rules & Operations.
Linear Algebra review (optional)
I/O Streams File I/O 2-D array review
EGR 2261 Unit 10 Two-dimensional Arrays
Two-Dimensional Arrays
2-D Array.
Chapter 7 Matrix Mathematics
1.5 Matricies.
Chapter 8: Arrays Starting Out with C++ Early Objects Ninth Edition
Computer Programming BCT 1113
Two Dimensional Array Mr. Jacobs.
Two-Dimensional Arrays Lesson xx
5. Function (2) and Exercises
Introduction To Matrices
Matrix Operations.
JavaScript: Functions.
Computer Graphics Matrix
Two Dimensional Arrays
7.3 Matrices.
Engineering Problem Solving with C++, Etter/Ingber
Multidimensional Arrays Vectors of Vectors
Using Free Functions Chapter 3 Computing Fundamentals with C++
Arrays We often want to organize objects or primitive data in a way that makes them easy to access and change. An array is simple but powerful way to.
1020: Introduction to Programming Mohamed Shehata November 22, 2017
Arrays An array is a collection of variables that all have the same name and the same data type. Each member of the array is known as an element of the.
4.9 Multiple-Subscripted Arrays
Chapter 12 Pointers and Memory Management
Chapter 11 Generic Collections
2.2 Introduction to Matrices
Data Structures (CS212D) Week # 2: Arrays.
Multidimensional array
Multidimensional Arrays
Computing Fundamentals with C++
Alternate Version of STARTING OUT WITH C++ 4th Edition
Arrays Arrays A few types Structures of related data items
Linear Algebra review (optional)
Working with Arrays in MATLAB
Arrays and Matrices Prof. Abdul Hameed.
Dr. Khizar Hayat Associate Prof. of Computer Science
Chapter 6 - Arrays Outline Multiple-Subscripted Arrays.
Presentation transcript:

Chapter 13 Vector of Vectors (2D Arrays) 3rd Edition Computing Fundamentals with C++ Rick Mercer Franklin, Beedle & Associates

Goals Process data stored as a vector of vectors (rows and columns) Use nested for loops Show a couple matrix operations

Vector of Vectors Data that conveniently presents itself in a tabular format is represented well with a vector of vectors General form vector <vector<type> > identifier (rows, vector<type> (cols, initialValueoptional )); Example declaration (the space is needed > >) vector <vector<int> > nums(5, vector<int> (3, -1)); // All 15 values (5 rows, 3 columns) are -1 Set the value of the first row and column to 9 nums[0][0] = 9; Space required

Rows and columns Change a few more elements nums[1][1] = 8; nums[2][2] = 7; nums[3][1] = 6; nums[4][0] = 5; Nested for loops are often used with a vector of vectors for (int row = 0; row < nums.size(); row++) { for (int col = 0; col < nums[0].size(); col++) { cout.width(4); cout << nums[row][col]; } cout << endl; Output: 9 -1 8 7 6 5 nums[2][0] nums[4][2] 9 -1 -1 -1 8 -1 -1 -1 7 -1 6 -1 5 -1 -1

Matrix Operations A matrix is a rectangular vector of numbers, symbols, or expressions, arranged in rows and columns The data structure best suited to represent a matrix in C++? A vector of vectors This presentation shows of a couple of the operations on this data structure

Build a 6x6 Matrix vector<vector<int> > nums(6, vector<int>(6)); // all 36 values (6 rows, 6 columns) are garbage // Initialize to arbitrary values: 1..36 int count = 1; for (int row = 0; row < nums.size(); row++) { for (int col = 0; col < nums[0].size(); col++) { nums[row][col] = count; count++; } } Matrix: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36

Scalar Multiplication Scalar multiplication takes a number (a "scalar") and multiplies it on every element in the matrix int scalar = 2; // Multiply all elements by 2 for (int row = 0; row < nums.size(); row++) { for (int col = 0; col < nums[0].size(); col++) { nums[row][col] *= scalar; } Matrix: 2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 42 44 46 48 50 52 54 56 58 60 62 64 66 68 70 72

Transpose the Matrix The transpose of a Matrix makes all row the columns and all columns the rows // Create a transposed version vector<vector<int> > transpose(6, vector<int>(6)); for (int row = 0; row < nums.size(); row++) { for (int col = 0; col < nums[0].size(); col++) { transpose[col][row] = nums[row][col]; } // Copy the transposed matrix back into nums nums[row][col] = transpose[row][col];

Before and After before after The first row (before) is now the first column (after) The last row (before) is now the last column (after) The 2nd row (before) is now the 2nd column (after) . . . and so on before after 2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 42 44 46 48 50 52 54 56 58 60 62 64 66 68 70 72 2 14 26 38 50 62 4 16 28 40 52 64 6 18 30 42 54 66 8 20 32 44 56 68 10 22 34 46 58 70 12 24 36 48 60 72

Primitive Arrays There are similarities between vector and primitive C++ arrays There are also similarities between a vector of vectors and primitive C++ arrays with two subscripts vector<vector<int> > table(4, vector<int>(6)); // Ugh string table[4][6]; // Simpler declaration Elements are accessed the same way table[0][0]= "Upper left"; table[3][5]= "Lower right";

Array Initializers Primitive arrays are easier to declare feel free to use them instead of vectors A lot of code uses the primitive C++ arrays Arrays also have initializers, values between { } separated by commas double oneSubscript[] = {1.5, 0.9, 0.03, 4.2}; int twoSubscripts[2][3] = { {1, 2, 3}, {4, 5, 6} };

Arrays with more than 2 Subscripts? Yes, they are possible and sometimes useful double threeD[2][35][10]; threeD[1][34][9] = 87;