Lecture #9 **go to Blackboard ** Review from Shackelford Reading If statements Loops. Using if statements and loops

Similar documents
Today in CS161. Lecture #12 Arrays. Learning about arrays. Examples. Graphical. Being able to store more than one item using a variable

Today in CS161. Lecture #7. Learn about. Rewrite our First Program. Create new Graphics Demos. If and else statements. Using if and else statements

Today in CS161. Week #3. Learn about. Writing our First Program. See example demo programs. Data types (char, int, float) Input and Output (cin, cout)

LAB 4.1 Relational Operators and the if Statement

Fundamentals of Programming CS-110. Lecture 2

Some of the Choices. If you want to work on your own PC with a C++ compiler, rather than being logged in remotely to the PSU systems

C++ For Science and Engineering Lecture 12

Introduction to Programming EC-105. Lecture 2

Do not turn to the next page until the start of the exam.

1- Write a single C++ statement that: A. Calculates the sum of the two integrates 11 and 12 and outputs the sum to the consol.

Computer Programming

GE U111 Engineering Problem Solving & Computation Lecture 6 February 2, 2004

o Counter and sentinel controlled loops o Formatting output o Type casting o Top-down, stepwise refinement

Flow of programming execution control is sequential unless a control structure is used to change that there are 2 general types of

CSc Introduc/on to Compu/ng. Lecture 8 Edgardo Molina Fall 2011 City College of New York

Reviewing all Topics this term

Flow of Control. bool Data Type. Flow Of Control. Expressions. C++ Control Structures < <= > >= ==!= ! &&

Review: Exam 1. Your First C++ Program. Declaration Statements. Tells the compiler. Examples of declaration statements

1) What of the following sets of values for A, B, C, and D would cause the string "one" to be printed?

Chapter 5. Conditions, Logical Expressions, and Selection Control Structures

Chapter 5 Topics. Chapter 5 Topics. Flow of Control. bool Data Type. Flow of Control. Chapter 5

The American University in Cairo Department of Computer Science & Engineering CSCI &09 Dr. KHALIL Exam-I Fall 2011

Chapter 4: Subprograms Functions for Problem Solving. Mr. Dave Clausen La Cañada High School

CSCI 111 First Midterm Exam Spring Solutions 09.05am 09.55am, Wednesday, March 14, 2018

do { statements } while (condition);

Sample Code: OUTPUT Daily Highs & Lows

CSCI 111 Midterm 1, version A Exam Fall Solutions 09.00am 09.50am, Tuesday, October 13, 2015

The C++ Language. Arizona State University 1

Exam 1. CSI 201: Computer Science 1 Fall 2018 Professors: Shaun Ramsey

The American University in Cairo Computer Science & Engineering Department CSCE Dr. KHALIL Exam II Spring 2010

Laboratory 0 Week 0 Advanced Structured Programming An Introduction to Visual Studio and C++

conditional statements

Problem Solving: Storyboards for User Interaction

Linux Tutorial #1. Introduction. Login to a remote Linux machine. Using vim to create and edit C++ programs

Ch 6. Functions. Example: function calls function

University of Michigan EECS 183: Elem. Programming Concepts Fall 2011 Exam 1: Part 1: Form 1. Professors: ML Dorf, Elliot Soloway

Basic program The following is a basic program in C++; Basic C++ Source Code Compiler Object Code Linker (with libraries) Executable

7/21/10 MOTIVATION. Did you see Inception this weekend? COMP 10 EXPLORING COMPUTER SCIENCE. Lecture 6 Arrays

Computer Programming. Basic Control Flow - Loops. Adapted from C++ for Everyone and Big C++ by Cay Horstmann, John Wiley & Sons

THE INTEGER DATA TYPES. Laura Marik Spring 2012 C++ Course Notes (Provided by Jason Minski)

Chapter 2. C++ Basics. Copyright 2014 Pearson Addison-Wesley. All rights reserved.

Solving a 2D Maze. const int WIDTH = 10; const int HEIGHT = 10;

C++ Programming: From Problem Analysis to Program Design, Third Edition

CSC 126 FINAL EXAMINATION Spring Total Possible TOTAL 100

Superior University. Department of Electrical Engineering CS-115. Computing Fundamentals. Experiment No.7. User Defined Functions II

Integer Data Types. Data Type. Data Types. int, short int, long int

Intro to Programming & C Why Program? 1.2 Computer Systems: Hardware and Software. Why Learn to Program?

CS 141, Introduction to Computer Science Fall Midterm Exam

Review Questions for Final Exam

OBJECT ORIENTED PROGRAMMING

Reading from and Writing to Files. Files (3.12) Steps to Using Files. Section 3.12 & 13.1 & Data stored in variables is temporary

