Sovi.AI - AI Math Tutor

Scan to solve math questions

QUESTION IMAGE

fix the bugs in the following pseudocode. // this program is supposed t…

Question

fix the bugs in the following pseudocode.
// this program is supposed to display every fifth year
// starting with 2017; that is, 2017, 2022, 2027, 2032,
// and so on, for 30 years.
start
declarations
num year
num start_year = 2017
num factor = 5
num end_year = 30
while year <= end_year
output year
endif
stop

Explanation:

Step1: Initialize the year variable

The original code didn't initialize the year variable. It should be set to START_YEAR (2017) at the beginning. So we add year = START_YEAR.

Step2: Correct the loop condition

The original loop condition while year <= END_YEAR was incorrect. Since END_YEAR represents the number of years (30) and we are incrementing by FACTOR = 5 each time, the correct upper - limit for the loop should be START_YEAR+(END_YEAR * FACTOR).

Step3: Increment the year variable

The original code had no way to update the year variable. We add year = year + FACTOR inside the loop to get the next year in the sequence (e.g., 2017, 2022, 2027...). Also, change endif to endwhile as it is a while loop, not an if - statement.

Answer:

start
    Declarations
        num year
        num START_YEAR = 2017
        num FACTOR = 5
        num END_YEAR = 30
        year = START_YEAR
    while year <= (START_YEAR + (END_YEAR * FACTOR))
        output year
        year = year + FACTOR
    endwhile
stop