Download presentation
Presentation is loading. Please wait.
Published byBrian Webster Modified over 9 years ago
1
Lec 13 Oct 21, 02
2
Array Initialization in the declaration statement ► int temp[5] = {98, 87, 92, 79,85}; ► char codes[6] = { ‘s’, ’a’, ‘m’, ‘p’, ‘l’, ‘e’ }; ► double slopes[3] = {10.96, 6.43, 2.58}; // multi line initialization below. ► int gallons[20] = {19, 16, 14, 19, 20, 18, 12, 10, 22, 15, 18, 17, 12, 10, 22, 15, 18, 17, 16, 14, 23, 19, 15, 18, 16, 14, 23, 19, 15, 18, 21, 5 }; 21, 5 };
3
Array Initialization in the declaration statement contd.. ► float length[7] = {7.8, 6.4, 4.9, 11.2}; / * In the above declaration, number of elements listed in curly brackets is not same as numbers of elements listed in square brackets. Therefore, length[0], length[1], length[2], and length[3] are initilized with listed values. The other array elements will be initialized to zero.*/ / * In the above declaration, number of elements listed in curly brackets is not same as numbers of elements listed in square brackets. Therefore, length[0], length[1], length[2], and length[3] are initilized with listed values. The other array elements will be initialized to zero.*/ ► int gallons[] = {16, 12, 10, 14, 11}; /* A unique feature of initializers is that the size of an array may be omitted when initializing values are included in the declaration statement. /* A unique feature of initializers is that the size of an array may be omitted when initializing values are included in the declaration statement.
4
Array Initialization in the declaration statement contd.. ► char codes[6] = { ‘s’, ‘a’, ‘m’, ‘p’, ‘l’, ‘e’ }; char codes[] = { ‘s’, ‘a’, ‘m’, ‘p’, ‘l’, ‘e’ }; char codes[] = { ‘s’, ‘a’, ‘m’, ‘p’, ‘l’, ‘e’ }; String initialization: String initialization: char codes[] = “sample”; char codes[] = “sample”; // \o below is called null character // \o below is called null character samPle\o
5
Declaring and Processing 2-D arrays ► A two – dimensional array, which is sometimes referred to as a table, consists of both rows and columns of elements. ► Eg: 816952 315276 315276 1425210 1425210 is called a two – dimensional array of integers. This array consists of 3 rows and four columns. is called a two – dimensional array of integers. This array consists of 3 rows and four columns. Declaration of the above array is: Declaration of the above array is: int val[3][4] ; // array named value with 3 rows and 4 int val[3][4] ; // array named value with 3 rows and 4 columns columns
6
Other examples ► float volts[10][5]; /* 2-D array named volts with 10 rows and /* 2-D array named volts with 10 rows and 5 columns */ 5 columns */ ► int code[6][26]; /* 2-D array named code with 6 rows and 26 columns */ /* 2-D array named code with 6 rows and 26 columns */
7
Accessing 2-D arrays ► The term val[1][3] uniquely identifies the element in row 1, column 3. ► watts = val[2][3]; val[0][0] = 62; val[0][0] = 62; newnum = 4 * (val[1][0] -5); newnum = 4 * (val[1][0] -5); Sum=val[0][0]+val[0][1]+val[0][2]+val[0][3]; Sum=val[0][0]+val[0][1]+val[0][2]+val[0][3];
8
Initilization in declaration statement: ► int val[3][4] = { 8, 16, 9, 52, 3, 15, 27, 6, 3, 15, 27, 6, 14, 25, 2, 10}; 14, 25, 2, 10}; (or) int val[3][4] = { {8,16,9,52}, (or) int val[3][4] = { {8,16,9,52}, {3,15,27,6}, {3,15,27,6}, {14,25,2,10} }; {14,25,2,10} }; (or) int val[3][4] ={8,16,9,52,3,15,27,6,14,25,2,10}; (or) int val[3][4] ={8,16,9,52,3,15,27,6,14,25,2,10}; // Note : 2-D array initialization is done row-wise. First, the elements of the first row are initialized, then second row and so on…
9
Example 1 ► #include ► #include int main() int main() { const int NUMROWS = 3; const int NUMCOLS = 4; const int NUMROWS = 3; const int NUMCOLS = 4; int i,j; int i,j; for(i=0; i<NUMROWS; i++) // outer loop for(i=0; i<NUMROWS; i++) // outer loop { cout<<endl; cout<<endl; for ( j=0; j<NUMCOLS; j++) // inner loop for ( j=0; j<NUMCOLS; j++) // inner loop //{ //{ //val [I] [j] = val [i] [j] * 10; for processing of 2-D arrays //val [I] [j] = val [i] [j] * 10; for processing of 2-D arrays cout<<setw(5)<<val[i][j]; cout<<setw(5)<<val[i][j]; // } // } } cout<<endl; cout<<endl; return 0; return 0; }
10
Larger Dimension arrays ► Although arrays with more than two dimensions are not commonly used, C++ does allow any number of dimensions to be declared. ► eg: int response[4][10][6]; first element [0][0][0] first element [0][0][0] last element [3][9][5] last element [3][9][5]
11
Conceptual Understanding of larger dimensional arrays ► Conceptually, a three dimensional array can be viewed as a book of data tables. Using this visualization, the first index can be thought of as the location of the desired row in a table, the second index value as the desired column, and the third index value, as the page number of the selected table. Similarly an array of any dimension can be declared. ► eg: Fourth dimension is used to declare a desired book on the shelf. Fifth selected shelf in the book case. And son on…….
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.