Sunday, 22 May 2016

Chapter 3 (Programming Projects)

1. #include<stdio.h>
    int main(void)
    {
        int d,m,y;
        printf("Enter a date (mm/dd/yyyy): ");
        scanf("%d/%d/%d", &m, &d, &y);
        printf("%d%.2d%.2d", y, m, d);
        return 0;
    }



2. 

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;
     }
    

Tuesday, 17 May 2016

Chapter 2 (Exercise Solutions)

1. Yes, a warning message is generated by the compiler because main() is not returning a value. Add return 0; at the end of the main() function to remove the error.



2. (a) #include<stdio.h>
    (b) Parkinson's Law:
          Work expands so as to fill the time
          available for its completion.



3. #include<stdio.h>
    int main(void)
    {
       int height=8, length=12, width=10, volume;
       volume = height * length * width;
       printf("Dimensions: %dx%dx%d\n", length, width, height);
       printf("Volume (cubic inches): %d\n",volume);
       printf("Dimensional weight (pounds): %d\n", (volume + 165)/166);
       return 0;
    }



4.  #include<stdio.h>
     int main(void)
     {
         int a,b,c,d,e;
         float f,g,h,i,j;
         printf("%d   %d   %d   %d   %d   %f   %f   %f   %f   %f", a,b,c,d,e,f,g,h,i,j);
         return 0;
     }

No all the values printed have no pattern among them. They are just garbage values.



5. 100bottles is not a legal identifier because identifiers should start with a letter or an underscore.



6. Underscores are added in identifiers just for legibility so that we can separate two words. Putting more than one adjacent underscore may create a burden to the programmer. If same identifier repeats at many places in a program you have to use same number of underscores every time in that identifier (If by mistake one identifier is missed, it will cause error(s) in the program).



7. for & while are keywords in C.



8. There are 14 tokens in this statement:

Identifiers: 4 which are        answer   q   p   p
Operators: 5  which are        =   *   -   *   /
Constants: 2 which are         3   3
Punctuation: 3 which are      (    )    ;



9. answer = ( 3 * q - p * p ) / 3 ;



10. Spaces which are essential are:
* int main(void)      (Between int and main(void)
* int height              (During declaration of variables between int and height)


  

Chapter 2 (Programming Projects)

1. #include<stdio.h>
    int main(void)
    {
        printf("       *\n      *\n     *\n*   *\n * *\n  *");
        return 0;
    }



2. #include<stdio.h>
    #define PI 3.14159
    int main(void)
   {
       int r = 10;
       float v;
       v = 4.0f / 3.0f * PI * r * r * r;
       printf("Volume of sphere = %f", v);
       return 0;
   }

If we write 4.0f/3.0f as 4/3 then it results in integer number 1, because when both operands in a divison operation are integers fractional part is truncated.



3. #include<stdio.h>
    #define PI 3.14159
    int main(void)
   {
       int r;
       float v;
       printf("Enter the radius of the sphere: ");
       scanf("%d", &r);
       v = 4.0f / 3.0f * PI * r * r * r;
       printf("Volume of sphere = %f", v);
       return 0;
   }



4. #include<stdio.h>
    int main(void)
    {
        float amount;
        printf("Enter an amount: ");
        scanf("%f", &amount);
        amount = amount + 0.05 * amount;
        printf("With tax added: %.2f", amount);
        return 0;
    }



5. #include<stdio.h>
    int main(void)
   {
        int x;
        printf("Enter value of x: ");
        scanf("%d", &x);
        printf("%d", 3*x*x*x*x*x + 2*x*x*x*x - 5*x*x*x - x*x + 7*x - 6);
        return 0;
   }



6. #include<stdio.h>
    int main(void)
   {
        int x;
        printf("Enter value of x: ");
        scanf("%d", &x);
        printf("%d", ((((3*x + 2)*x- 5)*x -1) *x + 7)*x - 6);
        return 0;
   }



7. #include<stdio.h>
    int main(void)
   {
       int amt;
       printf("Enter a US dollar amount: ");
       scanf("%d", &amt);
       printf("$20 bills: %d\n", amt/20);
       amt = amt - 20*(amt/20);
       printf("$10 bills: %d\n", amt/10);
       amt = amt - 10*(amt/10);
       printf("$5 bills: %d\n", amt/5);
       amt = amt - 5*(amt/5);
       printf("$1 bills: %d", amt);
       return 0;
    }



8. #include<stdio.h>
    int main(void)
    {
        float amount, rate, payment;
        printf("Enter amount of loan: ");
        scanf("%f", &amount);
        printf("Enter interest rate: ");
        scanf("%f", &rate);
        printf("Enter monthly payment: ");
        scanf("%f", &payment);
        amount = amount - payment + amount*(rate/100/12);
        printf("Balance remaining after first payment: $%.2f\n", amount);
        amount = amount - payment + amount*(rate/100/12);
        printf("Balance remaining after second payment: $%.2f\n", amount);
        amount = amount - payment + amount*(rate/100/12);
        printf("Balance remaining after third payment: $%.2f", amount);
        return 0;
     }


Tuesday, 10 May 2016

Programming Project 12

#include<stdio.h>
int main(void)
{
    int i,n,j,prod;
    float e=0.0f,E;
    printf("Enter a small floating point number: ");
    scanf("%f",&E);
    for(i=1;;i++)
    {
        for(j=1,prod=1;j<=i;j++)
            prod=prod*j;
        if(1.0f/prod<E)
            break;
        e=e+(1.0f/prod);
    }
    printf("e: %f",e+1);
    return 0;
}

Programming Project 11

#include<stdio.h>
int main(void)
{
    int i,n,j,prod;
    float e=0.0f;
    printf("Enter an integer: ");
    scanf("%d",&n);
    for(i=1;i<=n;i++)
    {
        for(j=1,prod=1;j<=i;j++)
            prod=prod*j;

        e=e+(1.0f/prod);
    }
    printf("e: %f",e+1);
    return 0;
}

Programming Project 2

#include<stdio.h>
int main(void)
{
    int h,m;
    printf("Enter a 24-hour time: ");
    scanf("%d:%d",&h,&m);

    if(h<12&&h>=1)
    {
        printf("Equivalent 12-hour time: %d:%.2d AM",h,m);
        return 0;
    }
    if(h==12)
    {
        printf("Equivalent 12-hour time: %d:%.2d PM",h,m);
        return 0;
    }
    if(h==0)
    {
        printf("Equivalent 12-hour time: %.2d:%.2d AM",h,m);
        return 0;
    }
    if(h>12)
        printf("Equivalent 12-hour time: %d:%.2d PM",h-12,m);
        return 0;
}