Answer to Question #320435 in Algorithms for NTANGA

Question #320435

1. Determine the exact output that is produced when the following code is executed. (7)


for x = 1 to 10 step 1


for y = 1 to x step 1


if y MOD 2 = 0 AND x MOD 2 <> 0 || y MOD 2 <> 0 AND x MOD 2 = 0 then


display "*"


else


display "#"


endif


next y


display " " ~newline


next x


2. Rewrite the following for-loop as a pre-test while-loop. (6)


for x = 1 to 10


for y = 10 to x


if y MOD 2 = 0 then


display "*"


else


display "#"


endif


display " " ~newline


3. Rewrite the following for-loop as a post-test do-loop. (5)


for x = 20 to -20 step -3


display "x = ", x


next x


4. Identify an error in the following pseudocode: (2)


sum = 0


for x = 1 to 10


if y MOD 2 = 0


display y


sum = sum + y

1
Expert's answer
2022-03-29T18:34:37-0400

1

#
*#
#*#
*#*#
#*#*#
*#*#*#
#*#*#*#
*#*#*#*#
#*#*#*#*#
*#*#*#*#*#


2.

x = 1
while x <= 10
    y = 10
    while y >= x
        if y MOD 2 = 0 then
            display "*"
        else
            display "#"
        endif
        y = y - 1
    display " " ~newline
    x = x + 1


3.

x = 20
do
    display "x = ", x
    x = x - 3
while x > -20


4.

sum = 0

for x = 1 to 10
    if y MOD 2 = 0
        display y
    sum = sum + y

The wrong variable is used inside the loop.

Correct algorithm:

sum = 0

for x = 1 to 10
    if x MOD 2 = 0
        display x
    sum = sum + x

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS