Chapter 7

I l @ ve RuBoard

Chapter 7

  1. True: b.

    1. number >= 1 && number < 9

    2. ch != 'q' && ch != 'k'

    3. (number >= 1 && number <= 9) && number != 5

    4. (number >= 1 && number <= 9) or else number < 1 number > 9

  2. Line 5:  

    Should be scanf("%d %d", &weight, &height); . Don't forget those & s for scanf() . Also, this line should be preceded by a line prompting input.

    Line 9:  

    What is meant is (height < 72 && height > 64) . However, the first part of the expression is unnecessary because height must be less than 72 for the else if to be reached in the first place. Therefore, a simple (height > 64) will serve.

    Line 11:  

    The condition is redundant; the second subexpression ( weight not less than or equal to 300) means the same as the first. A simple (weight > 300) is all that is needed. But there is more trouble. Line 11 gets attached to the wrong if ! Clearly, this else is meant to go along with line 6. By the most recent if rule, however, it will be associated with the if of line 9. Therefore, line 11 is reached when weight is less than 100 and height is 64 or under. This makes it impossible for weight to exceed 300 when this statement is reached.

    Lines 7 through 9:  

    Should be enclosed in braces. Then line 11 will become an alternative to line 6, not to line 9.

    Line 12:  

    Simplify to if (height > 48) .

    Line 14:  

    This else associates with the last if , the one on line 12. Enclose lines 12 and 13 in braces to force this else to associate with the if of line 11. Note that the final message is printed only for those weighing between 100 and 300 pounds .

    Here's a corrected version:

     #include <stdio.h> int main(void)                                      /* 1  */ {                                                   /* 2  */   int weight, height;  /* weight in lbs, height in inches */                                                     /* 4  */   printf("Enter your weight in pounds and ");   printf("your height in inches.\n");   scanf("%d %d", &weight, &height);         /* 5  */   if (weight < 100)                              /* 6  */   {      if (height >= 72)                           /* 7  */         printf("You are very tall for your weight.\n");      else if (height > 64)                       /* 9  */         printf("You are tall for your weight.\n");   }   else if (weight > 300)                         /* 11 */   {      if (height < 48)                            /* 12 */          printf(" You are quite short for your weight.\n");   }   else                                              /* 14 */      printf("Your weight is ideal.\n");             /* 15 */                                                     /* 16 */   return 0; } 
  3. 1 . The assertion is true, which numerically is a 1 .

    . 3 is not less than 2.

    1 . If the first expression is false, the second is true, and vice versa; just one true expression is needed.

    6 , because the value of 6 > 2 is 1 .

    10 , because the test condition is true.

    . If x > y is true, the value of the expression is y > x , which is false in that case, or . If x > y is false, the value of the expression is x > y , which is false in that case.

  4. The program prints the following:

     *#%*#%$#%*#%*#%$#%*#%*#%$#%*#%*#% 

    Despite what the indentation suggests, the # is printed during every loop because it is not part of a compound statement.

  5. The program prints the following:

     MerryMerrMerOh no! MerrMerOh no! MerOh no! 
  6. The comments on lines 5 through 7 should be terminated with */ . The expression 'a' <= ch >= 'z' should be replaced with this:

     ch >= 'a' && ch <= 'z' 

    Or, more simply and more portably, include ctype .h and use islower () . Incidentally, 'a' <= ch >= 'z' is valid C; it just doesn't have the right meaning. Because relational operators associate left to right, the expression is interpreted as ('a' <= ch) >= 'z' . The expression in parentheses has the value 1 or (true or false), and this value is checked to see whether it is equal to or greater than the numeric code for 'z' . Neither nor 1 satisfies that test, so the whole expression always evaluates to (false). In the second test expression, should be && . Also, although !(ch < 'A') is both valid and correct in meaning, ch >= 'A' is simpler. The 'Z' should be followed by two closing parentheses, not one. Again, more simply, use isupper () . The oc++; statement should be preceded by an else . Otherwise, it is incremented every character. The control expression in the printf() call should be enclosed in double quotes.

    Here is a corrected version:

     #include <stdio.h> #include <ctype.h> int main(void) {   char ch;   int lc = 0;    /* lowercase char count */   int uc = 0;    /* uppercase char count */   int oc = 0;    /* other char count     */   while ((ch = getchar()) != '#')   {        if (islower(ch))             lc++;        else if (isupper(ch))             uc++;        else             oc++;   }   printf("%d lowercase, %d uppercase, %d other", lc, uc, oc);   return 0; } 
  7. Unhappily, it prints the same line indefinitely:

     You are 65. Here is your gold watch. 

    The problem is that the line

     if (age = 65) 

    sets age to 65 , which tests as true, every loop cycle.

  8. Here is the resulting run using the given input:

     q Step 1 Step 2 Step 3 c Step 1 g Step 1 Step 3 b Step 1 Done 

    Note that both b and # terminate the loop, but that entering b elicits the printing of Step 1, and entering # doesn't.

  9. Here is one solution:

     #include <stdio.h> int main(void) {   char ch;   while ((ch = getchar()) != '#')   {     if (ch != '\n')     {        printf("Step 1\n");        if (ch == 'b')           break;        else if (ch != 'c')        {           if (ch != 'g')                printf("Step 2\n");            printf("Step 3\n");        }     }   }   printf("Done\n");   return 0; } 
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