computer science i: 10-6-2015 understand how to evaluate expressions with div and mod random numbers...

15
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.

Upload: monica-adams

Post on 18-Jan-2018

222 views

Category:

Documents


0 download

DESCRIPTION

Dry Run the Following

TRANSCRIPT

Page 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/…

Computer Science I: 10-6-2015

Understand how to evaluate expressions with DIV and MOD

Random NumbersReading random codeWriting random code

Odds/evens/…

Get out your notes.

Page 2: 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/…

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.

Page 3: 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/…

Dry Run the Following

Page 4: 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/…

DIV = Integer Division

1. 20 DIV 92. 10 DIV 33. 21 DIV 124. 9 DIV 105. 5 DIV 26. 1232 DIV 10007. 1232 DIV 1008. 1232 DIV 10

Page 5: 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/…

MOD = Remainder of Integer Division

1. 20 MOD 92. 10 MOD 33. 21 MOD 124. 9 MOD 105. 5 MOD 26. 1232 MOD 10007. 1232 MOD 1008. 1232 MOD 10

Page 6: 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/…

Random Numbers…why use them?

• Testing data• Modeling• Gaming• Adding

‘chance’ to something

Page 7: 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/…

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;

Page 8: 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/…

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

Page 9: 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/…

Example Programprogram 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}

Page 10: 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/…

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;

Page 11: 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/…

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

Page 12: 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/…

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;

Page 13: 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/…

Check for Understanding: Write the line of code to generate the following ranges of values

1. 1 to 82. 10 to 343. 19 to 274. -3 to 125. -5 to 316. -6 to -17. 8 to 24 even8. 7 to 35 odd9. -7 to 21 odd10. -10 to 10 even11. A pair of six sided dice

Page 14: 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/…

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 answers4. At the end the computer shows how many the user answered

correctly.

If you are stuck…Hands on,

Pseudo-code, Dry run, Code.

Page 15: 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/…

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