Summer 2012 Instructor: Hassan Khosravi

Slides:



Advertisements
Similar presentations
Topic Reviews For Unit ET156 – Introduction to C Programming Topic Reviews For Unit
Advertisements

Chapter 4 Loops Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved
Combining Like Terms. Only combine terms that are exactly the same!! Whats the same mean? –If numbers have a variable, then you can combine only ones.
Writing Pseudocode And Making a Flow Chart A Number Guessing Game
Credit hours: 4 Contact hours: 50 (30 Theory, 20 Lab) Prerequisite: TB143 Introduction to Personal Computers.
0 - 0.
MULT. INTEGERS 1. IF THE SIGNS ARE THE SAME THE ANSWER IS POSITIVE 2. IF THE SIGNS ARE DIFFERENT THE ANSWER IS NEGATIVE.
Mike Scott University of Texas at Austin
Evaluate the numerical expression 52 – 2 ∙ 4 + (7 – 2)
1 University of Utah – School of Computing Computer Science 1021 "Thinking Like a Computer"
5.9 + = 10 a)3.6 b)4.1 c)5.3 Question 1: Good Answer!! Well Done!! = 10 Question 1:
A8 – Control Structures if, if-else, switch Control of flow in Java Any sort of complex program must have some ability to control flow.
EasyGUI “Probably the Easiest GUI in the world”. Assumptions (Teachers’ Notes) This resources sets out an introduction to using easyGUI and Python
CMPT 120 Functions and Decomposition
CMPT 120 Control Structures in Python
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 10P. 1Winter Quarter Repetition Structures.
While Loop Lesson CS1313 Spring while Loop Outline 1.while Loop Outline 2.while Loop Example #1 3.while Loop Example #2 4.while Loop Example #3.
N.S. 1.3: Converting Fractions, Decimals, & Percents Period Odds: 9/19/13 Period Evens: 9/20/13.
Chapter 5 Loops Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved.
CS 240 Computer Programming 1
Test B, 100 Subtraction Facts
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 8P. 1Winter Quarter Control Statements Lecture.
CHAPTER 11 FILE INPUT & OUTPUT Introduction to Computer Science Using Ruby (c) 2012 Ophir Frieder et al.
CMPT 100 : INTRODUCTION TO COMPUTING TUTORIAL #5 : JAVASCRIPT 2 GUESSING GAME By Wendy Sharpe 1.
L6:CSC © Dr. Basheer M. Nasef Lecture #6 By Dr. Basheer M. Nasef.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 3 Loops.
CS107 Introduction to Computer Science Lecture 3, 4 An Introduction to Algorithms: Loops.
CS107 Introduction to Computer Science Lecture 2.
PSEUDOCODE & FLOW CHART
Week 5 - Friday.  What did we talk about last time?  Repetition  while loops.
CS107 Introduction to Computer Science Loops. Instructions Pseudocode Assign values to variables using basic arithmetic operations x = 3 y = x/10 z =
CS150 Introduction to Computer Science 1
CS107 Introduction to Computer Science Lecture 5, 6 An Introduction to Algorithms: List variables.
Complexity (Running Time)
Main task -write me a program
Review Algorithm Analysis Problem Solving Space Complexity
An Introduction to Textual Programming
Control Structures FOR Statement Looping.
Instructor: Chris Trenkov Hands-on Course Python for Absolute Beginners (Spring 2015) Class #002 (January 17, 2015)
 Make sure you are subscribed to announcements on Moodle.  Activity 4 will be due 48hrs after your lab ends.
