180 likes | 261 Views
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)
E N D
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) { int hours [3] [2] = {{8,5}, {7,9}, {6,3}}; // or int hours [3][2] = {8,5,7,9,6,3};
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};
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
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];
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];}
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];}
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; }
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] ; }
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]; }
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.
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]; }
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.
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];
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”; }