Lecture_Notes_26_Arrays

Lecture_Notes_26_Arrays - 4 When you declare an array using...

Info icon This preview shows pages 1–7. Sign up to view the full content.

View Full Document Right Arrow Icon
Declaring Two Dimensional Arrays To represent a matrix data, we will use 2-D arrays. int myMatrix[2][3] = {{4, 5, 34}, {3, 12, 22}}; int myMatrix[2][3] = {{4, 5}, {3}}; 4 5 34 3 12 22 Number of rows Number of columns 4 5 0 3 0 0
Image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Declaring Two Dimensional Arrays int myMatrix[2][3] = {4, 5, 34, 3, 12, 22}; int myMatrix[2][3] = {4, 5, 34, 3}; 4 5 34 3 12 22 Number of rows Number of columns 4 5 34 3 0 0
Image of page 2
How 2D arrays are stored in memory The storage of 2D arrays in C/C++ is row major e.g., int myMatrix[2][3] = {{4, 5, 34}, {3, 12, 22}}; 4 5 34 3 12 22
Image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Accessing 2-D Array Elements #include <iostream> using namespace std; int main() { int myMatrix[2][3] = {{4, 5, 34}, {3, 12, 22}}; for ( int i=0; i<2; i++) { for ( int j=0; j<3; j++) { cout<< "myMatrix[" <<i<< "][" <<j<< "] = " <<myMatrix[i][j]<<endl; } } system( "PAUSE" ); return 0; }
Image of page 4