For loops Flashcards

1
Q
A program has been written to obtain 3 random virtual dice rolls:
DiceRoll(1) = Random Number 1-6
DiceRoll(2) = Random Number 1-6
DiceRoll(3) = Random Number 1-6
DISPLAY DiceRoll(1)
DISPLAY DiceRoll(2)
DISPLAY DiceRoll(3)
Rewrite this algorithm in pseudocode so that it makes use of iteration.
A

FOR LOOP EXAMPLE:
FOR counter 1 – 3:
dice_number = Random Number 1-6
OUTPUT dice_number

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Consider the array numbers = [2, 5, 3, 6, 2, 3, 6, 4] where the value of
numbers(2) is 3.
Write an algorithm in pseudocode which:
- Adds up the numbers in the array
- Displays the result.
To get full marks a loop should be used in your algorithm.

A

total = 0
FOR counter 0 to lengthOfNumbers
total = total + numbers(counter)
OUTPUT total

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

A computer game shows 6 players around a table on seats.
They are numbered 1 to 6.
The names of the players are stored in an array with 6 elements called PlayerName. The index position of the array is
used to indicate the seat number. For example, the value of PlayerName(1) is ‘Helen’.
During the game, each player sometimes moves clockwise by a given number of places.
For example, if the number of places is 2, Helen will move to seat 3, Priya will move to seat 1 etc.
Write an algorithm in pseudocode, which updates the contents of the array ‘PlayerMove’ after a move has
occurred. The algorithm should:
- Allow the number of places to move to be input
- Use iteration
- Ensure that all of the players’ names are moved to the correct position in the array

A
INPUT Num
FOR loop = 1 to Num
Temp = PlayerName(6)
PlayerName(6) = PlayerName(5)
PlayerName(5) = PlayerName(4)
PlayerName(4) = PlayerName(3)
PlayerName(3) = PlayerName(2)
PlayerName(2) = PlayerName(1)
PlayerName(1) = Temp
Next i
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

A display board can show a flashing message of up to 20 characters.
Write an algorithm in pseudocode which:
- Allows the user to input the message and the number of flashes
- Rejects the message if it is longer than 20 characters and stops
- Otherwise it repeatedly displays the message and clears the display
for the correct number of times.

A
INPUT Message
INPUT NumberOfFlashes
IF length(Message) > 20 Then
OUTPUT “This message is too long”
ELSE
FOR i = 1 to NumberOfFlashes
Display Message
Wait
Clear Message
Wait
NEXT i
END IF
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

You are writing a program that uses an array called WordList. This array
contains 10 foreign words in alphabetical order. The contents of the array
are shown on the left.
You need to write a an algorithm in pseudocode that:
- allows the user to input a word,
- goes through the items in the array WordList in turn, starting from the
WordList(1),
- if it finds the word that the user has input, it outputs “Word found”.

A
INPUT SearchWord
FOR count 1 to Length(WordList)
IF WordList(count) = SearchWord THEN
OUTPUT “Word Found”
END IF
How well did you know this?
1
Not at all
2
3
4
5
Perfectly