An Introduction to Python Programming Dr. Mark Goadrich Centenary College of Louisiana NWLAPCUG - May 15th 2008.
Writing Program Code in BASIC Write a program to prompt for and accept values into TWO variables, numx and numy. The program should square the value stored.
Making Decisions uCode: October Review What are the differences between: o BlueJ o Java Computer objects represent some thing or idea in the real.
Control Structures II Repetition (Loops). Why Is Repetition Needed? How can you solve the following problem: What is the sum of all the numbers from 1.
Georgia Institute of Technology More on Creating Classes part 2 Barb Ericson Georgia Institute of Technology Oct 2005.
COSC 1306—COMPUTER SCIENCE AND PROGRAMMING PYTHON BRANCHES AND LOOPS Jehan-François Pâris
Conditional Loops CSIS 1595: Fundamentals of Programming and Problem Solving 1.
1 CS161 Introduction to Computer Science Topic #8.
Algorithm Discovery and Design Objectives: Interpret pseudocode Write pseudocode, using the three types of operations: * sequential (steps in order written)
Think Possibility 1 Iterative Constructs ITERATION / LOOPS C provides three loop structures: the for-loop, the while-loop, and the do-while-loop. Each.
PROGRAMMING IN PYTHON LETS LEARN SOME CODE TOGETHER!
EGR 115 Introduction to Computing for Engineers Branching & Program Design – Part 3 Friday 03 Oct 2014 EGR 115 Introduction to Computing for Engineers.
The Department of Engineering Science The University of Auckland Welcome to ENGGEN 131 Engineering Computation and Software Development Lecture 2 Debugging,
Computer Science I: Understand how to evaluate expressions with DIV and MOD Random Numbers Reading random code Writing random code Odds/evens/…
Learning Objective To be able to… Understand flow chart symbols Complete and correct flow chart algorithms Create a program based on a flow chart.
Conditionals.
Loops Brent M. Dingle Texas A&M University Chapter 6 – Section 6.3 Multiway Branches (and some from Mastering Turbo Pascal 5.5, 3 rd Edition by Tom Swan)
PYTHON PROGRAMMING Year 9. Objective and Outcome Teaching Objective Today we will look at conditional statements in order to understand how programs can.
Repetition Structures
Week of 12/12/16 Test Review.
Learning to Program in Python
Introduction to pseudocode
For -G7 programing language Teacher / Shamsa Hassan Alhassouni.
Algorithm Discovery and Design
Programming Concepts and Database
Computer Science 1 Online time for Graphics Program Random review
How can you make a guessing game?
How can you make a guessing game?
Lecture 17 – Practice Exercise 3 SOLUTIONS
Presentation transcript:

Summer 2012 Instructor: Hassan Khosravi CMPT 120 Introduction To Computing Science And Programming I Pseudocode Summer 2012 Instructor: Hassan Khosravi

Guessing game Tell the user to pick a secret number between 1 and 100. The smallest possible number is 1; the largest possible is 100. Make a guess that is halfway between the smallest and largest (round down if necessary). Ask the user if your guess is too large, too small or correct. If they say you’re correct, you win and the game is over. If they say your guess is too small, the smallest possible number is now the guess plus one. If they say your guess is too large, the largest possible number is now the guess minus one. Unless you guessed correctly, go back to step 3.

How many guess are required if your number is 33? Can you think of a number where the algorithm in Figure 1.2 will make 7 guesses? Can you think of a number where the algorithm in Figure 1.2 will make 8 guesses? What is “legitimate input” for this algorithm? What happens if the user enters something else?

Pseudocode you need a way to describe the algorithms that you are going to implement. This is often done with pseudocode “pseudo-” means “almost”  Pseudocode is almost code

Storing Information loops Write command Write command Round down If statement

Pseudocode Storing Information: Set variable to value Set x to 1 Set y to “hello” Set z to True Write command: writes whatever is inside “” in the terminal Write “Hello” Write x Outputs the value of x Read Command: sets the value of the variable to what is read from the terminal Read x

Example Read two numbers and return their average Write "Enter two numbers" read first_number read second_number set average to (first_number + second_number)/ 2 write "The average is" write average

Example Read the height of a person in meters and output their height in inches 1 meter = 39.37 inch write"Enter your height in meters" read height_in_meters set height_in_inches to height_in_meters * 39.37 Write "Your height is" write height_in_inches write "inches tall"

Example Read the height of a person in meters and output their height in feet 1 feet = 12 inch write"Enter your height in meters" read height_in_meters set height_in_feet to height_in_meters * 39.37 /12 Write "Your height is" write height_in_feet write "feet tall"

Round down set y to If x is 2.3 then y=2 If x -2.3 then y = -3 Round up Set y to If x is 2.3 then y=3 If x is -2.3 then y=-2

