1 / 7

70 likes | 336 Views

Multidimensional Arrays Vectors of Vectors. There Could Be More than One. Two Dimensional Array Basics. matrices, tables and similar information is often naturally represented by a two-dimensional array first index – row second index - column

Download Presentation
## Multidimensional Arrays Vectors of Vectors

**An Image/Link below is provided (as is) to download presentation**
Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.
Content is provided to you AS IS for your information and personal use only.
Download presentation by click this link.
While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.
During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

**Multidimensional ArraysVectors of Vectors**There Could Be More than One**Two Dimensional Array Basics**matrices, tables and similar information is often naturally represented by a two-dimensional array first index – row second index - column declaration: basicTypearrayName[rows][columns]; example int a[3][4]; accessing a[0][1] = 23; cout << a[1][3]; c[2][4]+=55; either index out of range is still an error a[2][4] = 55; // error larger dimensions are possible: int b[10][100][200];**Two Dimensional Arrays in Loops**nested loops are useful with multidimensional arrays for (int i=0; i < length; ++i) for (int j=0; j < width; ++j) cout << a[i][j] << ” ”;**Arrays and Functions**• if multidimensional array is used as parameters, all but first dimension have to be stated • the first dimension can be passed as separate parameter const int width=3; void printArray(int c[][width], int length){ for (int i=0; i < length; ++i) for (int j=0; j < width; ++j) cout << c[i][j] << ” ”; }**Vectors of Vectors**• alternative to multidimensional arrays • outer vector’s type is declared to be a vector vector<vector<int>> a; • inner vectors can then be added to the outer vector vector<int> row(width); for(inti=0; i < length; ++i) a.push_back(row); • vector elements can be accessed as in multidimensional array a[1][2] = 55; • vectors retain all advantages over arrays: have extra functions, carry size, can pass by value/return, can change size on demand • example: can use size() in a loop iterating over vector for (inti=0; i < a.size(); ++i) for (int j=0; j < a[i].size(); ++j) cout << a[i][j] << ” ”;**a.erase(a.begin()+1);**a.erase(a.begin()+1); Ragged (Jagged) Array a.erase(a.begin()+1); • ragged (jagged) array: rows can vary in size • example: program seats in a (non-square) auditorium or in airplane • what does this code do? vector<int> row; vector<vector<int>> a; for(inti=0; i < 4; ++i){ row.push_back(i); a.push_back(row); } • what does this code do? a.erase(a.begin()+1); a[2].insert(a[2].begin()+1, 55);**Review Questions**• Why are multidimensional arrays necessary? • How does one declare a two-dimensional array? • What is the first index? the second index? • How does one access an element of multidimensional array? • Why are nested loops useful with multidimensional arrays? • How is multidimensional array passed as a parameter to a function? • What is vector of vectors? • How does one declare a vector of vectors? Initialize it? • How are individual elements of a vector of vectors accessed? • What are the advantages of a vector of vectors over multidimensional array? • What is ragged/jagged array? How does one determine the size of a row in a ragged array? How does one change the number of rows in a ragged array? How does one change the number of elements in a row in a ragged array?

More Related