CS101 Prelab 8

Name: ______________________ NetId: ___________________ Section: _____


First of all, this lab requires you to use some math functions. Don't forget to use the -lm option when compiling your code.

Fill in the blanks to answer the following questions. 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).

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

    #include <stdio.h> 
    void main(void)
    { 
      int i = 17.25; /* 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"); 
    }
    _______________________________ 
     
  2. The following C program compiles without errors. Write the output produced by this program.

  3. #include <stdio.h> 
    void main(void)
    { 
       int x ; 
       if (0)              /* 0 means FALSE */
          printf("False ");
       if (33)              /* any non-zero value in C means TRUE */
          printf("True ");
       x = 0;
       if (x == -1) 
         printf("T "); 
       else 
        if (x != 0)       /* != means "not equal" */
          printf("U "); 
        else 
        printf("Y ");
       if (x == -1);      /* BAD place for a ; as discussed in lecture */
           printf("(';' Are not good after ifs) ");
       if (x = 0)        /* oops!, forgot == */
          printf("W ");
       printf("\n");
    } 
    _________________________________________________________________________
     
  4. The following C program compiles without errors. Write the output produced by this program.

    #include <stdio.h>

    void main(void)
    { 
  5.    	int x = 8; 
       	int y = 3; 
       	if ((x < 2) || ( y > 3))   /* || means OR */
         	printf("L "); 
      
       	if ((x > 7) && (y < 4))    /* && means AND */
        		printf("M "); 
       	else 
        		printf("N ");
       	if ( !(x < 10))            /* ! means NOT */
             printf("O ");
       	printf("\n");
    }

    _____________________________________________________


  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)    /* while condition is TRUE execute code in block { } */
       { 
            y = y - 2;
       } 
       printf("y = %i ",y);
     
       printf("\n");
       
    } 
    _________________________________________________________________________ 
     
        
  7. 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 = 56; 
       while (y >= 0)    /* how many statements below are part of the while loop? */
         y = y - 1;
         printf("T ");
     
       printf("\n");
       
    }  
    _________________________________________________________________________ 
  8. 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("N ");
      }
       printf("\n");
     } 
    ________________________________________________________________________
     
  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=1; i < 6; i=i+2) 
       { 
        printf("%i ",i);  
       } 
       printf("\n"); 
    } 
        
    _______________________________________________________________________ 
  10. The following C program compiles without errors. Write the output produced by this program.

    #include <stdio.h> 
    void main(void)
    { 
       int i, j; 
       for(i=0; i < 4; i=(2*i)+1) 
          for(j=i; j < 2; j = j + 1) 
               printf("<%i,%i> ",i,j);   
     } 
        _____________________________________________________________ 
  11. The following code fragment is placed in a complete C program compiled (without errors) and run. Will the following loop terminate? (Yes/ No)

    ______________________________________________________________
    Note: A loop that does not terminate is called an infinite loop.

       int a=8; 
       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; 
       } 
  12. The following C program compiles without errors. Write the output produced by this program.

    #include <stdio.h>
     
    void main(void)
    { 
       int i = 3; 
       do 
       { 
         printf("%i ",i-1); 
         i = i - 2; 
       } while( i > 0); 
       printf("\n");
    } 

    ______________ _______________________________________

  13. Complete the following loop to print all the odd numbers from 1 to 99 inclusive as follows:

    1 3 5 7 9 ... 99

      int i;
      for(i = 1; _____________________;  ________________) 
      {  
        
          printf("%i ", ___________________ );
      }




  14. 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.

    #include <stdio.h>
    #include <math.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 equals y ");
    else if ( fabs(x - y) < 1.0e-6)
    printf(" x is close to y ");
    else if (fabs(x-y) < 1.0e-20)
    printf(" x is very close to y ");
    else
    printf(" x is not even close to y! ");

    printf("\n");
    }


    ________________________________________________________________________________________________