Presentation is loading. Please wait.

Presentation is loading. Please wait.

Multidimensional Arrays Vectors of Vectors

Similar presentations


Presentation on theme: "Multidimensional Arrays Vectors of Vectors"— Presentation transcript:

1 Multidimensional Arrays Vectors of Vectors
There Could Be More than One

2 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: basicType arrayName[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];

3 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] << ” ”;

4 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] << ” ”; }

5 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(int i=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 (int i=0; i < a.size(); ++i) for (int j=0; j < a[i].size(); ++j) cout << a[i][j] << ” ”;

6 Ragged (Jagged) Array ragged (jagged) array: rows can vary in size
a.erase(a.begin()+1); a.erase(a.begin()+1); a.erase(a.begin()+1); Ragged (Jagged) Array 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(int i=0; i < 4; ++i){ row.push_back(i); a.push_back(row); } a.erase(a.begin()+1); a[2].insert(a[2].begin()+1, 55);

7 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?


Download ppt "Multidimensional Arrays Vectors of Vectors"

Similar presentations


Ads by Google