Pick a number. Conditionals. Boolean Logic Relational Expressions Logical Operators Numerical Representation Binary. CS Conditionals 1

Laboratory 7. Programming Workshop 2 (CSCI 1061U) Faisal Qureshi.

Operations. Making Things Happen

Chapter 2. C++ Basics. Copyright 2014 Pearson Addison-Wesley. All rights reserved.

Chapter 2. C++ Basics

Multiple Choice (Questions 1 13) 26 Points Select all correct answers (multiple correct answers are possible)

! A program is a set of instructions that the. ! It must be translated. ! Variable: portion of memory that stores a value. char

CSCE Practice Midterm. Data Types

Flow of Control Branching 2. Cheng, Wei COMP May 19, Title

! A literal represents a constant value used in a. ! Numbers: 0, 34, , -1.8e12, etc. ! Characters: 'A', 'z', '!', '5', etc.

Tic Tac Toe Game! Day 8

1. a) What #include statement do you put at the top of a program that does uses cin, cout or endl?

Superior University. Department of Electrical Engineering CS-115. Computing Fundamentals. Experiment No.1

Chapter Overview. C++ Basics. Variables and Assignments. Variables and Assignments. Keywords. Identifiers. 2.1 Variables and Assignments

Chapter 1 - What s in a program?

Chapter 2. C++ Syntax and Semantics, and the Program Development Process. Dale/Weems 1

University of Technology. Laser & Optoelectronics Engineering Department. C++ Lab.

UNIVERSITY OF SWAZILAND SUPPLEMENTARY EXAMINATION, JULY 2013

CS 101 Computer Programming and utilization. Dr Deepak B Phatak Subrao Nilekani Chair Professor Department of CSE, Kanwal Rekhi Building IIT Bombay

Objectives. In this chapter, you will:

CSCE Practice Midterm. Data Types

Introduction to C++ Chapter

CSE120 Wi18 Final Review

Basic Computer Programming (Processing)

Today. o main function. o cout object. o Allocate space for data to be used in the program. o The data can be changed

BITG 1113: Array (Part 2) LECTURE 9

CS Introduction to Programming Midterm Exam #2 - Prof. Reed Fall 2015

CSc Introduc/on to Compu/ng. Lecture 17 Edgardo Molina Fall 2011 City College of New York

Exception with arguments

Computer Science II Lecture 1 Introduction and Background

CS 115 Midterm 2 Solutions

CISC 1600 Lecture 3.1 Introduction to Processing

Linked List using a Sentinel

Chapter Four: Loops II

Agenda. The main body and cout. Fundamental data types. Declarations and definitions. Control structures

Overview. - General Data Types - Categories of Words. - Define Before Use. - The Three S s. - End of Statement - My First Program

CS 376b Computer Vision

Loops and Files. of do-while loop

Computer Programming : C++

Chapter 2 C++ Fundamentals

1) Log on to the computer using your PU net ID and password.

The sequence of steps to be performed in order to solve a problem by the computer is known as an algorithm.

CHAPTER 3 BASIC INSTRUCTION OF C++

Introduction to C++ (Extensions to C)

CSCI 111 First Midterm Exam Fall Solutions 09.00am 09.50am, Wednesday, October 18, 2017

Chapter Four: Loops. Slides by Evan Gallagher. C++ for Everyone by Cay Horstmann Copyright 2012 by John Wiley & Sons. All rights reserved

C++ PROGRAMMING. For Industrial And Electrical Engineering Instructor: Ruba A. Salamh

LECTURE 02 INTRODUCTION TO C++

Tutorial 2: Compiling and Running C++ Source Code

Transcription:

Today in CS161 Lecture #9 **go to Blackboard ** Practicing Review from Shackelford Reading If statements Loops Begin writing Programs Using if statements and loops Graphics Begin creating the tic tac toe program CS161 Lecture #9 1

Shackelford Reading Explain the difference between data types and variables What kind of data type should we use for Keeping track of how many students are in your class Storing if you are a freshman, sophomore, junior, or senior Storing your GPA CS161 Lecture #9 2

Shackelford Reading Now, pick variable names to represent these Keeping track of how many students are in your class Storing if you are a freshman, sophomore, junior, or senior Storing your GPA Give an example of a poor variable name CS161 Lecture #9 3

Shackelford Reading Write an algorithm that will Prompt the user to type in three numbers and then read in those three numbers Calculate the largest of the three and display the result Give an example of an algorithm that would require multiple if, else statements CS161 Lecture #9 4

Review of C++ from Lectures Show an example of how to do output Show an example of how to do input Write a C++ statement to display a message Please enter your age: to the console (screen). What does every complete statement end with? Why do we need to use #include <iostream>? CS161 Lecture #9 5

