Friday, 20 May 2016

Chapter 3 (Exercise Solutions)

1. In these solutions I am using * to represent a blank space.
(a)   ****86,1040

(b)   *3.02530e+01 (Actually this answer may vary for different compilers, some compilers may show the output as 3.02530e+001)

(c)   83.1620

(d)   Since this number is very small, the conversion specifier g will use exponential format to show the output. Moreover in %-6.2g, 2 shows maximum number of significant digits.
        1e-06* (This answer will also depend on compiler, some compilers will show the output as 1e-006)



2.(a)  printf("%-8.1e", x);

   (b)  printf("%10e", x);     (By default 6 digits are printed, so we do not need to explicitly mention it)

   (c)  printf("%-8.3f", x);

   (d)  printf("%6.0f.", x);     (By writing %6.0f we get no digits after decimal point and decimal point is also dropped. If we want to show decimal point we have to put a . after f as I've done)



3.(a)  Both are equivalent.

   (b)  Both are not equivalent. In the first format string, after reading first input from the user scanf will search for a minus sign(-) but if the user enters space scanf will return immediately. In the second scanf format string, after reading the first input if the user enters space scanf will skip through it and then matches the minus sign.

   (c)  Both are not equivalent. In the first format string, after reading a float value as the user presses Enter scanf will store that float value in the specified location and successfully returns. In the second format string after reading a float value scanf will not return unless user enters a non-whitespace character.

  (d)  Both are equivalent.



4. The values of i, x and j will be 10, 0.3 and 5 respectively.



5. The values of x, i and y will be 12.3, 45 and 0.6 respectively.



6.  /* Modified addfrac.c program */
     #include<stdio.h>
     int main(void)
     {
           int num1, denom1, num2, denom2, result_num, result_denom;
           printf("Enter first fraction: ");
           scanf("%d /%d", &num1, &denom1);
           printf("Enter second fraction: ");
           scanf("%d /%d", &num2, &denom2);
           result_num = num1 * denom2 + num2 * denom1;
           result_denom = denom1 * denom2;
           printf("The sum is %d/%d\n", result_num, result_denom);
           return 0;
     }
    

No comments:

Post a Comment