Sams Teach Yourself C in 21 Days

(singke) #1
Answers 835

F


Exercises


  1. This listing should have worked, even though it is poorly structured. The purpose
    of this listing is to demonstrate that whitespace is irrelevant to how the program
    runs. You should use whitespace to make your programs readable.

  2. The following is a better way to structure the listing from exercise 1:
    #include <stdio.h>
    int x, y;
    int main( void )
    {
    printf(“\nEnter two numbers “);
    scanf( “%d %d”,&x,&y);
    printf(“\n\n%d is bigger\n”,(x>y)?x:y);
    return 0;
    }
    This listing asks for two numbers,xandy, and then prints whichever one is bigger.

  3. The only changes needed in Listing 4.1 are the following:
    16: printf(“\n%d %d”, a++, ++b);
    17: printf(“\n%d %d”, a++, ++b);
    18: printf(“\n%d %d”, a++, ++b);
    19: printf(“\n%d %d”, a++, ++b);
    20: printf(“\n%d %d”, a++, ++b);

  4. The following code fragment is just one of many possible answers. It checks to see
    ifxis greater than or equal to 1 and less than or equal to 20. If these two condi-
    tions are met,xis assigned to y. If these conditions are not met,xis not assigned
    toy; therefore,yremains the same.
    if ((x >= 1) && (x <= 20))
    y = x;

  5. The code is as follows:
    y = ((x >= 1) && (x <= 20))? x : y;
    Again, if the statement is TRUE,xis assigned to y; otherwise,yis assigned to itself,
    thus having no effect.

  6. The code is as follows:
    if (x < 1 && x > 10 )
    statement;

  7. a. 7
    b. 0
    c. 9
    d. 1 (true)
    e. 5


49 448201x-APP F 8/13/02 11:22 AM Page 835

Free download pdf