Solution to Assignment 2

1. A possible program is here. To test the program, I chose the following values and compared the obtained results with hand-calculations:
x=813, b=5, result=11223
x=17, b=16, result=1,1.
x=17, b=16, result=1,15.
x=0, b=0, "Expecting b>=1, received 0".
x=-1, "Expecting x>=0, received -1".
x=0, b=5, result=0.

The corresponding script file is here.

2. Tracing the code (Assuming the value k=5 was entered).

10     printf("Enter the value of k=");
11     scanf("%d", &k);
12     i=0;   /* counter of iterations*/

13     while (k!=1)
14     {
15        printf("i=%d, k=%d\n",i, k);  /* printing current values of i and k */

16        /* Computing new value of k (Collatz map) */
17        if (k%2==0)
18           k=k/2;
19        else
20           k=3*k+1;

21        i++; /* Incrementing counter */
22     }

23     printf("Done: i=%d, k=%d\n",i, k); /* printing final values of i and k */
    line #  of|  i |  k  | true/false cond. / Output 
     the code |    |	 |     / Comments
    -------------------------------------------------
       10     |  ? |  ?  | print: "Enter the value of k="
    -------------------------------------------------
       11     |  ? |  5  | print: "5"   (entered by user)
    -------------------------------------------------
       12     |  0 |  5  | 
    -------------------------------------------------
       13     |  0 |  5	 |  loop entered, (5!=1): true 
    -------------------------------------------------
       15     |  0 |  5  | print: "i=0, k=5"
    -------------------------------------------------
       17     |  0 |  5  |  5%2=1, (1==0): false, jump to 20.
    -------------------------------------------------
       20     |  0 |  16 |  3*5+1=16
    -------------------------------------------------
       21     |  1 |  16 |  0+1=1
    --------------------------------------------------------
       13     |  1 |  16 |  (16!=1): true 
    -------------------------------------------------
       15     |  1 |  16 | print: "i=1, k=16"
    -------------------------------------------------
       17     |  1 |  16 |  16%2=0, (0==0): true
    -------------------------------------------------
       18     |  1 |  8  |  16/2=8
    -------------------------------------------------
       21     |  2 |  8  |  1+1=2
    --------------------------------------------------------
       13     |  2 |  8  |  (8!=1): true 
    -------------------------------------------------
       15     |  2 |  8  | print: "i=2, k=8"
    -------------------------------------------------
       17     |  2 |  8  |  8%2=0, (0==0): true
    -------------------------------------------------
       18     |  2 |  8  |  8/2=4
    -------------------------------------------------
       21     |  3 |  4  |  2+1=3
    --------------------------------------------------------
       13     |  3 |  4  |  (4!=1): true 
    -------------------------------------------------
       15     |  3 |  4  | print: "i=3, k=4"
    -------------------------------------------------
       17     |  3 |  4  |  4%2=0, (0==0): true
    -------------------------------------------------
       18     |  3 |  4  |  4/2=2
    -------------------------------------------------
       21     |  4 |  2  |  3+1=4
    --------------------------------------------------------
       13     |  4 |  2  |  (2!=1): true 
    -------------------------------------------------
       15     |  4 |  2  | print: "i=4, k=2"
    -------------------------------------------------
       17     |  4 |  2  |  2%2=0, (0==0): true
    -------------------------------------------------
       18     |  4 |  2  |  2/1=1
    -------------------------------------------------
       21     |  5 |  1  |  4+1=5
    --------------------------------------------------------
       13     |  5 |  1  |  (1!=1): false, loop exited
    -------------------------------------------------
       23     |  5 |  1  | print: "Done: i=5, k=1"
    

3. (a)    10.0+15/2+4.3 = 10.0+ 7(int) +4.3 = 20.3 (double)

(b)    10.0+15%2+4.3 = 10.0 + 1 +4.3 = 15.3 (double)

(g)    20.0-2/6+3 = 20.0 - 0 + 3 = 23.0 (double)

(i)    10+17%3+4. = 10(int)+2(int)+4.0(double)=16.0 (double)

(j)    10+17/3.+4 = 10(int)+5.66667(double) + 4 (int)=19.66667 (double)
(Comment: 3. unlike 3 (no dot) is a floating point value.)

A program that checks these values and prints their size in bytes is here and its output is here.