Chapter 6

I l @ ve RuBoard

Chapter 6

  1. 2, 7, 70, 64, 8, 2

  2. It would produce the following output:

     36 18  9  4  2  1 
    1. x > 5

    2. scanf("%lf",&x) != 1

    3. x == 5

    1. scanf("%d", &x) == 1

    2. x != 5

    3. x >= 20

  3. Line 4:  

    Should be list[10] .

    Line 6:  

    Commas should be semicolons.

    Line 6:  

    Range for i should be from 0 to 9, not 1 to 10.

    Line 9:  

    Commas should be semicolons.

    Line 9:  

    >= should be <= . Otherwise, when i is 1 , the loop never ends.

    Line 10:  

    There should be another closing brace between lines 9 and 10. One brace closes the compound statement, and one closes the program. In between should be a return 0; line.

    Here's a corrected version:

     #include <stdio.h> int main(void) {                                      /* line 3  */   int i, j, list[10];                  /* line 4  */   for (i = 0; i <  10;  i++)           /* line 6  */   {                                    /* line 7  */       list[i] = 2*i + 3;               /* line 8  */       for (j = 1; j <= i; j++)         /* line 9  */           printf(" %d", list[j]);      /* line 10 */       printf("\n");                    /* line 11 */   }   return 0; } 
  4. Here's one way:

     #include <stdio.h> int main(void) {    int col, row;    for (row = 1; row <= 4; row++)    {       for (col = 1; col <= 8; col++)          printf("$");       printf("\n");    }   return 0; } 
    1. It would produce the following output:

       Hi! Hi! Hi! Bye! Bye! Bye! Bye! Bye! 
    2. It would produce the following output:

       ACGM 
    1. It would produce the following output:

       Saddle up my char 
    2. It would produce the following output:

       Tbeemf!vq!nz!dibs 
    3. It would produce the following output:

       Saddle up my charg 
    4. It would produce the following output:

       $addle up my char 
  5. Here is the output we get:

     11121314 *** 1 4 7 *** 1 5 2 7 4 9 8 11 *** +++++ ++++ +++ ++ 
    1. mint

    2. 10 elements

    3. type double values

    4. Both i. and iii. are correct. In i., mint is the same as &mint[0] .

  6. Because the first element has index , the loop range should be 1 to SIZE - 1 , not 1 to SIZE . Making that change, however, causes the first element to be assigned the value instead of 2 . So rewrite the loop this way:

     for (index = 0; index < SIZE; index++)    by_twos[index] = 2 * (index + 1); 

    Similarly, the limits for the second loop should be changed. Also, an array index should be used with the array name :

     for (index = 0; index < SIZE; index++)      printf("%d ", by_twos[index]); 

    One dangerous aspect of bad loop limits is that the program may work; however, because it is placing data where it shouldn't, it might not work at some time in the future, forming sort of a programming time bomb.

  7. It should declare the return type as long , and it should have a return statement that returns a long value.

  8. Typecasting num to long makes sure the calculation is done as a long calculation, not an int calculation. On a system with a 16-bit int , multiplying two int s produces a result that is truncated to an int before the value is returned, possibly losing data.

     long square(int num) {    return ((long) num) * num; } 
  9. Here is the output:

     1: Hi! k = 1 k is 1 in the loop Now k is 3 k = 3 k is 3 in the loop Now k is 5 k = 5 k is 5 in the loop Now k is 7 k = 7 
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