CS101 Prelab 8 - solutions

Name: ______________________     NetId: ___________________      Section: _____
 

Print out this page and fill in the blanks to answer the following questions.  Hand in this assignment at the beginning of Lab 8. You should use the material found in Lecture 12 and 13/14 of the course notes and/or the course C book Chapters 3.6 ,  6.3 & 3.7 - 3.10, 7.5
As with all prelabs/labs, if you are not sure of the answer you may go to any EWS lab on campus and check your results by using the gcc compiler, or you can ask anyone of the CS101 staff for help.

The following exercises should help prepare you for the in-lab activities and MP2.
You will use the C selection statements (if and if-else) along with C loop statements (while, do-while,for). 

Each problem is worth 1/4 point.

  1. The following C program compiles without errors. Write the output produced by this program.
    #include <stdio.h> 
    void main(void)
    {
    int i = 13.99; /* i holds ONLY integer values */

    double x;


    printf(" %i ", i );

    x =  8 / 3; /* 8 and 3 are integers */
    printf("%lf ",x);

    x = 8 / 3.0; /* 8 is an integer 3.0 is a real (double) */
    printf("%lf ",x);

    x = 8.0 / 3;
    printf("%lf ",x);

    x = 8.0/3.0;
    printf("%lf ",x);

    printf("\n");
    }



    ____________ 13 2.000000 2.666667 2.666667 2.666667________(no deduction for 2.66 or 2.666 or ...)_______________  


  2. The following C program compiles without errors. Write the output produced by this program.
    #include <stdio.h> 
    void main(void)
    {
    int x ;

    if (5) /* any non-zero value in C means TRUE */
    printf("A ");


    if (0) /* 0 means FALSE */
    printf("B ");


    x = 0;

    if (x == -1)
      printf("C ");
    else
      
       if (x != 0) /* != means "not equal" */
         printf("D ");
       else
          printf("E ");


    if (x == -1); /* BAD place for a ; as discussed in lecture */
    printf("F ");


    if (x = 0) /* oops!, forgot == */
    printf("G ");


    printf("\n");

    }  


    ________A_______E________F__________________________________________________


  3.   The following C program compiles without errors. Write the output produced by this program.

    #include <stdio.h>

    void main(void)
    {
    int x = 5;
    int y = 4;

    if ((x < 1) || ( y > 3)) /* || means OR */
      printf("H ");

    if ((x > 4) && (y < 3)) /* && means AND */
          printf("I ");
    else
          printf("J ");


    if ( !(x < 2)) /* ! means NOT */
    printf("K ");


    printf("\n");
    }

    _______H______J______K_______________________________________________
     

  4. The following C program compiles without errors (Note: there are no typos). Write the output produced by this program.
    #include <stdio.h> 
    void main(void)
    {
    int y = 2;
       while (y > 0)    /* while condition is TRUE execute code in block { } */
    {
    y = y - 1;
    }
    printf("y = %i ",y);

    printf("\n");
     
    }


    ________y = 0_________________________________________________________________
     
  5. The following C program compiles without errors (Note: there are no typos). Write the output produced by this program.
    #include <stdio.h> 
    void main(void)
    {
    int y = 2;
       while (y > 0)    /* how many statements below are part of the while loop? */
    y = y - 1;
    printf("M ");

    printf("\n");
      
    }


    __________M_______________________________________________________________


  6. The following C program compiles without errors (Note: there are no typos). Write the output produced by this program.
    #include <stdio.h> 
    void main(void)
    {
    int y = 3;
       while (y > 0)    /* how many statements below are part of the while loop? */
    {
    y = y - 1;
    printf("N "); 
     }

    printf("\n");
    }


    ______N______N_______N____________________________________________________

  7. The following C program compiles without errors. Write the output produced by this program.
    #include <stdio.h> 
    void main(void)
    {
    int i;
       for(i=0; i < 3; i=i+1) 
    {
       printf("%i ",i);  
    }

    printf("\n");
    }



    ______0_______1________2___________________________________________________
  8.  

  9. The following C program compiles without errors. Write the output produced by this program.
    #include <stdio.h> 
    void main(void)
    {
    int i;
       for(i=0; i < 4; i=(2*i)+1) 
    {
       printf("%i ",i);
    }

    printf("\n");
    }

    _______0____1_____3_____________________________________________
     

  10. The following code fragment is placed in a complete C program compiled (without errors) and run. Will the following loop terminate?  (Y/ N)

    ____________Y__________________________________________________
    Note: A loop that does not terminate is called an infinite loop.
       int a=2; 
    int b=8;
    while(1) /* 0 means False any other value means True */
    {
        if(a > b)
          break; /* the break statement "breaks out" of loops and switch statements */
       if (a == 100)
          a = 1;
       else /* else matches with the closest if (unless {} are used) */      
    b = b - 1;
    }

  11. The following C program compiles without errors. Write the output produced by this program.
    #include <stdio.h>

    void main(void)
    {
    int i = -1;
       do 
    {
          printf("%i ",i);
        i = i - 2;
    } while( i > 0);

    printf("\n");
    }

    ____________-1________________________________________________
     

  12. Complete the following loop to print all the even numbers from 0 to 100 inclusive as follows:

      0  2  4  6  8 ... 100

      int i;
    for(i = 0; ______i <= 100_____or i < 101__________; __i = i+2 but NOT_i=2*i_____________)
    {

    printf("%i ", __________i_________ );
    }



  13. The following C program compiles without errors. Write the output produced by this program.
    Note that abs is used for integer values but fabs is used for values of data type float or double.

  14. #include <stdio.h>

    void main(void)
    {
      float x = 1.0/3.0;     /* float and double data types hold real numbers. double holds more digits than float */
      double y = 1.0/3.0;

      if (x == y)
          printf(" x = y ");
      else
             if ( fabs(x - y) < 1.0e-6)
                    printf(" x close to y ");

      if (fabs(x-y) < 1.0e-15)
                    printf(" x very close to y ");

      printf("\n");   
    }


    ___________ x close to y ____________________________________________