Programming in C++ Yet Another Example o Declare an array called name which will hold up to 10 individual char values number of elements in the array char name[10: / declaration allocates memory Base Address 6000600160026003600460056006600760086009 name[o] name[1] name[2 name [3] name[4] name[9
11 Yet Another Example ❖Declare an array called name which will hold up to 10 individual char values. char name[10]; // declaration allocates memory number of elements in the array name[0] name[1] name[2] name[3] name[4] . . . . . name[9] 6000 6001 6002 6003 6004 6005 6006 6007 6008 6009 Base Address
Programming in C++ Assigning Values to Individual Array Elements float temps[ 5] // allocates memory for array int m= 4 temps[2]=98.6 temps[31=101.2 temps[0]=99.4 temps[ m ]= temps[ 31/2.0: temps[ 11=temps[31-1.2, / /what value is assigned? 7000 7004 7008 7012 7016 994 ? 9861012 50.6 tem ps[o temps[1] temps[2 temps[3 temps[41 12
12 Assigning Values to Individual Array Elements float temps[ 5 ] ; // allocates memory for array int m = 4 ; temps[ 2 ] = 98.6 ; temps[ 3 ] = 101.2 ; temps[ 0 ] = 99.4 ; temps[ m ] = temps[ 3 ] / 2.0 ; temps[ 1 ] = temps[ 3 ] - 1.2 ; // what value is assigned? temps[0] temps[1] temps[2] temps[3] temps[4] 7000 7004 7008 7012 7016 99.4 ? 98.6 101.2 50.6
Programming in C++ What values are assigned? float temps[ 5]; //allocates memory for array int m for(m=0;m<5;m++) temps[ n]=100.0+m*0.2 7000 7004 7008 7012 7016 tem ps[o temps[1] temps[2 temps[3 temps[41 13
13 What values are assigned? float temps[ 5 ] ; // allocates memory for array int m ; for (m = 0; m < 5; m++) { temps[ m ] = 100.0 + m * 0.2 ; } temps[0] temps[1] temps[2] temps[3] temps[4] 7000 7004 7008 7012 7016 ? ? ? ? ?
Programming in C++ Now what values are printed? float temps[ 5]; //allocates memory for array m for(m=4;m>=0;m-) cout < temps[ m] < end 7000 7004 7008 7012 7016 100010021004100.61008 tem ps[o temps[1] temps[2 temps[3 temps[41 14
14 Now what values are printed? float temps[ 5 ] ; // allocates memory for array int m ; . . . . . for (m = 4; m >= 0; m-- ) { cout << temps[ m ] << endl ; } temps[0] temps[1] temps[2] temps[3] temps[4] 7000 7004 7008 7012 7016 100.0 100.2 100.4 100.6 100.8
Programming in C++ Variable Subscripts float temps[51: / allocates memory for array int m= 3 What is temps[ m +11? What is temps[ m]+1? 7000 7004 7008 7012 7016 100.0100.21004100.6100.8 tem ps[o temps[1] temps [2 temps[3 temps[4] 15
15 Variable Subscripts float temps[ 5 ] ; // allocates memory for array int m = 3 ; . . . . . . What is temps[ m + 1] ? What is temps[ m ] + 1 ? temps[0] temps[1] temps[2] temps[3] temps[4] 7000 7004 7008 7012 7016 100.0 100.2 100.4 100.6 100.8