Sams Teach Yourself C in 21 Days

(singke) #1
Answers 841

F


Exercises

1.long array[50];


  1. Notice that in the following answer, the 50th element is indexed to 49. Remember
    that arrays start at 0.
    array[49] = 123.456;

  2. When the statement is complete,xequals 100.

  3. When the statement is complete,ctrequals 11. (ctrstarts at 2 and is incremented
    by 3 while it is less than 10 .)

  4. The inner loop prints five Xs. The outer loop prints the inner loop 10 times. This
    totals 50 Xs.

  5. The code is as follows:
    int x;
    for( x = 1; x <= 100; x += 3) ;

  6. The code is as follows:
    int x = 1;
    while( x <= 100 )
    x += 3;

  7. The code is as follows:
    int ctr = 1;
    do
    {
    ctr += 3;
    } while( ctr < 100 );

  8. This program never ends. recordis initialized to 0. The whileloop then checks to
    see whether recordis less than 100. 0 is less than 100 , so the loop executes, thus
    printing the two statements. The loop then checks the condition again. 0 is still,
    and always will be, less than 100 , so the loop continues. Within the brackets,
    recordneeds to be incremented. You should add the following line after the sec-
    ondprintf()function call:
    record++;

  9. Using a defined constant is common in looping; you’ll see examples similar to this
    code fragment in Weeks 2 and 3. The problem with this fragment is simple. The
    semicolon doesn’t belong at the end of the forstatement. This is a common bug.


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

Free download pdf