Review of C++ from Lectures Which of the following are NOT valid assignment statements: Total = 9; 72 = amount; Profit == 129; Letter = w ; Testing = 100 CS161 Lecture #9 6

Review of C++ from Lectures Which of the following are NOT valid output statements: cout << Hello CS161! ; cout << I said: Have Fun! ok? ; cout < 10; cout << Programming is great fun!; CS161 Lecture #9 7

Review of C++ from Lectures Write a program that displays Your name City that you live in College major Write C++ code that calculates the number of acres in a tract of land with 389,767 square ft (one acre of land has 43,560 square ft) CS161 Lecture #9 8

Answer #include <iostream> using namespace std; int main() { float acres; //number of acres in a tract of land acres = 389767 / 43560; cout <<" There are " <<acres <<" acres in 389,767 sq ft" <<endl; } cin.get(); //pause return 0; CS161 Lecture #9 9

Answer more general #include <iostream> using namespace std; const int SQFT_ACRE = 43560; int main() { float acres; //number of acres in a tract of land float sqft; //how many square ft you have cout << "How large is your tract - in square ft? "; cin >> sqft; cin.get(); acres = sqft / SQFT_ACRE; cout <<" There are " <<acres <<" acres in " <<sqft <<" sq ft" <<endl; } cin.get(); //pause return 0; CS161 Lecture #9 10

Practicing Loops Write C++ code that calculates the average rainfall for three months. Ask the user to enter the amount of rainfall for June, July, and August in inches. The program should display the average rainfall from that data received Try this without a loop Now, let s see what this is like with a loop! CS161 Lecture #9 11

Answer program fragment float average; //this will hold the answer float june_rain, july_rain, aug_rain; //rainfalls in inches cout << "Please enter the rainfall in inches for June: "; cin >> june_rain; cin.get(); cout << "Please enter the rainfall in inches for July: "; cin >> july_rain; cin.get(); cout << "Please enter the rainfall in inches for August: "; cin >> aug_rain; cin.get(); //calculate the average average = (june_rain + july_rain + aug_rain)/3.0; cout << "The average rainfall was: " <<average <<"in" <<endl; cin.get(); //pause CS161 Lecture #9 12

Answer with loops!! float average; //this will hold the answer float rain; //rainfalls in inches int counter = 1; float total = 0; //a running total do { cout << "Please enter the rainfall in inches for month " <<counter <<" : "; cin >> rain; cin.get(); ++ counter; total += rain; //we need a running total } while (counter <= 3); //calculate the average average = total / 3.0; cout << "The average rainfall was: " <<average <<"in" <<endl; cin.get(); //pause CS161 Lecture #9 13

Answer more general float average; //this will hold the answer float rain; //rainfalls in inches int counter = 1; float total = 0; //a running total int num_months = 0; //number of months to average cout << "How many months of rainfall are we averaging? "; cin >> num_months; cin.get(); do { cout << "Please enter the rainfall in inches for month " <<counter <<" : "; cin >> rain; cin.get(); ++ counter; total += rain; //we need a running total } while (counter <= num_months); //calculate the average average = total / num_months; cout << "The average rainfall was: " <<average <<"in" <<endl; CS161 Lecture #9 14

Answer (I d use a for loop) float average; //this will hold the answer float rain; //rainfalls in inches float total = 0; //a running total int num_months = 0; //number of months to average cout << "How many months of rainfall are we averaging? "; cin >> num_months; cin.get(); for (int counter = 1; counter <= num_months; ++ counter) { cout << "Please enter the rainfall in inches for month " <<counter <<" : "; cin >> rain; cin.get(); } total += rain; //we need a running total //calculate the average average = total / num_months; cout << "The average rainfall was: " <<average <<"in" <<endl; CS161 Lecture #9 15

