Q1. The following algorithm adds the numbers in box no. 1,2,3,4,5 and puts the sum in box number 8. For this to happen what
should be the minimum value in box number 11.
where (n) means box number n.
Step1: Initialization: Put 0 in box number (8).
Step1: Initialization: Put 0 in box number (8).
Step2: Put 0 in box number (11).
Step3. Add: Add number in box (8) and no. in box (1) and put the result in box (8).
Step4: Add: no. in box (11) +2 and put the sum in box number (11).
Step 5: Check if no. in box (11) > 0 & it is divisible by 5
Step6:Change instruction 3: Increase the second box number in step 2 by 1.
1 2 3 4 5 6 7 8 9 10 11 12
2
|
4
|
3
|
7
|
9
|
0
|
1
|
2
|
4
|
1
|
1
|
Solution :
These types of problems can be easily solved by following the algorithm steps .
The initial values in the boxes are :
1 2 3 4 5 6 7 8 9 10 11 12
2
|
4
|
3
|
7
|
9
|
0
|
1
|
2
|
4
|
1
|
1
|
We can consider the collection of boxes as an array. So the contents of the array are :
A[1] A[2] A[3] A[4] A[5] A[6] A[7] A[8] A[9] A[10] A[11] A[12]
2
|
4
|
3
|
7
|
9
|
0
|
1
|
2
|
4
|
1
|
1
|
Now initially after Step 1 & Step 2, the values in the array are :
A[1] A[2] A[3] A[4] A[5] A[6] A[7] A[8] A[9] A[10] A[11] A[12]
2
|
4
|
3
|
7
|
9
|
0
|
1
|
0
|
4
|
1
|
0
|
1
|
From here the iteration starts until the number in box (11) > 0 & it is divisible by 5. We can rewrite the condition as :
(A[11] > 0) && (A[11] % 5 == 0)
Iteration 1 :
Step 3 : Add the number in box (8) and number in box (1) and put the result in box (8).
This is can be well understood as :
A[8] = A[8] + A[1]
= 0 + 2
A[8] = 2
Step 4 : Add number in box (11) and 2 and put the sum in box (11).
i.e. A[11] = A[11] + 2
= 0 + 2
A[11] = 2
Step 5 : Check if (A[11] > 0) && (A[11] % 5 == 0) then EXIT.
Here the condition is not fulfilled hence the algorithm controls move to Step 6.
Step 6 : Increase the second box number in step 2 by 1.
In this step, we are changing step 3.Now the second box number will be (2).
So the step 3 becomes A[8] = A[8] + A[2].
The array after iteration 1 becomes :
A[1] A[2] A[3] A[4] A[5] A[6] A[7] A[8] A[9] A[10] A[11] A[12]
2
|
4
|
3
|
7
|
9
|
0
|
1
|
2
|
4
|
1
|
2
|
1
|
Iteration 2 :
Step 3 : A[8] = A[8] + A[2]
A[8] = 6
Step 4 :. A[11] = A[11] + 2
A[11] = 4
Step 5 : if (A[11] > 0) && (A[11] % 5 == 0) = FALSE
Hence the control moves to next step.
Step 6 : Now the second box number will be (3).
Step 3 becomes A[8] = A[8] + A[3].
The array after iteration 2 is :
A[1] A[2] A[3] A[4] A[5] A[6] A[7] A[8] A[9] A[10] A[11] A[12]
2
|
4
|
3
|
7
|
9
|
0
|
1
|
6
|
4
|
1
|
4
|
1
|
Iteration 3 :
Step 3 : A[8] = A[8] + A[3]
A[8] = 9
Step 4 : A[11] = A[11] + 2
A[11] = 6
Step 5 : if (A[11] > 0) && (A[11] % 5 == 0) = FALSE.
Hence the control moves to next step.
Step 6 : Now the second box number will be (4).
Step 3 becomes A[8] = A[8] + A[4].
The array after iteration 3 is:
A[1] A[2] A[3] A[4] A[5] A[6] A[7] A[8] A[9] A[10] A[11] A[12]
2
|
4
|
3
|
7
|
9
|
0
|
1
|
9
|
4
|
1
|
6
|
1
|
Iteration 4 :
Step 3 : A[8] = A[8] + A[4]
A[8] = 16
Step 4 : A[11] = A[11] + 2
A[11] = 8
Step 5 : if (A[11] > 0) && (A[11] % 5 == 0) = FALSE.
Hence the control moves to next step.
Step 6 : Now the second box number will be (5).
Step 3 becomes A[8] = A[8] + A[5].
The array after iteration 4 is :
A[1] A[2] A[3] A[4] A[5] A[6] A[7] A[8] A[9] A[10] A[11] A[12]
2
|
4
|
3
|
7
|
9
|
0
|
1
|
16
|
4
|
1
|
8
|
1
|
Iteration 5 :
Step 3 : A[8] = A[8] + A[5]
A[8] = 25
Step 4 : A[11] = A[11] + 2
A[11] = 10
Step 5 : Check if (A[11] > 0) && (A[11] % 5 == 0) = TRUE.
Now the iterations stops and the algorithm stops.
The final array after iteration 5 becomes :
A[1] A[2] A[3] A[4] A[5] A[6] A[7] A[8] A[9] A[10] A[11] A[12]
2
|
4
|
3
|
7
|
9
|
0
|
1
|
25
|
4
|
1
|
10
|
1
|
------------------------------------------------------------------------------------------------------------
Answer
Numbers in box (8) = 25
Number in box (11) = 10