Instructions: The first three problems ask you to read a Pseudocode algorithm. Trace through each of them by hand, keeping track of the variables, to determine the answer to the question.
Problem Solution Set Var to 100 Set N to 3 Set Var to Var - N If Var < 97 then do: Set Var to 0 else-do: Set Var to 1 Var N Var < 97 ------------------ 100 3 97 3 false 1 So the answer is Var = 1 after the algorithm runs
Problem Solution Set Var to 1 Set N to 0 While Var < 16 do: Set Var to Var * 2 Set N to N + 1 Var N Var < 16 ------------------ 1 0 true 2 1 true 4 2 true 8 3 true 16 4 false So the answer is Var = 16 when the loop finishes
Problem Solution # Comment: A mystery procedure in Pseudocode To DoSomethingInteresting Set Var to 0 Set N to 1 While N < 10 do: Set Var to Var + N Set N to N + 2Var N N < 10 ------------------ 0 1 true 1 3 true 4 5 true 9 7 true 16 9 true 25 11 false So Var = 25 after this procedure is called. The procedure computes the sum of the odd numbers between 1 and 10
Instructions: The next three problems ask you to write a Pseudocode
algorithm. Model your answers after the Pseudocode algorithms in the first there questions.
# Comment: An algorithm that adds A, B, and C giving Var Set Var to 0 Set Var to Var + A or Set Var to A + B + C Set Var to Var + B Set Var to Var + C
# Comment: Sets Var to 1 if M=N or to 0 of M <> N
If M = N then do:
Var = 1
else=do:
Var = 0
Algorithm Trace # Comment: Multiplies Var by itself N times. # It is not necessary to know the value of Var, but here we let Var be 2. # And here we need a third variable to store the result. Call it Product. Set Var to 2 Set Product to 1 Set N to 1 While N <= 10 do: Set Product = Product * Var Set N to N + 1 Var Product N N <= 10 -------------------------------- 2 1 1 true 2 2 2 true 2 4 3 true 2 8 4 true 2 16 5 true ... ... ... ... 2 512 10 true 2 1024 11 false