Top Banner
Example – calculating interest until the amount doubles using a for loop: will calculate up to 10 years, if necessary if condition decides when to terminate loop ak terminates the execution of the whole lo
23

Example – calculating interest until the amount doubles using a for loop: will calculate up to 1000 years, if necessary if condition decides when to terminate.

Dec 19, 2015

Download

Documents

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Example – calculating interest until the amount doubles using a for loop: will calculate up to 1000 years, if necessary if condition decides when to terminate.

Example – calculating interest until the amount doubles using a for loop:

will calculate up to 1000years, if necessary

if condition decideswhen to terminate loop

break terminates the execution of the whole loop.

Page 2: Example – calculating interest until the amount doubles using a for loop: will calculate up to 1000 years, if necessary if condition decides when to terminate.

only needed 10 years

Page 3: Example – calculating interest until the amount doubles using a for loop: will calculate up to 1000 years, if necessary if condition decides when to terminate.

Example – accept input, appending it to vector, until a negative number is entered:

allow up to 1000values, if necessary

Page 4: Example – calculating interest until the amount doubles using a for loop: will calculate up to 1000 years, if necessary if condition decides when to terminate.

negative value stops the input

Page 5: Example – calculating interest until the amount doubles using a for loop: will calculate up to 1000 years, if necessary if condition decides when to terminate.

What if there are more than 2 situations?

3 situations:

find the largest of 3 variables a, b, c

a ≥ b ≥ c a ≥ c ≥ b

b ≥ a ≥ c b ≥ c ≥ a

c ≥ b ≥ a c ≥ a ≥ b

4 situations:

convert a compass angle to a direction:

0º east

90º north

180º west

270º south

Page 6: Example – calculating interest until the amount doubles using a for loop: will calculate up to 1000 years, if necessary if condition decides when to terminate.

Could use “nested” if/else commands

Page 7: Example – calculating interest until the amount doubles using a for loop: will calculate up to 1000 years, if necessary if condition decides when to terminate.

or

Page 8: Example – calculating interest until the amount doubles using a for loop: will calculate up to 1000 years, if necessary if condition decides when to terminate.

The “elseif” command

if expression1

{commands if expression1 is true }

elseif expression2

{commands if expression2 is true }

else

{commands if both expressions are false }

end

Page 9: Example – calculating interest until the amount doubles using a for loop: will calculate up to 1000 years, if necessary if condition decides when to terminate.

Examples:

Note – many elseifsare allowed,

but only 1 “else”

Page 10: Example – calculating interest until the amount doubles using a for loop: will calculate up to 1000 years, if necessary if condition decides when to terminate.

Example – Hi-Lo: a guessing game with feedback

select hidden number

input guess

correct?

yes

noprovide hi/lo feedback

5 tries?

yesno

win

lose

Page 11: Example – calculating interest until the amount doubles using a for loop: will calculate up to 1000 years, if necessary if condition decides when to terminate.
Page 12: Example – calculating interest until the amount doubles using a for loop: will calculate up to 1000 years, if necessary if condition decides when to terminate.

Variable values by example

1 31 61 92 32 62 93 33 6 3 94 34 64 9

inde

x1in

dex2

All possible combinationsof the indices are generated.

Page 13: Example – calculating interest until the amount doubles using a for loop: will calculate up to 1000 years, if necessary if condition decides when to terminate.

Example – computing a table of z = x2+y2 for x and y equal to the integers 1, 2,…6:

Page 14: Example – calculating interest until the amount doubles using a for loop: will calculate up to 1000 years, if necessary if condition decides when to terminate.

Example – matching of people’s skills and tasks:

Situation: 4 tasks 4 people with

different skills to do them

Skill table as shown

Goal – assign tasks to maximize the sum

Example solution of 20

Job 1

Job 2

Job 3

Job 4

Joe 7 4 4 2

Sue 6 8 5 2

Bob 4 7 1 3

Liz 6 5 2 1

Page 15: Example – calculating interest until the amount doubles using a for loop: will calculate up to 1000 years, if necessary if condition decides when to terminate.

Solution – use nested loops to try all combinations, skipping repeats:

First, let’s initialize variables:

  Job

  1 2 3 4

Joe 7 4 4 2

Sue 6 8 5 2

Bob 4 7 1 3

Liz 6 5 2 1

Page 16: Example – calculating interest until the amount doubles using a for loop: will calculate up to 1000 years, if necessary if condition decides when to terminate.

Next, start nested loops:

Check for repeats and skip

continue stops the present pass and starts the next pass.

Page 17: Example – calculating interest until the amount doubles using a for loop: will calculate up to 1000 years, if necessary if condition decides when to terminate.

Test a valid assignment for quality:

And then terminate the 4 for loops:

Page 18: Example – calculating interest until the amount doubles using a for loop: will calculate up to 1000 years, if necessary if condition decides when to terminate.

The result:

Job 1

Job 2

Job 3

Job 4

Joe 7 4 4 2

Sue 6 8 5 2

Bob 4 7 1 3

Liz 6 5 2 1

Page 19: Example – calculating interest until the amount doubles using a for loop: will calculate up to 1000 years, if necessary if condition decides when to terminate.

Debugging ≡ finding and correcting errors (bugs) in programs

Useful debugging tools:– Ability to stop a program in the middle of its

execution (at a breakpoint)– Ability to examine variable values at that point– Ability to modify variable values at that point

Page 20: Example – calculating interest until the amount doubles using a for loop: will calculate up to 1000 years, if necessary if condition decides when to terminate.

controls forcreating and

removingbreakpoints

Page 21: Example – calculating interest until the amount doubles using a for loop: will calculate up to 1000 years, if necessary if condition decides when to terminate.

indicator of breakpoint location(can have multiple breakpoints)

Page 22: Example – calculating interest until the amount doubles using a for loop: will calculate up to 1000 years, if necessary if condition decides when to terminate.

What shows up at the breakpoint Command window: Editor window:

location indicator

differentprompt

Page 23: Example – calculating interest until the amount doubles using a for loop: will calculate up to 1000 years, if necessary if condition decides when to terminate.

Can single step (F10) or continue (F5) to the next breakpoint (or end)