Lab 8 Answer Sheet

This lab is to be worked in a group.


Name: ________________________     Netid: _______________    Section: _______

Name: ________________________     Netid: _______________    Section: _______

Name: ________________________     Netid: _______________    Section: _______
 

Each problem worth 1 point except Part I #1 worth 2 points.

Part I


1. Rewrite the program 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 = 10;
int counter = 1;



printf("Enter a number: ");
scanf("%i", &N);


________do________________________________________________________________________


	_________{_______________________________________________________________________



	_____________printf(" %i ", rand()%(N+1));________________________________________



	_____________counter = counter + 1;_______________________________________________



	________} while(counter <= Total);________________________________________________

printf("\n");
}



2.  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 */

void main(void)
{
int N;
int Total = 10;
int counter = 1;



printf("Enter a number: ");
scanf("%i", &N);

	__________for(counter = 1; counter <= Total; counter = counter +1)_____________ 


	__________{______________________________________________________________________



	_________________printf(" %i ", rand()%(N+1));___________________________________



	___________}___________________or ____counter = counter +1;  and no update statement above__



	________________________________________}________________________________________

printf("\n");
}





Part II


  1. TA's signature __________________________________
     
  2. What is the best strategy to win the guessing game?
     
     
    Guess the middle number in the possible range so that the range is halved after every guess.
    OR any equivalent answers

     

Part III

  1. TA's signature ______________________________________
Note: If students write code for openGL, do not grade it. (The signature suffices.)