Today in CS161 Week #5 **weekly visit D2L** Practicing…

Slides:



Advertisements
Similar presentations
Summer Computing Workshop. Introduction to Variables Variables are used in every aspect of programming. They are used to store data the programmer needs.
Advertisements

CS161 Topic #11 1 Today in CS161 Lecture #11 More Functions Write Programs using Functions Black Jack (add the dealer and make corrections) Arguments Now.
CS Sept Your first C++ program… Boilerplate // Cannon, demo program #include using namespace std; int main() {// program goes here… return.
1 9/29/06CS150 Introduction to Computer Science 1 Loops Section Page 255.
1 11/3/08CS150 Introduction to Computer Science 1 Reading from and Writing to Files Section 3.12 & 13.1 & 13.5.
Computer Science 1620 Variables and Memory. Review Examples: write a program that calculates and displays the average of the numbers 45, 69, and 106.
1 9/29/06CS150 Introduction to Computer Science 1 Loops Section Page 255.
1 9/28/07CS150 Introduction to Computer Science 1 Loops section 5.2, 5.4, 5.7.
1 10/20/08CS150 Introduction to Computer Science 1 do/while and Nested Loops Section 5.5 & 5.11.
1 10/29/07CS150 Introduction to Computer Science 1 Reading from and Writing to Files Section 3.12 & 13.1 & 13.5.
1 10/25/06CS150 Introduction to Computer Science 1 Reading from and Writing to Files.
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.
VARIABLES, TYPES, INPUT/OUTPUT, ASSIGNMENT OPERATION Shieu-Hong Lin MATH/CS Department Chapel.
Today in CS161 Lecture #4 Solving Problems with Computers Walk through the Tic Tac Toe Algorithm Getting ready for Creating Programs Turn the Inches to.
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.
CS161 Topic #21 CS161 Introduction to Computer Science Topic #2.
Today in CS161 Lecture #9**go to Blackboard ** Practicing… Review from Shackelford Reading If statements Loops Begin writing Programs Using if statements.
Chapter 4 Selection Structures: Making Decisions.
1 C++ Programming Basics Chapter 2 Lecture CSIS 10A.
1 CS161 Introduction to Computer Science Topic #3.
Today in CS161 Lecture #8 Learn about… Logicals: And, Or, Not Repetition: loops! Rewrite our First Program Using loops Graphics Let’s make the graphics.
CS161 Topic #16 1 Today in CS161 Lecture #16 Prepare for the Final Reviewing all Topics this term Variables If Statements Loops (do while, while, for)
1 CS161 Introduction to Computer Science Topic #8.
Today in CS161 Week #2 Solving Problems with Computers What are Algorithms Write a short Algorithm Preparing to write programs On Unix and Mac systems.
Today in CS161 Lecture #5 Learn about… Data types (char, int, float) Input and Output (cin, cout) Writing our First Program Write the Inches to MM Program.
CS Jan 2007 Chapter 2 sections 1, 2, 4 – 6, 8,
Lecture 5: Expressions and Interactivity Professor: Dr. Miguel Alonso Jr. Fall 2008 CGS2423/COP1220.
Think First, Code Second Understand the problem Work out step by step procedure for solving the problem (algorithm) top down design and stepwise refinement.
Lecture 5 Computer programming -1-. Input \ Output statement 1- Input (cin) : Use to input data from keyboard. Example : cin >> age; 2- Output (cout):
11 Introduction to Object Oriented Programming (Continued) Cats.
Midterm: Question 1 (35%) (30 minutes) Write an assembly program to draw a rectangle. – (5%) Show a message to ask for information of the rectangle – (5%)
CS161 Topic #10 1 Today in CS161 Lecture #10 Introduction to Functions What is a function? Why would you want to use a function? How do you define functions?
CS 240 Computer Programming 1
Today in CS161 Lecture #7 Learn about… If and else statements Rewrite our First Program Using if and else statements Create new Graphics Demos Using if.
Looping I (while statement). CSCE 1062 Outline  Looping/repetition construct  while statement (section 5.1)
Scratch Programming Cards
Bill Tucker Austin Community College COSC 1315
Introduction to Computer Programming
Repetitive Structures
PYGAME.
CS161 Introduction to Computer Science
Computing Fundamentals
Chapter 2 Assignment and Interactive Input
Today in CS161 Week #2 Preparing to write programs Assignments
Introduction to C++ October 2, 2017.
Today in CS161 Week #7 Arrays Learning about arrays Examples Graphical
Multi-dimensional Array
Starter Write a program that asks the user if it is raining today.
Chapter 2 part #3 C++ Input / Output
Introduction to Object-Oriented Programming
Starting Out with C++: From Control Structures through Objects
Today in CS161 Week #3 Learn about… Writing our First Program
Today in CS161 Week #4 Using if statements Learn about Repetition
CS150 Introduction to Computer Science 1
Variables, Identifiers, Assignments, Input/Output
Today in CS161 Week #4 Learn about… Rewrite our First Program
Let’s Write a Graphics Program
CS150 Introduction to Computer Science 1
Let’s all Repeat Together
Today in CS161 Week #6 Introduction to Functions
Let’s all Repeat Together
Today in CS161 Week #5 **go to Blackboard ** Practicing…
do/while Selection Structure
Reading from and Writing to Files
Chapter 2 part #3 C++ Input / Output
CS31 Discussion 1D Fall18: week 2
Reading from and Writing to Files Part 2
Tic-Tac-Toe Game Engine
Reading from and Writing to Files
Programming Fundamental
Presentation transcript:

Today in CS161 Week #5 **weekly visit D2L** Practicing… Review from Reading If statements Loops Continue writing Programs Using if statements and loops Graphics Begin creating the tic tac toe program CS161 Week #5

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 Week #5

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 Week #5

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 Week #5

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 Week #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 Week #5

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 Week #5

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 Week #5

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 Week #5

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 Week #5

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 Week #5

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 Week #5

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 Week #5

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 Week #5

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 Week #5

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 Week #5

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; } CS161 Week #5

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 Week #5

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

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 Week #5

Tic Tac Toe Board… CS161 Week #5

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

Selecting the Player //Select the starting player char XorO; do { cout << "Please select who will begin X or O: "; cin >> XorO; cin.get(); //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 Week #5

Selecting the Player… CS161 Week #5

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

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

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; //I created two new variables to help with the calculations int half_size = square_size/2; 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 Week #5

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"); { //right hand column outtextxy(window_size-half_size,half_size,"X"); outtextxy(window_size-half_size,window_size*2/3-half_size,"X"); outtextxy(window_size-half_size,window_size-half_size,"X"); } clearmouseclick(516); //important! CS161 Week #5

Testing it out so far…in a loop CS161 Week #5

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