17
CS114-009 Class 15 Today More practice with arrays Introduction to Multi-dimensional arrays Announcements Programming project #4 due 10/23 by midnight (e-mail .cpp to [email protected]) Exam 2 is Tuesday, 10/28 during class

CS114-009 Class 15 Today More practice with arrays Introduction to Multi-dimensional arrays Announcements Programming project #4 due 10/23 by midnight

Embed Size (px)

DESCRIPTION

Practice with arrays Problem: Generate 1,000,000 random numbers between 0 and 100. Find and print the number(s) that occurred the most often Tasks:  Generate 1,000,000 random numbers  Add to the appropriate counters 101 different counters Use an array for this  Find the counter(s) with the largest value  Print out those counters

Citation preview

Page 1: CS114-009 Class 15 Today  More practice with arrays  Introduction to Multi-dimensional arrays Announcements  Programming project #4 due 10/23 by midnight

CS114-009 Class 15 Today

More practice with arrays Introduction to Multi-dimensional arrays

Announcements Programming project #4 due 10/23 by midnight (e-

mail .cpp to [email protected]) Exam 2 is Tuesday, 10/28 during class

Page 2: CS114-009 Class 15 Today  More practice with arrays  Introduction to Multi-dimensional arrays Announcements  Programming project #4 due 10/23 by midnight

Programming project #4 Make sure your file “ballot.txt” is in the correct

folder If your .net project is called CS114, go to the

folder CS114, then click on CS114 again to go to the next folder. If you a Debug folder, you are in the correct location (do not go to the Debug folder)

Page 3: CS114-009 Class 15 Today  More practice with arrays  Introduction to Multi-dimensional arrays Announcements  Programming project #4 due 10/23 by midnight

Practice with arrays Problem: Generate

1,000,000 random numbers between 0 and 100. Find and print the number(s) that occurred the most often

Tasks: Generate 1,000,000

random numbers Add to the appropriate

counters 101 different counters Use an array for this

Find the counter(s) with the largest value

Print out those counters

Page 4: CS114-009 Class 15 Today  More practice with arrays  Introduction to Multi-dimensional arrays Announcements  Programming project #4 due 10/23 by midnight

Our algorithm Tasks:

Keep track of the number of occurrences of each number 0..100

101 different counters Use an array for this

Generate 1,000,000 random numbers

Based on the number, increment appropriate counter

Find the maximum value of all counters

Print out the number(s) with the largest counter

The array counters keeps track of how many times we’ve seen each item counters[0] contains the

number of occurrences of 0 counters[1] contains the

number of occurrences of 1 counters[2] contains the

number of occurrences of 2 … counters[100] contains the

number of occurrences of 100

All counters initialized to 0

Page 5: CS114-009 Class 15 Today  More practice with arrays  Introduction to Multi-dimensional arrays Announcements  Programming project #4 due 10/23 by midnight

More on our algorithm Implement via main routine

and three functions Function One:

Generates 1,000,000 random numbers between 0 and 100

Based on the number generated, increment a counter

Function Two Figures out which element in

the array of counters is the largest

Function Three Prints all numbers where

their counter matches the max counter

Write the functions!

void GenNumbers (int [ ]);int Largest (int [ ]);void PrintNums (int [ ], int);

int main ( ) {int counters[101];for (int a=0; a<101; a++)

counters[a] = 0;GenNumbers (counters);int max=Largest(counters);PrintNums (counters, max);return 0;

}

Page 6: CS114-009 Class 15 Today  More practice with arrays  Introduction to Multi-dimensional arrays Announcements  Programming project #4 due 10/23 by midnight

The functionsvoid GenNumbers(int count[101]) {

int value, seed = time(0);srand(seed);for (int a=0; a<1000000; a++) {

value = rand() % 101;count[value] = count[value] + 1;

}}

int Largest(int counters[101]) {int max = counters[0];for (int a=1; a<101; a++)

if (counters[a] > max) max = counters[a];return max;

}

void PrintNums(int data[101], int value) {cout << "The number(s) that occurred the most : ";for (int a=0; a<101; a++)

if (data[a] == value) cout << a << " ";cout << endl;

}

Page 7: CS114-009 Class 15 Today  More practice with arrays  Introduction to Multi-dimensional arrays Announcements  Programming project #4 due 10/23 by midnight

