Lab 8 Answer Sheet


Name: ________________________ Netid: _______________ Section: _______

Part 1

1. _____________________________________________________________



2. _____________________________________________________________



3. _____________________________________________________________



4. _____________________________________________________________



5. _____________________________________________________________



6. _____________________________________________________________



7. _____________________________________________________________



8. _____________________________________________________________


9. _____________________________________________________________



10. _________________________   ____________________________________



11. ________________________   _____________________________________


       _______________________________________________________________________


12. _____________________________________________________________





Part 2

1. Run your program and enter 100 as input. Write down the results your program displays.



___________________ ___________________ ___________________ ___________________ ___________________



2. Rewrite the program in the loop.c file using a do-while loop. Fill in the blanks below with your code. Try compiling and running both versions of the loop to make sure they behave exactly the same.

/* You may not need to use all the blanks below. */

  #include <stdio.h>
  #include <time.h>    /* library containing the "time" function */
  #include <stdlib.h>  /* library containing the "rand" function */
  void main(void) 
  {
     int N;
     int Total = 5;
     int counter = 1;
      
     printf("Enter a number: ");
     scanf("%i", &N);
        ________________________________________________________________________________ 
     
     
        ________________________________________________________________________________

        ________________________________________________________________________________

        ________________________________________________________________________________

        ________________________________________________________________________________
   printf("\n");
} 


3. Rewrite the program using a for loop. Fill in the blanks below with your code. Try compiling and running both versions of the loop to make sure they behave exactly the same.



/* You may not need to use all the blanks below. */

  #include <stdio.h>
  #include <time.h>    /* library containing the "time" function */
  #include <stdlib.h>  /* library containing the "rand" function */
  int main(void) 
  {
     int N;
     int Total = 5;
     int counter = 1;
    
     printf("Enter a number: ");
     scanf("%i", &N);



        ________________________________________________________________________________ 
     
    
        ________________________________________________________________________________

        ________________________________________________________________________________

        ________________________________________________________________________________

        ________________________________________________________________________________
  printf("\n");
} 



Part 3


4. Run the guessing game program three times once for 'e', 'm' and 'd', write down the computer generated 'target' (the mystery number to be guessed):

_____________ ____________________ _______________________

5. What is the best strategy to win the guessing game? That is, for a random number N of the form N = 2i, write down a strategy in which you need at most i+1 times to guess the target .
(For example, when n = 16 = 24 you would need at most 5 guesses to guess any target between 1 and 16.)


_________________________________________________________________________________________________________________________________



_________________________________________________________________________________________________________________________________

Part 4

6. Write the single C statement that you created for step 4.

________________________________________________________________