Download presentation
Presentation is loading. Please wait.
Published byMonica Adams Modified over 8 years ago
1
Computer Science I: 10-6-2015 Understand how to evaluate expressions with DIV and MOD Random Numbers Reading random code Writing random code Odds/evens/… Get out your notes.
2
Learning Objectives Review a program that uses a for loop with a dry run. Review DIV and MOD operations. Be able to read a program that uses random numbers. Be able to write a program that uses random numbers.
3
Dry Run the Following
4
DIV = Integer Division 1.20 DIV 9 2.10 DIV 3 3.21 DIV 12 4.9 DIV 10 5.5 DIV 2 6.1232 DIV 1000 7.1232 DIV 100 8.1232 DIV 10
5
MOD = Remainder of Integer Division 1.20 MOD 9 2.10 MOD 3 3.21 MOD 12 4.9 MOD 10 5.5 MOD 2 6.1232 MOD 1000 7.1232 MOD 100 8.1232 MOD 10
6
Random Numbers…why use them? Testing data Modeling Gaming Adding ‘chance’ to something
7
How do you do it? Seed the computer’s random number generator – Randomize; {Command} Make the computer generate a random number in a given range. – Random command Number:= random(12)+ 2;
8
Syntax Answer:= random(range) + offset; – Answer:=random(100) + 1; – Coin:= random(2)+1; – Die:= random(6)+1; Answer:= 2*random(range) + offset; – Evans/Odds – Evento100:= 2*random(50)+2; – Oddto99:=2*random(50)+1
9
Example Program program flipper; var count, coin:integer; begin randomize; for count := 1 to 50 do begin coin:= random(2) + 1; {1 = heads, 2 = tails} if coin = 1 then writeln('Heads') else writeln('Tails'); end; {Of the for loop} end. {Of the program}
10
Reading code Name the range of values that the following could generate Ans:= random(5); Ans:= random(6)+2; Ans:= random(40)-7; Ans:= 2*random(10)+3; Ans:= 2*random(9)-7;
11
Writing code Write the random line to generate a number for the following ranges. 1-6 2-30 A pair of 6-sided dice 3-9 odd 12-24 even
12
Check for Understanding: Find the range of values the following code produces. 1)ans:=random(6)+1; 2)ans:=random(14)+2; 3)ans:=random(7)-4; 4)ans:=random(12)+5; 5)ans:=random(23)+8; 6)ans:=random(50)-5; 7)ans:=random(18)+370; 8)ans:=2* random(10) + 1; 9)ans:=2* random(9) + 6; 10)ans:=2* random(21) + 10; 11)ans:=2* random(100) + 51;
13
Check for Understanding: Write the line of code to generate the following ranges of values 1.1 to 8 2.10 to 34 3.19 to 27 4.-3 to 12 5.-5 to 31 6.-6 to -1 7.8 to 24 even 8.7 to 35 odd 9.-7 to 21 odd 10.-10 to 10 even 11.A pair of six sided dice
14
Program options 1.Flip a coin 100 times, show the total number of heads and tails AND which occurred the most often. 2.Roll a pair of 6 sided dice 100 times. Find out which occurs most often, 3 or 11. Show how often each of these rolls occur and which occurs most often. 3.Math tutor: 1.User inputs the number of sample addition problems o show. 2.The computer generates the problems and shows them to the user. 3.The user guesses the answers 4.At the end the computer shows how many the user answered correctly. If you are stuck… Hands on, Pseudo-code, Dry run, Code.
15
Program options. Input: 10 ages Output: The age of the oldest person. – Push: Also enter names and output the name and age of the oldest person – Push: Show the name and age for the top 3. – Push: Calculate the average age Input: 10 times (in seconds) Output: The fastest time. – Push: Also enter names and output the name and time of the fastest person – Push: Show the name and time for the top 3. – Push: Calculate the average time
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.