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

Slides:



Advertisements
Similar presentations
1 CS 105 Lecture 11 Arrays Version of Wed, Apr 6, 2011, 6:20 pm.
Advertisements

Overview Reference parameters Documenting functions A game of craps. Design, code, test and document.
Programming Functions: Passing Parameters by Reference.
Dr. Yang, Qingxiong (with slides borrowed from Dr. Yuen, Joe) LT4: Control Flow - Loop CS2311 Computer Programming.
Dr. Yang, QingXiong (with slides borrowed from Dr. Yuen, Joe) LT5: Array (1D and 2D) CS2311 Computer Programming.
Chapter 5 Functions.
Arrays Programming COMP102 Prog. Fundamentals I: Arrays / Slide 2 Arrays l An array is a collection of data elements that are of the same type (e.g.,
1 9/29/06CS150 Introduction to Computer Science 1 Loops Section Page 255.
#include using namespace std; void main() { int a[3]={10,11,23}; for(int i=0;i
CS150 Introduction to Computer Science 1
Computer Science 1620 Multi-Dimensional Arrays. we used arrays to store a set of data of the same type e.g. store the assignment grades for a particular.
1 Arrays In many cases we need a group of nearly identical variables. Example: make one variable for the grade of each student in the class This results.
Chapter 8. 2 Objectives You should be able to describe: One-Dimensional Arrays Array Initialization Arrays as Arguments Two-Dimensional Arrays Common.
Chapter 9 Introduction to Arrays
Lesson 7 Arrays CS 1 Lesson 7 -- John Cole1. Arrays Hold Multiple Values Array: variable that can store multiple values of the same type Values are stored.
1 Arrays & functions Each element of an array acts just like an ordinary variable: Like any ordinary variable, you can pass a single array element to a.
What is the out put #include using namespace std; void main() { int i; for(i=1;i
Arrays.
CS161 Topic #14 1 Today in CS161 Lecture #14 Practicing! Writing Programs to Practice Write a program that counts the number of vowels in a sentence, ended.
Multi-Dimensional Arrays in Java "If debugging is the process of removing software bugs, then programming must be the process of putting them in." -- Edsger.
CS Class 07 Topics –  When software goes wrong  Count controlled loops  Sentential controlled loops  putting it all together Announcements.
HANGMAN Created By: Will Matthau & Christian Stumpf.
CS161 Topic #15 1 Today in CS161 Lecture #15 Practicing! Writing Programs to Practice Write a game program (1 player) of Mad Math Reuse the functions to.
Computer Programming for Engineers. Outline Tic-Tac-Toe (O-X Game) Drawing 3x3 grid Receiving the inputs Checking for a winner Taking turns between.
CS161 Week #8 1 Today in CS161 Week #8 Practicing! Writing Programs to Practice Write a program that counts the number of vowels in a sentence, ended by.
© Copyright 2013 by Pearson Education, Inc. All Rights Reserved. 1 Chapter 8 Multidimensional Arrays.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 7 Clicker Questions September 22, 2009.
GAME102 - INTRO WHILE LOOPS G. MacKay. Fundamental Control Structures  STRAIGHT LINE  CONDITIONAL  LOOPS.
Scope When we create variables and functions, they are limited in where they are visible and where they can be referenced For the most part, the identifiers.
CS Class 08 Today  Exercises  Nested loops  for statement  Built-in functions Announcements  Homework #3, group solution to in-class.
For loops, nested loops and scopes Jordi Cortadella Department of Computer Science.
Section 5 - Arrays. Problem solving often requires information be viewed as a “list” List may be one-dimensional or multidimensional List is implemented.
A First Book of C++: From Here To There, Third Edition2 Objectives You should be able to describe: One-Dimensional Arrays Array Initialization Arrays.
1 One Dimensional Arrays Chapter 11 2 "All students to receive arrays!" reports Dr. Austin. Declaring arrays scores :
1 Arrays and Strings Lecture: Design Problem l Consider a program to calculate class average Why?? ?
1 CS161 Introduction to Computer Science Topic #8.
Review for Final Exam. Contents 5 questions (20 points each) + 1 bonus question (20 points) – Basic concepts in Chapters 1-4 – Chapters 5-9 – Bonus: Chapter.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 6 Arrays.
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
CS Class 19 Today  Practice with classes Announcements  Turn in algorithm for Project 5 in class today  Project 5 due 11/11 by midnight – .
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter Array Basics.
1 Chapter 12 Arrays. 2 C++ Data Types structured array struct union class address pointer reference simple integral enum char short int long bool floating.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 15. Dictionaries (1): A Key Table Class.
Think First, Code Second Understand the problem Work out step by step procedure for solving the problem (algorithm) top down design and stepwise refinement.
CPS120 Introduction to Computer Science Exam Review Lecture 18.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 6 Multidimensional.
SEQUENTIAL AND OBJECT ORIENTED PROGRAMMING Arrays.
1 Lecture 4: Part1 Arrays Introduction Arrays  Structures of related data items  Static entity (same size throughout program)
CS Class 04 Topics  Selection statement – IF  Expressions  More practice writing simple C++ programs Announcements  Read pages for next.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter 7 Pointers and C-Strings.
Lecture 9 – Array (Part 2) FTMK, UTeM – Sem /2014.
Chapter 9 Introduction to Arrays Fundamentals of Java.
1 C++ Data Types structured array struct union class address pointer reference simple integral enum char short int long bool floating float double long.
Introduction to Programming Lecture 12. Today’s Lecture Includes Strings ( character arrays ) Strings ( character arrays ) Algorithms using arrays Algorithms.
1 Multidimensional Arrays Chapter 13 2 The plural of mongoose starts with a "p" Initializing a multidimensional array Processing by.
Objectives You should be able to describe: One-Dimensional Arrays
LESSON 8: INTRODUCTION TO ARRAYS. Lesson 8: Introduction To Arrays Objectives: Write programs that handle collections of similar items. Declare array.
For loops, nested loops and scopes
Lesson 5 Functions I A function is a small program which accomplishes a specific task. For example, we invoke (call) the function, sqrt(x), in the library.
Multi-dimensional Array
Arrays … The Sequel Applications and Extensions
Chapter 5 Function Basics
Review for Final Exam.
CS150 Introduction to Computer Science 1
Multidimensional Arrays
Multidimensional Arrays
Review for Final Exam.
CS150 Introduction to Computer Science 1
Fundamental Programming
Arrays Prepared By Paritosh Srivastava PGT (CS) KV NHPC Banbasa.
Presentation transcript:

CS Class 15 Today  More practice with arrays  Introduction to Multi-dimensional arrays Announcements  Programming project #4 due 10/23 by midnight ( .cpp to  Exam 2 is Tuesday, 10/28 during class

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)

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

Our algorithm Tasks:  Keep track of the number of occurrences of each number 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

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

The functions void GenNumbers(int count[101]) { int value, seed = time(0); srand(seed); for (int a=0; a< ; 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; }

Multi-dimensional Arrays Declaring two dimensional arrays int 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 B E D CA

Multi-dimensional Arrays Consider tic-tac-toe  Mark each position with a character ‘X’, ‘O’, space  Need two dimensions Logical Image Actual Physical Layout

Three more rules for arrays Must put each subscript in its own set of brackets x = 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 them int 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} };

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

Code for tic-tac-toe (part one) #include 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

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

Class Exercises Code for tic-tac-toe is out on our class web page  Look under Sample Programs for Oct. 21.  It is missing the code for CheckWinner( ). Add the code for CheckWinner.  Three ‘X’ in a row, return 1  Three ‘O’ in a row, return 2  No winner and not full, return 0  All full, return 3

Ugly Code for CheckWinner int 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; }

Good Code for CheckWinner int 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; }

Better Code for CheckWinner int 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; }

End of Class 15