Multi-dimensional Arrays Declaring two

dimensional arraysint arr[5][4];

First subscript is rows Second is columns

first item is arr[0][0]last item is arr[4][3]

How do you reference the locations indicated? Location A Location B Location C Location D Location E

BE

D

CA

Page 8: CS114-009 Class 15 Today  More practice with arrays  Introduction to Multi-dimensional arrays Announcements  Programming project #4 due 10/23 by midnight

Multi-dimensional Arrays Consider tic-tac-toe

Mark each position with a character

‘X’, ‘O’, space Need two dimensions

Logical Image

Actual Physical Layout

Page 9: CS114-009 Class 15 Today  More practice with arrays  Introduction to Multi-dimensional arrays Announcements  Programming project #4 due 10/23 by midnight

Three more rules for arraysMust put each subscript in its own set of bracketsx = arr[3][2];arr[3,2] = 25; // this is NOT legal syntax

When passing to a function: must provide dimensions for each (except first) subscript int funct1 (int array1[ ][10], … ); int funct2 (int array2[ ], … );

Can initialize arrays as you declare themint a[10] = {1,2,3,4,5,6,7,8,9,10};int b[10] = {1,2,3,4,5};int c[3][4] = { {1,2,3,4}, {5,6,7,8}, {9,10,11,12} };

Page 10: CS114-009 Class 15 Today  More practice with arrays  Introduction to Multi-dimensional arrays Announcements  Programming project #4 due 10/23 by midnight

Class Exercise: tic-tac-toe Want to write a game of

tic-tac-toe Main routine Function to update the

game board Function to check if

there is a winner Function to print the

board out after each move

Main routine Declare variables Initialize the board Loop (switch users)

Enter a move Update the board Print the board Check to see if winner

Print the winner

Page 11: CS114-009 Class 15 Today  More practice with arrays  Introduction to Multi-dimensional arrays Announcements  Programming project #4 due 10/23 by midnight

Code for tic-tac-toe (part one)

#include <iostream>using namespace std;void MakeMove(char game[][3], int, int, char);void PrintBoard (char game[][3]);int CheckWinner (char game[][3]);int main ( ) {

char game[3][3];for (int a=0; a<3; a++) // initialize board

for (int b=0; b<3; b++)game[a][b] = ' ';

int done = 0, count = 0, row, col;

while ( done == 0 ) { if (count%2 == 0) { cout << "Enter move (r,c) for X : "; cin >> row >> col; MakeMove (game, row, col, 'X'); } else { cout << "Enter move (r,c) for O : "; cin >> row >> col; MakeMove (game, row, col, 'O'); } done = CheckWinner (game); PrintBoard (game); count++;}if (done == 1)cout << "Winner was X" << endl;else if (done == 2)cout << "Winner was O" << endl;else // done == 3 cout << “Tie – cat’s game” << endl;return 0;

}

Function Prototypes & main

Page 12: CS114-009 Class 15 Today  More practice with arrays  Introduction to Multi-dimensional arrays Announcements  Programming project #4 due 10/23 by midnight

Code for tic-tac-toe (part two)void MakeMove (

char game[ ][3],int r, int c, char ch) {if (game[r][c] == ' ')

game[r][c] = ch;else

cout << "Illegal move,turn is lost!" << endl;

}int CheckWinner (

char game[ ][3]) {return 0, 1 (‘X’) or 2 (‘O’);

}

void PrintBoard (char game[][3]) {cout << "\t+---+---+---+“

<< endl;for (int a=0; a<3; a++) {

cout << "\t| ";for (int b=0; b<3; b++) cout << game[a][b]

<< " | ";cout << endl;cout << "\t+---+---+---+“

<< endl;}

}

Page 13: CS114-009 Class 15 Today  More practice with arrays  Introduction to Multi-dimensional arrays Announcements  Programming project #4 due 10/23 by midnight

Class Exercises Code for tic-tac-toe is out on our class web

pageLook under Sample Programs for Oct. 21. It is missing the code for CheckWinner( ).

Add the code for CheckWinner.Three ‘X’ in a row, return 1Three ‘O’ in a row, return 2No winner and not full, return 0All full, return 3

Page 14: CS114-009 Class 15 Today  More practice with arrays  Introduction to Multi-dimensional arrays Announcements  Programming project #4 due 10/23 by midnight

