Chapter 5

I l @ ve RuBoard

Chapter 5

    1. 30

    2. 27 (not 3 ). (12 + 6)/(2*3) would give 3 .

    3. x = 1, y = 1 (integer division)

    4. x = 3 (integer division) and y = 9

    1. 6 ( reduces to 3 + 3.3 )

    2. 52

    3. (reduces to 0 * 22.0 )

    4. 13 (reduces to 66.0 / 5 or 13.2 , then assigned to int )

  1. Line 0:  

    Should include <stdio.h> .

    Line 3:  

    Should end in a semicolon, not a comma.

    Line 6:  

    The while statement sets up an infinite loop because the value of i remains 1 and is always less than 30 . Presumably we meant to write while(i++ < 8) .

    Lines 6 “8:  

    The indentation implies that we wanted lines 7 and 8 to form a block, but the lack of braces means that the while loop includes only line 7. Braces should be added.

    Line 7:  

    Because 1 and i are both integers, the result of the division will be 1 when i is 1 , and for all larger values. Using n = 1.0/i; would cause i to be converted to floating-point before division and would yield non-zero answers.

    Line 8:  

    We omitted a newline character ( \n ) in the control statement. This causes the numbers to be printed on one line, if possible.

    Line 10:  

    Should be return 0; .

    Here is a corrected version:

     #include <stdio.h> int main(void) {   int i = 1;   float n;   printf("Watch out! Here come a bunch of fractions!\n");   while (i++ < 30)   {     n = 1.0/i;     printf(" %f\n", n);   }   printf("That's all, folks!\n");   return 0; } 
  2. The main problem lies in the relationship between the test statement (is sec greater than 0?) and the scanf() statement that fetches the value of sec . In particular, the first time the test is made, the program hasn't had a chance to even get a value for sec , and the comparison will be made to some garbage value that happens to be at that memory location. One solution, albeit an inelegant one, is to initialize sec to, say, 1 so that the test is passed the first time through. This uncovers a second problem. When you finally type to halt the program, sec doesn't get checked until after the loop is finished, and the results for 0 seconds are printed out. What you really want is to have a scanf() statement just before the while test is made. You can accomplish that by altering the central part of the program to read this way:

     scanf("%d", &sec); while ( sec > 0 ) {   min = sec/S_TO_M;   left = sec % S_TO_M;   printf("%d sec is %d min, %d sec. \n", sec, min, left);   printf("Next input?\n");   scanf("%d", &sec);   } 

    The first time through, the scanf() outside the loop is used. Thereafter, the scanf() at the end of the loop (and hence just before the loop begins again) is used. This is a common method for handling problems of this sort .

  3. Here is the output:

     %s is a string  is a string 1 1 2 1 

    Let's explain. The first printf() statement is the same as this:

     printf("%s is a string\n","%s is a string\n"); 

    The second print statement first increments num to 1 and then prints the value. The third print statement prints num , which is 1 , and then increments it to 2 . The fourth print statement prints the current value of n , which still is 2 , and then decrements n to 1 . The final print statement prints the current value of n , which is 1 .

  4. Here is the output:

     DAD:3 3.00 

    The expression c1 - c2 has the same value as 'D' - 'A' , which in ASCII is 68 - 65 .

  5. It prints on one line the digits 1 through 10 in fields that are five columns wide and then starts a new line:

     1    2    3    4    5    6    7    8    9    10 
  6. Here is one possibility:

     #include <stdio.h> int main(void) {      char c = 'a';      while (c <= 'g')           printf("%5c", c++);      printf("\n");      return 0; } 
  7. Here is the output for each example:

    1. 1 2

      Note that x is incremented and then compared. The cursor is left on the same line.

    2.  101  102  103  104 

      Note that this time x is compared and then incremented. In both this case and in example a., x is incremented before printing takes place. Note, too, that indenting the second printf() statement does not make it part of the while loop. Therefore, it is called only once, after the while loop ends.

    3. stuvw

      Here, there is no incrementing until after the first printf() .

  8. This is an ill- constructed program. Because the while statement doesn't use braces, only the printf() statement is part of the loop, so the program prints the message COMPUTER BYTES DOG indefinitely until you can kill the program.

    1. x = x + 10;

    2. x++'; or ++x; or x = x + 1;

    3. c = 2 * (a + b);

    4. c = a + 2* b;

    1. x--'; or --x; or x = x - 1;

    2. m = n % k;

    3. p = q / (b - a);

    4. x = (a + b) / (c * d);

I l @ ve RuBoard


C++ Primer Plus
C Primer Plus (5th Edition)
ISBN: 0672326965
EAN: 2147483647
Year: 2000
Pages: 314
Authors: Stephen Prata

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net