Q2. The following algorithm doubles the numbers in 2,4,6,8. For this what should be the minimum value in box number 12.
(n) means box number n.
1 2 3 4 5 6 7 8 9 10 11 12
2
|
4
|
11
|
7
|
9
|
0
|
5
|
2
|
4
|
1
|
2
|
1
|
Step1: Initialization: Put the no in box (7) into box number 12.
Step2: Add: no in box (2) + no in box (2). Put the result in box (2).
Step3. Check: number in box number (12) leaves a remainder 1 when divided by 5.
Step4: Change : Increase box number in step 2 by 2.
Step 5: Multiply: Multiply no. in box(12) by 2.
Step6: Subtract: Decrease no. in box(12) by the no whose box no. is in box (3)
Solution
------------------------------------------------------------------------------------------------------------
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
|
11
|
7
|
9
|
0
|
5
|
2
|
4
|
1
|
2
|
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
|
11
|
7
|
9
|
0
|
5
|
2
|
4
|
1
|
2
|
1
|
Step 1:
Now initially after Step 1, 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
|
11
|
7
|
9
|
0
|
5
|
2
|
4
|
1
|
2
|
5
|
From here the iteration
starts until the number in box (12) leaves a remainder 1 when divided by 5. We can rewrite the condition as :
(A[12] % 5 == 0)
Iteration 1 :
Step 2 : Add the number in box (2) and number in
box (2) and put the result in box (2).
This is can be well understood as :
A[2]
= A[2] + A[2]
= 4 + 4
A[2]
= 8
Step 3 : Check if (A[12] % 5 ==
0) then EXIT.
Here the condition is not fulfilled hence the algorithm
controls move to Step 4.
A[12] % 5
= 5 % 5
= 0
Step 4 : Increase box number in step 2 by 2.
In this step, we are changing step 2.Now the box
number will be (2 + 2 ) = (4).
So the step 2 becomes
A[4]
= A[4] + A[4]
Step 5: Multiply number in box 12 by 2.
i.e. A[12] = A[12] * 2;
= 5 * 2
= 10
The Array 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
|
8
|
11
|
7
|
9
|
0
|
5
|
2
|
4
|
1
|
2
|
10
|
Step 6 : Decrease no. in
box(12) by the no whose box no. is in box (3) i.e.
A[12] = A[12] –
A [A[3]] = A[12] –
A[11]
= 10 – 2 = 8
The final 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
|
8
|
11
|
7
|
9
|
0
|
5
|
2
|
4
|
1
|
2
|
8
|
Iteration 2 :
Step 2 : A[4] = A[4]
+ A[4] = 7 + 7
A[4] = 14
Step 3 :. A[12] % 5 = 8
% 5 = 3
if (A[12] % 5 == 0) = FALSE
Hence the control moves to next step.
Step 4 : Increase box number in step 2 by 2.
In this step, we are changing step 2.Now the box
number will be (4 + 2 ) = (6).
So the step 2 becomes :
A[6] = A[6] +
A[6]
Step 5 : Multiply number in box 12 by 2.
i.e. A[12] = A[12] * 2;
= 8 * 2
= 16
The Array 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
|
8
|
11
|
14
|
9
|
0
|
5
|
2
|
4
|
1
|
2
|
16
|
Step 6 : Decrease no. in
box(12) by the no whose box no. is in box (3) i.e.
A[12] = A[12] –
A [A[3]] = A[12] –
A[11]
= 16 – 2 =
14
The final 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
|
8
|
11
|
14
|
9
|
0
|
5
|
2
|
4
|
1
|
2
|
14
|
Iteration 3 :
Step 2 : A[6] = A[6]
+ A[6] = 0 + 0
A[6] = 0
Step 3 :. A[12] % 5 = 14
% 5 = 4
if (A[12] % 5 == 0) = FALSE
Hence the control moves to next step.
Step 4 : Increase box number in step 2 by 2.
In this step, we are changing step 2.Now the box
number will be (6 + 2 ) = (8).
So the step 2 becomes :
A[8] = A[8] +
A[8]
Step 5 : Multiply number in box 12 by 2.
i.e. A[12] = A[12] * 2;
= 14 * 2
= 28
The Array 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
|
8
|
11
|
14
|
9
|
0
|
5
|
2
|
4
|
1
|
2
|
28
|
Step 6 : Decrease no. in
box(12) by the no whose box no. is in box (3) i.e.
A[12] = A[12] –
A [A[3]] = A[12] –
A[11]
= 28 – 2 = 26
The final 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
|
8
|
11
|
14
|
9
|
0
|
5
|
2
|
4
|
1
|
2
|
26
|
Iteration 4 :
Step 2 : A[8] = A[8]
+ A[8] = 2 + 2
A[8] = 4
Step 3 :. A[12] % 5 = 26
% 5 = 1
if (A[12] % 5 == 0) = TRUE
Hence the program exits..
So the final array
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
|
8
|
11
|
14
|
9
|
0
|
5
|
4
|
4
|
1
|
2
|
26
|
------------------------------------------------------------------------------------------------------------
Answer
Numbers
in box (12) = 26
------------------------------------------------------------------------------------------------------------