Lecture_Notes_26_Arrays

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

Info iconThis 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
Background image of page 1

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

View Full DocumentRight 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
Background 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
Background image of page 3

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

View Full DocumentRight 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; }
Background image of page 4
Arrays 1. An array can be of any number of dimensions 2. If you declare 2-D array element a[x][y] incorrectly as a[x,y], it would be treated as a[y] 3. Arrays are static and their size can not be changed
Background image of page 5

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

View Full DocumentRight Arrow Icon
Background image of page 6
Background image of page 7
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: 4. When you declare an array using element-list, first dimension is optional. However, the second dimension is must. Passing 2-D arrays To pass 2D array to functions, the size of first subscript is optional. Compiler ignores the first subscript. However, it is must to specify the second subscript Example: Passing 2-D arrays void printArray( int a[3]) { for ( int i=0; i<2; i++) { for ( int j=0; j<3; j++) cout<<a[i][j]<< " " ; cout<<endl; } } int main() { int array1[2][3]={{1, 2, 3},{4, 5, 6}}, array2[2][3]={1, 2, 3, 4, 5}, array3[2][3]={{1, 2}, {4}}; cout<< "Values in array1 by row are: " <<endl; printArray(array1); cout<< "Values in array2 by row are: " <<endl; printArray(array2); cout<< "Values in array3 by row are: " <<endl; printArray(array3); system( "PAUSE" ); return 0; }...
View Full Document

This note was uploaded on 04/01/2012 for the course GENE 121 taught by Professor Smucker during the Winter '11 term at Waterloo.

Page1 / 7

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

This preview shows document pages 1 - 7. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online