CPTR 124 Fundamentals of Programming


In this lab you will write programs that use loops.


  1. Teams

    You are encouraged to work with a partner for this lab. You and your partner should begin thinking about the problems and begin writing the code before lab time.

  2. Part 1: Guessing Game

    In C++, you can generate a pseudorandom number by calling the rand function. To generate pseudorandom numbers, execute the following statement once in your program

    srand(static_cast<unsigned>(time(0)));
    	 
    Afterward, each time you need a pseudorandom number, the statement
    x = rand();
    	 
    will assign a pseudorandom number to the integer variable x. You will need to add the preprocessor directives #include <cstdlib> and #include <ctime> to the top of your program.

    The following complete program prints 10 pseudorandom numbers:

       #include <iostream>
       #include <cstdlib>
       #include <ctime>
       
       using namespace std;
       
       int main()
       {
           srand(static_cast<unsigned>(time(0)));
           
           int i = 0;
           while ( i < 10 )
           {
               cout << rand() << endl;    
               i++;
           }
       }
    

    Each time the program is run it prints a different sequence of numbers.

    In Visual C++, the expression rand() evaluates to a pseudorandom number in the range 0...32,767. This range is too large for our immediate need, so you will have to adapt the result with some arithmetic that includes the modulus operator (%).

    See Chapter 8 in the textbook for more information about random numbers.

    Write a C++ program that plays a guessing game with the user. The program's user is the player. The player is supposed to guess an integer in the range 1–100. The program should generate a number in this range at random. The program's number is the correct answer. The player's guess is compared to the correct number:

    • If the player's number is less than the correct number, then the program should print "Too low" and allow the player to guess again.
    • If the player's number is greater than the correct number, then the program should print "Too high" and allow the player to guess again.
    • If the player's number is the same as the correct number, the game is over.

    Within a loop, the game continues until the player guesses the correct answer. When the game ends, the total number of guesses is printed. The player's goal is to guess the number with the fewest attempts.

    Hint: While you are developing your program you can print the answer (secret number to guess) each time a new guess is requested. That way you can better tell if your program is working properly; that is, giving you the correct feedback. Be sure to disable this "feature" in your final version, or it will not be much of a game!

  3. Part 2: Histogram program

    Write a C++ program that reads in an arbitrary number of nonnegative integer values. The input is terminated by an integer value less than zero. Use the cin input stream object so the values may be entered from the keyboard or from a textfile using redirection.

    After all the values have been entered, print a histogram of the number of values in the following ranges: 0-99, 100-199, 200-299, 300-399, 400-499, and 500+.

    If the following values are entered:

            115 556 130 605 714 808 415 494 769 278 451 114 816 398 669
            481 166 81 335 459 388 504 548 402 646 299 458 733 187 615 932
            983 342 302 483 543 358 373 469 881 458 114 214 253 265 869 55
            198 337 889 533 694 343 307 620 712 710 650 827 62 115 951 257
            116 801 31 192 820 8 686 208 444 300 671 200 325 562 761 185 197
            126 380 771 384 131 397 773 570 523 262 510 304 360 793 481 391
            313 534 145 645 -1
                 

    the resulting histogram would be

              0- 99   5|******
            100-199  15|*******************
            200-299   9|***********
            300-399  19|************************
            400-499  12|***************
              500+   40|**************************************************
                       
            **********************************************************************
                       

    Each line lists the data range, the actual number of values in that range, a vertical bar, and then the bar of asterisks representing that quantity.

    The longest bar should always contain 50 asterisks, and the other bars should be scaled proportionally. The sample input

                 450 401 3 78 444 200 -1
                 

    would display

              0- 99   2|*********************************
            100-199   0|
            200-299   1|*****************
            300-399   0|
            400-499   3|**************************************************
              500+    0|
                       
            **********************************************************************
                       

    The formatting of your output should be identical to example output above.

  4. Check out

    Your finished programs will be evaluated for correctness and compliance. When approved, you should submit your two source files to eclass.