Lec 13 - Lec 13 Oct 21, 02 Array Initialization in the...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Lec 13 Oct 21, 02 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, 16, 14, 23, 19, 15, 18, 21, 5 }; 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.*/ 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. Array Initialization in the declaration statement contd.. char codes[6] = { `s', `a', `m', `p', `l', `e' }; char codes = { `s', `a', `m', `p', `l', `e' }; String initialization: char codes = "sample"; // \o below is called null character s a m P l e \o Declaring and Processing 2D arrays A two dimensional array, which is sometimes referred to as a table, consists of both rows and columns of elements. Eg: 8 16 9 52 315 27 6 14 25 2 10 is called a two dimensional array of integers. This array consists of 3 rows and four columns. Declaration of the above array is: int val[3][4] ; // array named value with 3 rows and 4 columns Other examples float volts[10][5]; /* 2-D array named volts with 10 rows and 5 columns */ int code[6][26]; /* 2-D array named code with 6 rows and 26 columns */ Accessing 2D arrays The term val[1][3] uniquely identifies the element in row 1, column 3. watts = val[2][3]; val[0][0] = 62; newnum = 4 * (val[1][0] 5); Sum=val[0][0]+val[0][1]+val[0][2]+val[0][3]; Initilization in declaration statement: 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} }; (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... Example 1 #include<iomanip.h> int main() { const int NUMROWS = 3; const int NUMCOLS = 4; int i,j; for(i=0; i<NUMROWS; i++) // outer loop { cout<<endl; for ( j=0; j<NUMCOLS; j++) // inner loop //{ //val [I] [j] = val [i] [j] * 10; for processing of 2-D arrays cout<<setw(5)<<val[i][j]; // } } cout<<endl; return 0; } #include<iostream.h> 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] last element [3][9][5] 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....... ...
View Full Document

This document was uploaded on 07/06/2011.

Ask a homework question - tutors are online