Ugly Code for CheckWinnerint CheckWinner (char game[ ][3]) {

if (game[0][0] == 'O' && game[0][1] == 'O' && game[0][2] == 'O') return 2;if (game[1][0] == 'O' && game[1][1] == 'O' && game[1][2] == 'O') return 2;if (game[2][0] == 'O' && game[2][1] == 'O' && game[2][2] == 'O') return 2;if (game[0][0] == 'O' && game[1][0] == 'O' && game[2][0] == 'O') return 2;if (game[0][1] == 'O' && game[1][1] == 'O' && game[2][1] == 'O') return 2;if (game[0][2] == 'O' && game[1][2] == 'O' && game[2][2] == 'O') return 2;if (game[0][0] == 'O' && game[1][1] == 'O' && game[2][2] == 'O') return 2;if (game[0][2] == 'O' && game[1][1] == 'O' && game[2][0] == 'O') return 2;

if (game[0][0] == 'X' && game[0][1] == 'X' && game[0][2] == 'X') return 1;if (game[1][0] == 'X' && game[1][1] == 'X' && game[1][2] == 'X') return 1;if (game[2][0] == 'X' && game[2][1] == 'X' && game[2][2] == 'X') return 1;if (game[0][0] == 'X' && game[1][0] == 'X' && game[2][0] == 'X') return 1;if (game[0][1] == 'X' && game[1][1] == 'X' && game[2][1] == 'X') return 1;if (game[0][2] == 'X' && game[1][2] == 'X' && game[2][2] == 'X') return 1;if (game[0][0] == 'X' && game[1][1] == 'X' && game[2][2] == 'X') return 1;if (game[0][2] == 'X' && game[1][1] == 'X' && game[2][0] == 'X') return 1;int count = 0;for (int a=0; a<3; a++)for (int b=0; b<3; b++)if (game[a][b] == 'X' || game[a][b] == 'O') count++;if (count == 9) return 3;return 0;

}

Page 15: CS114-009 Class 15 Today  More practice with arrays  Introduction to Multi-dimensional arrays Announcements  Programming project #4 due 10/23 by midnight

Good Code for CheckWinnerint CheckWinner (char game[ ][3]) {

for (int a=0; a<3; a++) { if (game[a][0] == 'O' && game[a][1] == 'O' && game[a][2] == 'O') return 2; if (game[0][a] == 'O' && game[1][a] == 'O' && game[2][a] == 'O') return 2; if (game[a][0] == ‘X' && game[a][1] == ‘X' && game[a][2] == ‘X') return 1; if (game[0][a] == ‘X' && game[1][a] == ‘X' && game[2][a] == ‘X') return 1;}if (game[0][0] == 'O' && game[1][1] == 'O' && game[2][2] == 'O') return 2;if (game[0][2] == 'O' && game[1][1] == 'O' && game[2][0] == 'O') return 2;

if (game[0][0] == 'X' && game[1][1] == 'X' && game[2][2] == 'X') return 1;if (game[0][2] == 'X' && game[1][1] == 'X' && game[2][0] == 'X') return 1;

int count = 0;for (int a=0; a<3; a++)

for (int b=0; b<3; b++)if (game[a][b] == 'X' || game[a][b] == 'O') count++;

if (count == 9) return 3;return 0;

}

Page 16: CS114-009 Class 15 Today  More practice with arrays  Introduction to Multi-dimensional arrays Announcements  Programming project #4 due 10/23 by midnight

Better Code for CheckWinnerint CheckWinner (char game[ ][3]) {

int count = 0;for (int rv=1,char c=‘X’; rv<3; rv++, c = ‘O’) { for (int a=0; a<3; a++) { if (game[a][0] == c && game[a][1] == c && game[a][2] == c) return rv; if (game[0][a] == c && game[1][a] == c && game[2][a] == c) return rv;

for (int b=0; b<3; b++) if (game[a][b] == c) count++; } if (game[0][0] == c && game[1][1] == c && game[2][2] == c) return rv; if (game[0][2] == c && game[1][1] == c && game[2][0] == c) return rv;}if (count == 9) return 3;return 0;

}

Page 17: CS114-009 Class 15 Today  More practice with arrays  Introduction to Multi-dimensional arrays Announcements  Programming project #4 due 10/23 by midnight

End of Class 15