38.9k views
5 votes
The following algorithm displays a sequence of 10 odd squares:

Start by setting n to 1.
Repeat 10 times:
Multiply n by itself and display the result.
Add 2 to n.
Which of these correctly expresses that algorithm in pseudocode?

A
n ← 1
REPEAT 10 TIMES {
result ← n * n
DISPLAY(n)
n ← n + 2
}

B
n ← 1
REPEAT 10 TIMES {
result ← n * n
DISPLAY(result)
n ← n + 2
}

C
n ← 1
REPEAT 10 TIMES {
result ← n * 2
DISPLAY(result)
n ← n + 2
}

D
n ← 1
REPEAT 10 TIMES {
result ← n * n
DISPLAY(result)
n ← 2
}

User ImGroot
by
8.0k points

1 Answer

2 votes

Final answer:

The correct pseudocode to display a sequence of 10 odd squares is option B, where 'n' starts at 1 and in each iteration within the loop, 'n' is squared, the result is displayed, and then 'n' is incremented by 2.

Step-by-step explanation:

The algorithm in question aims to display a sequence of 10 odd squares. The correct pseudocode that represents this algorithm is option B:

n ← 1
REPEAT 10 TIMES {
result ← n * n
DISPLAY(result)
n ← n + 2
}

This pseudocode starts by setting the variable n to 1. It then enters a loop that repeats 10 times. In each iteration, the algorithm squares the current value of n (which is an odd number), outputs the result, and then increments n by 2, ensuring that the next number n is also odd. The other options either display the wrong value, incorrectly increment n, or both.

The correct expression in pseudocode for the given algorithm is:

B)n <<< 1

REPEAT 10 TIMES { result <<< n * n DISPLAY(result) n <<< n + 2}

This algorithm starts by setting the variable n to 1. It then repeats the following steps 10 times: multiply n by itself to get the square, display the result, and add 2 to n for the next iteration. The correct pseudocode expression reflects these steps by calculating the square and displaying the result before incrementing n by 2.

User Liysd
by
8.4k points

No related questions found