210 likes | 311 Views
Class 7 Honors. Objectives. Understand the close relationship between two-dimensional arrays and pointers Manipulate a two-dimensional array using pointers. One Dimensional Array. int main (void) ( short zippo. 9,. [2] =. { 5,. 8,. 12,. 4,. 11,. 3,. 2}. [4]. 5. 9. 8. 12. 4.
E N D
Objectives • Understand the close relationship between two-dimensional arrays and pointers • Manipulate a two-dimensional array using pointers
One Dimensional Array int main (void) ( short zippo 9, [2] = { 5, 8, 12, 4, 11, 3, 2} [4] 5 9 8 12 4 11 3 2 3512 3514 3516 3518
5 9 8 12 4 11 3 2 Two-Dimensional Array int main (void) ( short zippo [2] = { 5, 8, 12, 4, 11, 3, 2} [4] 9, What is the value of zippo [0][1] 9 zippo [2][0] 4 zippo [3][1] 2 zippo [3][2] ?
5 9 8 12 4 11 3 2 zippo [0] zippo [1] zippo [2] zippo [3]
5 9 3512 3512 3514 zippo [0] 8 12 3516 3518 3516 4 11 zippo [1] 3520 3522 3520 3 2 3524 3526 zippo [2] Display the 4 3524 zippo [3] cout << zippo [2] [0]; cout<< *zippo [2];
5 9 3512 3512 3514 zippo [0] 8 12 3516 3518 3516 4 11 zippo [1] 3520 3522 3520 3 2 3524 3526 zippo [2] Display the 11 3524 zippo [3] cout << zippo [2] [1]; cout<< *(zippo [2]+1);
5 9 8 12 4 11 3 2 3512 3514 3516 3518 3520 3522 3524 3528 3512 3516 3520 3524 zippo [0] zippo [1] zippo [2] zippo [3] zippo [1] + 4 3524 *(zippo [2] + 2) 3 *(zippo [0] + 1) 9
Using a Two-Dimensional Array int main () { short junk [3] [4] = {{2,4,58}, {3,5,69}, {12,10,8,6}}; int i, j; // display junk a row at a time for (i = 0; i < 3; i++) { for(j = 0; j < 4; j++) cout << junk [i] [j]; cout << endl; return 0; }
Using a Two-Dimensional Array int main () { short junk [3] [4] = {{2,4,58}, {3,5,69}, {12,10,8,6}}; int i, j; // display junk a row at a time using pointers for (i = 0; i < 3; i++) { for(j = 0; j < 4; j++) cout << *(junk[i]+ j); cout << endl; } return 0; }
Using a Two-Dimensional Array int main () { short junk [3] [4] = {{2,4,58}, {3,5,69}, {12,10,8,6}}; int i, j; //print junk a row at a time using pointers for (i = 0; i < 3; i++) { for(j = 0; j < 4; j++) cout << *(* (junk +i)+ j); cout << endl; } return 0; }
Using a Two-Dimensional Array int main () { short junk [3] [4] = {{2,4,58}, {3,5,69}, {12,10,8,6}}; multiply(junk,3); // multiply all 12 elements by 2 using pointers } void multiply(short array[ ][4],int rows) { for (int i = 0; i < rows; i++) { for( int j = 0; j < 4; j++) *(* (junk +i)+ j) *= 2; cout << endl; } }
Using a Two-Dimensional Array int main () { short junk [3] [4] = {{2,4,58}, {3,5,69}, {12,10,8,6}}; multiply(junk,3); // multiply all 12 elements by 2 using pointers } void multiply(short (*array)[4],int rows) { for (int i = 0; i < rows; i++) { for( int j = 0; j < 4; j++) *( junk[i] + j) *= 2; cout << endl; } }
Using a Two-Dimensional Array int main () { short junk [3] [4] = {{2,4,58}, {3,5,69}, {12,10,8,6}}; multiply(junk[0],12); // multiply all 12 elements by 2 using pointers } void multiply(short p[ ],int size) // OR void multiply(short *p, int size) { for (int i = 0; i < size; i++) {*p *= 2; p++; } }
void main(void) { char Months[12][10] ={“January”, “February”, “March”, “April”, “May” “June”, “July”, “August”, “September”, “October”, “November”, “December”}; cout << Months[9]; cout << *Months[1]; cout << (Months[2] + 2);
void main(void) { char *Months[12] = {“January”, “February”, “March”, “April”, “May”, “June”, “July”, “August”, “September”, cout << Months[9]; “October”, // would display October “November”, “December”};
WORKING WITH STRINGS char Scientists[4][9] = { “Galileo”, “Kepler”, “Newton”, “Einstein” }; CAUTIONS: MISUSE OF POINTERS TO STRINGS: 1. if ( Scientists[1] = = “Kepler”) Left side of = = Scientists[1] is the address of the the first character in the second row P.443 Figure 7-18
WORKING WITH STRINGS char Scientists[4][9] = { “Galileo”, “Kepler”, “Newton”, “Einstein” }; CAUTIONS: MISUSE OF POINTERS TO STRINGS: 1. if ( Scientists[1] = = “Kepler”) Rightsideof = = P.443 Figure 7-18 Address of “Kepler” is stored somewhere in fixed memory
WORKING WITH STRINGS char Scientists[4][9] = { “Galileo”, “Kepler”, “Newton”, “Einstein” }; CAUTIONS: MISUSE OF POINTERS TO STRINGS: 1. if ( Scientists[1] = = “Kepler”) P.443 Figure 7-18 CORRECTION: if(strcmp(Scientists[1],”Kepler”) == 0)