Download presentation
Presentation is loading. Please wait.
1
Computer Science I: Get out your notes. Understand how to evaluate expressions with 10 minutes online time to finish program DIV and MOD Random Numbers Reading random code Writing random code Odds/evens/…
2
Program options from Last Class
10 minutes to finish up program 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 Challenge: What is 4(1/1 - 1/3 + 1/5-1/7+1/9…)? Write a program to determine and prove your proposal.
3
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.
4
Dry Run the Following
5
DIV = Integer Division 20 DIV 9 10 DIV 3 21 DIV 12 9 DIV 10 5 DIV 2
6
MOD = Remainder of Integer Division
7
Random Numbers…why use them?
Testing data Modeling Gaming Adding ‘chance’ to something
8
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;
9
Syntax Answer:= random(range) + offset;
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
10
Example Program program flipper; var count, coin:integer; begin randomize; for count := 1 to 50 do 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}
11
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;
12
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
13
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;
14
Check for Understanding: Write the line of code to generate the following ranges of values
8 to 24 even 7 to 35 odd -7 to 21 odd -10 to 10 even A pair of six sided dice
15
Program options If you are stuck… Hands on, Pseudo-code, Dry run,
Flip a coin 100 times, show the total number of heads and tails AND which occurred the most often. 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. Math tutor: User inputs the number of sample addition problems o show. The computer generates the problems and shows them to the user. The user guesses the answers At the end the computer shows how many the user answered correctly.
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.