Lecture_Notes_26_Arrays

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

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

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

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

View Full Document
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
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

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

View Full Document
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; }