Example inches 10.866  10 you are 5 10 tall Read the height of a person in meters and output their height in feet and inches write"Enter your height in meters" read height_in_meters set height_in_inches to height_in_meters * 39.37 set feet to height_in_inches/12 round down feet to nearest integer set inches to height_in_inches - (feet *12) Round down inches write "you are" write feet, inches write "tall“ Let’s try 1.80 height in meters: 1.80 height in inches 70.866 height in feet 5.09  5 inches 10.866  10 you are 5 10 tall

If statement The most common way to make decisions is by using the if statement. If Command If statement then Do some stuff Else Do some other stuff The else part is optional and can be omitted. Read X If X < 2 then Write X “is smaller than two” Write X “is bigger or equal to two”

Read a number, if it is odd then write odd, if it is even write it is even write "Enter a number" Read number set number to number/2 set number_roundup to round up number set number_rounddown to round down number if number_roundup = number_rounddown then print write"even" else write "odd"

What do you think of Pseudocode and the examples A: They are very easy and the pace of the class is slow B: They are understandable and the pace is right C: I am finding the examples a little hard to follow. The pace of the class is too fast D: I just don’t get Pseudocode

Read a number between 1 and 10. try to guess it Read a number between 1 and 10. try to guess it? If you guess it correctly say you win, else say you lose. write "Enter a number between 1 to 10" Read number set guess to random value between 0 to 10 if number = guess then write "Wow" else write "Nope, wrong answer"

Question What would be the output if you input 13 Write “How old are you” if age <= 2 then write “you fly for free” Else if 2 < age < 13 then write “you pay the kids rate‘” Else then write 'you pay regular adult fare’ A: No output B: you fly for free C: you pay the kids rate D: you pay regular adult fare E: You get an error The Value for age is undefined, so it doesn’t make sense to compare it. You will get an error

Read three numbers and return their maximum Write "Enter three numbers" read num1,num2,num3 if num1>num2 then if num1 > num3 then write num1 else write num3 if num2 > num3 then write num2

Definite Iteration: for loops We need to be able to execute the same code several times The for loop The for loop can be used when you know ahead of time how many times you want to execute some code For a value i equal to each number from 1 to n: Statement regarding I Compute the factorial for a input value n! = 1 × 2 × 3 × · · · × (n − 1) × n . write “Enter a nonnegative integer:” read n set factorial to 1 for i equal to each number from 1 to n: set factorial to factorial × i write factorial

Examples Read a value n and count from 1 to n write "Enter a number" read n for i equal to each number from 1 to n\ write i

Examples Compute the average of 10 numbers set sum to 0 for i equal to each number from 1 to 10 read num set sum to sum + num average = sum/10 write average

Examples Read a value and determine whether it is prime or not write "read number" read n set prime to True for i from 1 to roundup(n/2) if remainder of n/i is 0 set prime to False if prime = True write "your number is prime" if prime = False write "your number is not prime"

Examples Compute the first n values in the Fibonacci series. Fibonacci series  are the numbers in the following integer sequence: 0,1,1,2,3,5,… Fn = Fn-1 + Fn-2 F0 = 0, F1 =1

set smaller to 0 set bigger to 1 write smaller write bigger do this for i equal to each number from 1 to n-2\ newnum = smaller + bigger write newnum smaller = bigger bigger= newnum Trace the code to see what the 8th number in the series would be A: 10 B:8 C:13 D:21 E:none of the above

Indefinite Iteration: while loops If you don’t know how many times you want the loop body to execute, the for loop is hard to work with. While a statement holds do Some stuff You could write this with a for loop if you analyze it carefully 

Example Calculate the sum of some positive numbers, when you are finished with your numbers give -1 as your number to terminate the program sum of some positive numbers write "Enter some numbers" set sum to 0 read number while number >0 sum = sum +num write "sum of numbers are" write sum

Example write"Enter a value" read n k = 0 while k*k =< n K=K+1 Enter a value n. Find the smallest number k where k2 is bigger than n write"Enter a value" read n k = 0 while k*k =< n K=K+1 write k

If we put num1 as 13 and num2 as 3, what would be the output write"Enter two numbers" read num1,num2 num3=0 while num1>= num2 num1 = num1 - num2 num3 = num3+1 write num3, num1 A: num3 = 2, num1=2 B: num3 = 4, num1=1 C: num3=3, num1 = 5 D: num3=0, num1=0 E none of the Above