 Download Download Presentation Multidimensional Arrays Vectors of Vectors

# Multidimensional Arrays Vectors of Vectors

Download Presentation ## Multidimensional Arrays Vectors of Vectors

- - - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - - -
##### Presentation Transcript

1. Multidimensional ArraysVectors 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: basicTypearrayName[rows][columns]; example int a; accessing a = 23; cout << a; c+=55; either index out of range is still an error a = 55; // error larger dimensions are possible: int b;

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(inti=0; i < length; ++i) a.push_back(row); • vector elements can be accessed as in multidimensional array a = 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] << ” ”;

6. 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.insert(a.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?