1 / 21

Class 7 Honors

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.

Download Presentation

Class 7 Honors

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Class 7 Honors

  2. Objectives • Understand the close relationship between two-dimensional arrays and pointers • Manipulate a two-dimensional array using pointers

  3. 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

  4. 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. 5 9 8 12 4 11 3 2 zippo [0] zippo [1] zippo [2] zippo [3]

  6. 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];

  7. 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);

  8. 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

  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; }

  10. 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; }

  11. 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; }

  12. 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; } }

  13. 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; } }

  14. 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++; } }

  15. 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);

  16. void main(void) { char *Months[12] = {“January”, “February”, “March”, “April”, “May”, “June”, “July”, “August”, “September”, cout << Months[9]; “October”, // would display October “November”, “December”};

  17. 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

  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

  19. 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)

  20. Q & A

  21. Conclusion of Class 7

More Related