due in class on +40
This assignment is an individual activity. It asks you to interpret and write algorithms using the pseudo-code notation we have studied in class.
Read the pseudo-code below and answer the questions that follow.
step 1. let Y be X + 5
step 2. if Y > 0 then set X to Y * 2
step 3. set X to X * 3
step 4. output X
Does this algorithm contain a conditional statement? If so, which one?
Does this algorithm contain iteration? If so, which steps repeat?
Trace the algorithm with the input X = 4. What does it output?
Trace the algorithm with the input X = -3. What does it output?
Trace the algorithm with the input X = -7. What does it output?
In this problem, you will write down an algorithm to add two-digit numbers in base ten. As shown in the figure, your input variables are X₁, X₀, Y₁, and Y₀. Each variable holds a single-digit integer.
When your algorithm is finished, the answer should be in the variables Z₂, Z₁, and Z₀ – each holding a single digit. For example, if I want to add 56+94, the algorithm will start with
X1 = 5 X0 = 6
Y1 = 9 Y0 = 4
and in the end, the Z variables will have the result:
Z2 = 1 Z1 = 5 Z0 = 0
Your algorithm should work for any single-digits provided in the input variables.
Here, C
refers to a sequence of variables (an array), and the notation C[I]
uses the value of I
to determine which C
to access.
step 1. set K to C[1]
step 2. set N to 1
step 3. set I to 2
step 4. if I > 7 then output N then K and stop
step 5. if C[I] = K then set N to N+1 and go to step 9
step 6. output N then K
step 7. set K to C[I]
step 8. set N to 1
step 9. set I to I+1
step 10. go back to step 4.
Below are the initial values of the array, and other variables you will use.
K :
N :
I :
C[1] : apple
C[2] : apple
C[3] : banana
C[4] : carrot
C[5] : carrot
C[6] : carrot
C[7] : date
What is the output of the algorithm?