lec 13 oct 21, 02. array initialization in the declaration statement ► int temp[5] = {98, 87, 92,...

11
Lec 13 Lec 13 Oct 21, 02 Oct 21, 02

Upload: brian-webster

Post on 18-Jan-2018

215 views

Category:

Documents


0 download

DESCRIPTION

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.

TRANSCRIPT

Page 1: 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’,

Lec 13Lec 13Oct 21, 02Oct 21, 02

Page 2: 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’,

Array Initialization in the declaration statementArray Initialization in the declaration statement

► int temp[5] = {98, 87, 92, 79,85};int temp[5] = {98, 87, 92, 79,85};► char codes[6] = { ‘s’, ’a’, ‘m’ , ‘p’, ‘l’, ‘e’ };char codes[6] = { ‘s’, ’a’, ‘m’ , ‘p’, ‘l’, ‘e’ };► double slopes[3] = {10.96, 6.43, 2.58};double slopes[3] = {10.96, 6.43, 2.58};

// multi line initialization below.// multi line initialization below.► int gallons[20] = {19, 16, 14, 19, 20, 18,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 };

Page 3: 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’,

Array Initialization in the declaration statement Array Initialization in the declaration statement contd..contd..

► float length[7] = {7.8, 6.4, 4.9, 11.2};float length[7] = {7.8, 6.4, 4.9, 11.2}; / * In the above declaration, number of elements / * In the above declaration, number of elements

listed in curly brackets is not same as numbers of listed in curly brackets is not same as numbers of elements listed in square brackets. Therefore, elements listed in square brackets. Therefore, length[0], length[1], length[2], and length[3] are length[0], length[1], length[2], and length[3] are initilized with listed values. The other array initilized with listed values. The other array elements will be initialized to zero.*/elements will be initialized to zero.*/

► int gallons[] = {16, 12, 10, 14, 11};int gallons[] = {16, 12, 10, 14, 11}; /* A unique feature of initializers is that the size /* A unique feature of initializers is that the size

of an array may be omitted when initializing of an array may be omitted when initializing values are included in the declarationvalues are included in the declaration statement. statement.

Page 4: 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’,

Array Initialization in the declaration Array Initialization in the declaration statement contd..statement contd..

► char codes[6] = { ‘s’, ‘a’, ‘m’, ‘p’, ‘l’, char codes[6] = { ‘s’, ‘a’, ‘m’, ‘p’, ‘l’, ‘e’ };‘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

ss aa mm PP ll ee \o\o

Page 5: 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’,

Declaring and Processing 2-D arraysDeclaring and Processing 2-D arrays

► A two – dimensional array, which is sometimes referred A two – dimensional array, which is sometimes referred to as a table, consists of both rows and columns of to as a table, consists of both rows and columns of elements.elements.

► Eg: 8Eg: 8 1616 99 5252 33 1515 2727 66 1414 2525 22 10 10 is called a two – dimensional array of integers. This array is called a two – dimensional array of integers. This array

consists of 3 rows and four columns.consists of 3 rows and four columns.

Declaration of the above array is:Declaration of the above array is: int val[3][4] ;int val[3][4] ; // array named value with 3 rows and 4 // array named value with 3 rows and 4

columnscolumns

Page 6: 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’,

Other examplesOther examples

► float volts[10][5];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];int code[6][26]; /* 2-D array named code with 6 rows and 26 /* 2-D array named code with 6 rows and 26

columns */columns */

Page 7: 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’,

Accessing 2-D arraysAccessing 2-D arrays

► The term val[1][3] uniquely identifies the The term val[1][3] uniquely identifies the element in row 1, column 3.element in row 1, column 3.

► watts = val[2][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]Sum=val[0][0]+val[0][1]+val[0][2]+val[0]

[3];[3];

Page 8: 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’,

Initilization in declaration statement:Initilization in declaration statement:

► int val[3][4] = { 8, 16, 9, 52,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] (or) int val[3][4]

={8,16,9,52,3,15,27,6,14,25,2,10};={8,16,9,52,3,15,27,6,14,25,2,10};

// Note : 2-D array initialization is done row-wise. // Note : 2-D array initialization is done row-wise. First, the elements of the first row are initialized, First, the elements of the first row are initialized, then second row and so on…then second row and so on…

Page 9: 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’,

Example 1Example 1► #include<iostream.h>#include<iostream.h>► #include<iomanip.h>#include<iomanip.h> 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 loopfor(i=0; i<NUMROWS; i++) // outer loop {{ cout<<endl;cout<<endl; for ( j=0; j<NUMCOLS; j++) // inner loopfor ( 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; }}

Page 10: 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’,

Larger Dimension arraysLarger Dimension arrays

► Although arrays with more than two Although arrays with more than two dimensions are not commonly used, C++ does dimensions are not commonly used, C++ does allow any number of dimensions to be allow any number of dimensions to be declared.declared.

► eg: int response[4][10][6];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]

Page 11: 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’,

Conceptual Understanding of larger Conceptual Understanding of larger dimensional arraysdimensional arrays

► Conceptually, a three dimensional array can Conceptually, a three dimensional array can be viewed as a book of data tables. Using this be viewed as a book of data tables. Using this visualization, the first index can be thought of visualization, the first index can be thought of as the location of the desired row in a table, as the location of the desired row in a table, the second index value as the desired the second index value as the desired column, and the third index value, as the column, and the third index value, as the page number of the selected table. Similarly page number of the selected table. Similarly an array of any dimension can be declared.an array of any dimension can be declared.

► eg: Fourth dimension is used to declare a eg: Fourth dimension is used to declare a desired book on the shelf. Fifth selected shelf desired book on the shelf. Fifth selected shelf in the book case. And son on…….in the book case. And son on…….