1 / 18

Class 4 Honors

Class 4 Honors. Objectives. Initialize a two-dimensional array Find the minimum and maximum values in a two-dimensional array Sum the rows and columns of a two dimensional array. 8. 5. 7. 9. 6. 3. Two-dimensional array initialization. int main (void)

Download Presentation

Class 4 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 4 Honors

  2. Objectives • Initialize a two-dimensional array • Find the minimum and maximum values in a two-dimensional array • Sum the rows and columns of a two dimensional array

  3. 8 5 7 9 6 3 Two-dimensional array initialization int main (void) { int hours [3] [2] = {{8,5}, {7,9}, {6,3}}; // or int hours [3][2] = {8,5,7,9,6,3};

  4. 8 8 0 7 7 9 9 6 6 0 0 0 Two-dimensional array initialization int main (void) { int hours [3] [2] = {{8}, {7,9}, {6}}; int hours [3][2] = {8,7,9,6};

  5. 8 7 9 6 0 0 Two-dimensional array initialization int hours [3][2] = {8,7,9,6}; cout << hours[1][0]; // displays the nine

  6. int main (void) { int Table1 [3] [4] = {{1,2,3,4}, {5,8}, {9,10,11}}; int Table2 [4] [4] = { {10,30,40}, {50,60,70,80}, {90}, {130,140,150,160} }; Write a cout statement to display the 140. p. 440 cout << Table2[3][1]; Write a cout statement to display the 80. cout << Table2[1][3];

  7. void ShowArray(int [ ][ 4], int); int main (void) { int Table1 [3] [4] = {{1,2,3,4}, {5,8}, {9,10,11}}; // display each array a row at a time ShowArray(Table1,3); return 0;} p. 440 void ShowArray(int T[][4], int number) {for (int row = 0; row < number; row++) for (int col =0; col < 4; col++) cout << T[row][col];}

  8. p. 440 void ShowArray(int [ ][4], int); int main (void) { int Table2 [4] [4] = { {10,30,40}, {50,60,70,80}, {90}, {130,140,150,160} }; // display each array a row at a time ShowArray(Table2,4); return 0; } void ShowArray(int T[][4], int number) {for (int row = 0; row < number; row++) for (int col =0; col < 4; col++) cout << T[row][col];}

  9. int main (void) { int Table1 [3] [4] = {{1,2,3,4}, {5,6,7,8}, {9,10,11,12}}; int Table2 [4] [4] = { {10,20,30,40}, {50,60,70,80}, {90,100,110,120}, {130,140,150,160} }; SumArray(Table1,3); } void SumArray(int Array[ ][4], int Rows) { int total=0; for (int X=0; X <Rows; X++) for ( int Y=0; Y <4; Y++) total+=Array[X][Y] ; cout << “the sum is” << total; }

  10. int main (void) { int Table1 [3] [4] = {{1,2,3,4}, {5,6,7,8}, {9,10,11,12}}; int Table2 [4] [4] = { {10,20,30,40}, {50,60,70,80}, {90,100,110,120}, {130,140,150,160} }; SumArray(Table2,4); } void SumArray(int Array[ ][4], int Rows) { for (int X=0; X <Rows; X++) for ( int Y=0; Y <4; Y++) total+=Array[X][Y] ; }

  11. int main (void) { int Table1 [3] [4] = {{1,2,3,4}, {5,6,7,8}, {9,10,11,12}}; int Table2 [4] [4] = { {10,20,30,40}, {50,60,70,80}, {90,100,110,120}, {130,140,150,160} }; InputData(Table2,4); } void InputData(int Array[ ][4], int Rows) { for (int X=0; X <Rows; X++) { cout << “enter 4 values for row” << (X +1); for ( int Y=0; Y <4; Y++) cin >> Array[X]Y]; }

  12. These are some of the basic functions using a two-dimensional array; we displayed all the contents, totaled all the contents and entered data into each element. The last slide is a practice exercise.

  13. Assume we have an two-dimensional array for the storage of votes for three candidates in five different precincts. We make use of define constants to make our program a little more modular.#define CANDIDATES 3#define PRECINCTS 5void main(){ int votes[CANDITATES][PRECINCTS]; }

  14. Write the functions to:1. Enter all the votes into the array. 2. Find and display the total number of votes cast for each candidate3. Find and display the total number of votes cast in each precinct.4. Find the winner of the election.

  15. int main() { char Months[12][10] ={“January”, “February”, “March”, “April”, “May” “June”, “July”, “August”, “September”, “October”, “November”, “December”}; p. 444Program 7-20 cout << Months[9];

  16. int Days[12] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31, 31}; for (int Count = 0; Count < 12; Count++) { cout << Months[Count] << “has”; cout << Days[Count] << “days.\n”; }

  17. Q & A

  18. Conclusion of Class 4

More Related