Name the three main control structures used in algorithm development and give an example of each.
Design an algorithm to find the largest number in a list using iteration and selection.
When would you choose iteration over selection in an algorithm?
Write pseudo code to check if a number is even.
Write pseudo code to print numbers from 1 to 5 using a loop.
Answers and Descriptions
Answer: 1. Sequence: Executing statements in order (e.g., adding two numbers). 2. Selection: Making decisions (e.g., if a number is positive). 3. Iteration: Repeating steps (e.g., printing numbers 1 to 10).
Description: Control structures guide program flow. Sequence ensures linear execution, selection enables conditional branching, and iteration supports repetition, forming the core of algorithm design.Answer:
INPUT list
max = list[0]
FOR each number IN list
IF number > max THEN
max = number
ENDIF
ENDFOR
OUTPUT max
Description: This algorithm uses iteration (looping through the list) and selection (comparing numbers) to find the maximum, demonstrating practical application of control structures.
Answer: Use iteration when repeating a task multiple times, such as summing numbers, and selection when making a one-time decision, like checking if a number is positive.
Description: Choosing the appropriate control structure depends on the problem. Iteration is ideal for repetitive tasks, while selection handles conditional logic, optimizing algorithm efficiency.Answer:
INPUT number
IF number MOD 2 = 0 THEN
OUTPUT "Number is even"
ELSE
OUTPUT "Number is odd"
ENDIF
Description: Pseudo code provides a clear, human-readable algorithm representation. This example uses a selection structure to check divisibility by 2, reinforcing control structure application.
Answer:
FOR i = 1 TO 5
OUTPUT i
ENDFOR
Description: A for loop is used when the number of iterations is known. This loop prints numbers 1 to 5, illustrating a basic iteration structure for repetitive tasks.