Adding if statements What if in the previous program, The user entered a negative number? cout << "How many months of rainfall are we averaging? "; cin >> num_months; cin.get(); if (num_months <= 0) cout << "A negative (or zero) was received...no good!"; else { CS161 Lecture #9 16

Giving the user another chance Now change that to give the user another chance!, The user entered a negative number? cout << "How many months of rainfall are we averaging? "; cin >> num_months; cin.get(); while (num_months <= 0) { cout << "A negative was received...no good!"; cout << endl <<"Try again!" <<endl; cout << "How many months of rainfall are we averaging? "; cin >> num_months; cin.get(); } CS161 Lecture #9 17

Giving the user another chance What about with a do-while? The user entered a negative number? do { cout << "How many months of rainfall are we averaging? "; cin >> num_months; cin.get(); if (num_months <= 0) cout << "A negative was received...no good" <<endl <<"Try Again!" << endl; } while (num_months <= 0); CS161 Lecture #9 18

Beginning the Tic Tac Toe Program 1. Algorithm: Display the board 1. Find out what the window size is 1. Let s keep the width and height the same 2. Set the color to white 3. Set the line width to wider, so we can see the board 4. Draw 2 vertical lines 1/3 rd and 2/3 rd across the window 5. Draw 2 horizontal lines 1/3 rd and 2/3 rd down the window 2. Now think about what variables you will need 1. Whole number to keep track of the window size CS161 Lecture #9 19

Beginning the Tic Tac Toe Program } int window_size; cout << "Please select the size of your window: "; cin >> window_size; cin.get(); initwindow(window_size,window_size); setcolor(15); //15 is WHITE setlinestyle(0,0,6); //Solid, No patter, 6 is VERY wide //vertical lines line(window_size/3, 0, window_size/3, window_size); line(window_size*2/3, 0, window_size*2/3, window_size); //horizontal lines line(0,window_size/3, window_size, window_size/3); line(0,window_size*2/3, window_size, window_size*2/3); //pause getch(); //for graphics window cin.get(); //for console window return 0; CS161 Lecture #9 20

Tic Tac Toe Board CS161 Lecture #9 21

Next Step 1. Algorithm: Select Player 1. Ask the user who will be the first to play 1. X or O 2. If the user enters a lower case or some other character 1. Display an error message 2. Give them another chance 3. Echo the choice to the user, once it is correct 2. Now think about what variables you will need 1. User Input 1. Character data 2. Must be a capital X or capital O CS161 Lecture #9 22

Selecting the Player //Select the starting player char XorO; do { cout << "Please select who will begin X or O: "; cin >> XorO; //check for error conditions if (XorO!= 'X' && XorO!= 'O') cout <<"Try again! Enter a capital X or O: " <<endl; } while (XorO!= 'X' && XorO!= 'O'); //echo the selection cout << "You selected to start with: " <<XorO <<endl; CS161 Lecture #9 23

Selecting the Player CS161 Lecture #9 24

Selecting a location on the board 1. Algorithm: Select Location on the board 1. Prompt the user to select a location on the board 2. Receive input from the mouse in the graphics window 1. Get the x,y location where the mouse hit occurred 2. Find out which square the x,y hit happened in 1. If x is in the first 1/3 rd of window 1. And y is in the first 1/3 rd of window 1. UPPER LEFT corner 2. Else if y is in first 2/3 rd of window 1. MIDDLE LEFT 3. Else 1. LOWER LEFT corner CS161 Lecture #9 25

Selecting a location on the board 2. Else if x is in first 2/3 rd of window 1. And y is in the first 1/3 rd of window 1. UPPER MIDDLE 2. Else if y is in first 2/3 rd of window 1. MIDDLE MIDDLE 3. Else 1. LOWER MIDDLE 3. Else 1. And y is in the first 1/3 rd of window 1. UPPER RIGHT 2. Else if y is in first 2/3 rd of window 1. MIDDLE RIGHT 3. Else 1. LOWER RIGHT CS161 Lecture #9 26

Mouse Hit! cout << "RIGHT BUTTON: Please select the location on the board with the mouse: <<endl; while (!ismouseclick(516)); //wait for a mouse click...right button int x = mousex(); //this is where the mouse click happened (x,y) int y = mousey(); setcolor(yellow); //color of the text settextstyle(0,0,5); //create a really large character (5) settextjustify(1,1); //center both horizontally and vertically int square_size = window_size/3; int half_size = square_size/2; //I created two new variables to help with the calculations if (x < window_size/3) //is it in the left side of the board? if (y < window_size/3) outtextxy(half_size,half_size,"x"); else if (y < window_size*2/3) outtextxy(half_size,window_size*2/3-half_size,"x"); else outtextxy(half_size,window_size-half_size,"x");.//plus more CS161 Lecture #9 27

Mouse Hit! else if (x < window_size *2/3) //middle column if (y < window_size/3) outtextxy(window_size *2/3-half_size,half_size,"X"); else if (y < window_size*2/3) outtextxy(window_size *2/3-half_size,window_size*2/3-half_size,"X"); else outtextxy(window_size *2/3-half_size,window_size-half_size,"X"); else { //right hand column if (y < window_size/3) outtextxy(window_size-half_size,half_size,"x"); else if (y < window_size*2/3) outtextxy(window_size-half_size,window_size*2/3-half_size,"x"); else outtextxy(window_size-half_size,window_size-half_size,"x"); } clearmouseclick(516); //important! CS161 Lecture #9 28

Testing it out so far in a loop CS161 Lecture #9 29

We have just begun 1. We still need to 1. Display the appropriate X versus O, depending on who the player is 2. Add a loop to continue until there is a.. 1. Winner or 2. Cat scratch 3. All to be done.next lecture! CS161 Lecture #9 30