Class 4 Honors

18
STARTING OUT WITH Judy Judy Etchison Etchison Class 4 Class 4 Honors Honors

description

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) - PowerPoint PPT Presentation

Transcript of Class 4 Honors

Page 1: Class  4 Honors

STARTING OUT WITH

STARTING OUT WITH

Judy EtchisonJudy Etchison

Class 4 HonorsClass 4 Honors

Page 2: Class  4 Honors

2

ObjectivesObjectives

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

Page 3: Class  4 Honors

3

Two-dimensional array initializationTwo-dimensional array initialization

58

7 9

6 3

int main (void){ int hours [3] [2] = {{8,5}, {7,9}, {6,3}};// or int hours [3][2] = {8,5,7,9,6,3};

Page 4: Class  4 Honors

4

Two-dimensional array initializationTwo-dimensional array initialization

08

7 9

6 0

int main (void){ int hours [3] [2] = {{8}, {7,9}, {6}};

int hours [3][2] = {8,7,9,6};

78

9 6

0 0

Page 5: Class  4 Honors

5

Two-dimensional array initializationTwo-dimensional array initialization

78

9 6

0 0

int hours [3][2] = {8,7,9,6};

cout << hours[1][0]; // displays the nine

Page 6: Class  4 Honors

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.

cout << Table2[3][1];

Write a cout statement to display the 80.

cout << Table2[1][3];

p. 440

Page 7: Class  4 Honors

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 timeShowArray(Table1,3); 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];}

p. 440

Page 8: Class  4 Honors

8

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 timeShowArray(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];}

p. 440

Page 9: Class  4 Honors

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

Page 10: Class  4 Honors

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

Page 11: Class  4 Honors

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

Page 12: Class  4 Honors

12

These are some of the basic functions using a These are some of the basic functions using a

two-dimensional array; we displayed all the two-dimensional array; we displayed all the

contents, totaled all the contents and entered contents, totaled all the contents and entered

data into each element. The last slide is a data into each element. The last slide is a

practice exercise.practice exercise.

Page 13: Class  4 Honors

13

Assume we have an two-dimensional array Assume we have an two-dimensional array

for the storage of votes for three candidates in for the storage of votes for three candidates in

five different precincts. We make use of five different precincts. We make use of

define constants to make our program a little define constants to make our program a little

more modular.more modular.

#define CANDIDATES 3#define CANDIDATES 3

#define PRECINCTS 5#define PRECINCTS 5

void main()void main()

{ int votes[CANDITATES][PRECINCTS];{ int votes[CANDITATES][PRECINCTS];

} }

Page 14: Class  4 Honors

14

Write the functions to:Write the functions to:

1. Enter all the votes into the array. 1. Enter all the votes into the array.

2. Find and display the total number of votes2. Find and display the total number of votes

cast for each candidate cast for each candidate

3. Find and display the total number of votes 3. Find and display the total number of votes

cast in each precinct.cast in each precinct.

4. Find the winner of the election.4. Find the winner of the election.

Page 15: Class  4 Honors

15

int main(){ char Months[12][10] ={“January”, “February”, “March”,

“April”, “May”

“June”,“July”,“August”,“September”,“October”,“November”,“December”};cout << Months[9];

p. 444Program 7-20

Page 16: Class  4 Honors

16

for (int Count = 0; Count < 12; Count++)

{ cout << Months[Count] << “has”;

cout << Days[Count] << “days.\n”;

}

int Days[12] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31, 31};

Page 17: Class  4 Honors

17

Q & A

Page 18: Class  4 Honors

STARTING OUT WITH

STARTING OUT WITH

Judy EtchisonJudy Etchison

Conclusion of Conclusion of Class 4Class 4