Unit 6
Algorithms
To do: March 22 - Please write in your IN.
Loop example #1
REPEAT 3 TIMES
{
snap once
clap once
}
Nested loop example #1
REPEAT 4 TIMES
{
REPEAT 3 TIMES
{
snap once
clap once
}
Step once to the right
}
Loop example #2
REPEAT 4 TIMES
{
DISPLAY("a")
}
Loop example #2
REPEAT 4 TIMES
{
DISPLAY("a")
}
OUTPUT
a a a a
Nested loop example #2
REPEAT 2 TIMES
{
REPEAT 4 TIMES
{
DISPLAY("a")
}
DISPLAY("b")
}
Nested loop example #2
REPEAT 2 TIMES
{
REPEAT 4 TIMES
{
DISPLAY("a")
}
DISPLAY("b")
}
OUTPUT
a a a a b a a a a b
Nested loop example #3 - How many a's will be printed?
REPEAT 5 TIMES
{
REPEAT 3 TIMES
{
DISPLAY("a")
}
}
Nested loop example #3 - How many a's will be printed? 15
REPEAT 5 TIMES
{
REPEAT 3 TIMES
{
DISPLAY("a")
}
}
Nested loops in the real world: clocks!
Nested loops MC question
In the following code segment, assume that x and y have been assigned integer values.
sum <- 0
REPEAT x TIMES{
REPEAT y TIMES{
sum <- sum + 1
}
}
At the end of which of the following code segments is the value of sum the same as the value of sum at the end of the preceding code segment? SELECT TWO ANSWERS.
A.
B.
C.
D.
Nested loops MC question
In the following code segment, assume that x and y have been assigned integer values.
sum <- 0
REPEAT x TIMES{
REPEAT y TIMES{
sum <- sum + 1
}
}
At the end of which of the following code segments is the value of sum the same as the value of sum at the end of the preceding code segment? SELECT TWO ANSWERS.
A.
B.
C.
D.
Go over homework:
Algorithms solve Problems
Problem: a general description of a task that can (or sometimes cannot) be solved with an algorithm
Algorithm: a finite set of instructions that accomplish a task.
Battleship Game - TWO rounds
Homework