CPS 006, Lab4 Inlab

In working on this lab, feel free to talk to people near you, asking them for help or volunteering help yourself.

First you'll need to create a directory and copy some files into it. For the prelab you should have created a subdirectory lab4 in your cps006 directory. If you didn't do this, make sure you do it now:

Do these five commands only if you didn't complete the prelab

 cd
 cd cps006
 mkdir lab4
 cd lab4
 cp ~ola/cps006/lab4/* .


Change into your lab4 subdirectory and copy files as shown below (don't forget the trailing . on the last line).
   cd
   cd cps006/lab4
   pwd                  (make sure it's your lab4 directory)
   mkdir inlab
   cd inlab
   cp ~ola/cps006/lab4/inlab/* .

If you type the ls command you'll find three files

RTP and Love

Load the file drawtriangle.cpp into xemacs and compile it by entering
   make drawtriangle

Then run the program twice entering 5 and 20 for the number of rows.

You should notice that for 5 stars the program printed a diagram like this:

*
* * 
* * * 
* * * * 
* * * * * 

Now, remove the comment marker// before the call to PrintSpaces and recompile the program. The for loop in PrintTriangle should look like

for(k=0; k < rowCount; k++) { PrintSpaces(MIDDLE-k); PrintStars(k+1); cout << endl; }

Before you run the program you should try to figure out how the diagram will change. This will be good practice for test questions.

Run the program and work hard to understand why and how the output changed.

More General

The functions PrintStars and PrintSpaces both print a specific string a number of times specified by a parameter. The string printed in PrintSpaces is " " and the string printed in PrintStars is "* ". Write one function to be used in place of these. The function should have the prototype below. void PrintStrings(string s, int numStrings) // post: print s numStrings times on the current line

Add this function to drawtriangle.cpp and then replace calls to PrintSpaces and PrintStars with calls to PrintStrings. The program should print the same shaped-triangle it did before you made the changes, i.e., the triangle whose top vertex is centered in the screen.

After making sure that PrintStrings works, modify the program so that the triangle is shaped as shown in the row below.

prompt> drawtriangle
how many rows: 10
                                                                            * 
                                                                          * * 
                                                                        * * * 
                                                                      * * * * 
                                                                    * * * * * 
                                                                  * * * * * * 
                                                                * * * * * * * 
                                                              * * * * * * * * 
                                                            * * * * * * * * * 
                                                          * * * * * * * * * * 

You should be able to make this change by changing just the number of spaces printed before stars are printed in each row, i.e., by changing MIDDLE-k to some other expression.

As a hint, there are 78 spaces printed before the star in the first row, 76 spaces printed before the first star in the second row, 74 before the first star in the third row, and so on.

As long as the triangle has a "flat" right edge, it doesn't matter where the edge is located.

Make sure a TA sees your working drawtriangle program with the function PrintStrings.


One-armed men and bandits

Load the program slots.cpp into xemacs, compile it, then run it.

The current version simulates a kind of slot machine by showing three random strings on a line, and showing seven lines each time the program is run.

You are to make three modifications to the program.

  1. First implement the functions whose prototypes follow.
    bool AllSame(string a, string b, string c)
    // post: return true if all strings are the same
    //       otherwise returns false
    
    
    bool AllDifferent(string a, string b, string c)
    // post: return true if all strings are different
    //       otherwise returns false
    
    

    For example, if there only two strings, the function AllSame could be written as follows.

     bool AllSame(string a, string b)
     // post: returns true iff a and b are the same
     {
         return a == b;
     }
    

    You'll want to use either the and operator && or the or operator || in writing these two functions.

    n You can ask the person near you for help if the TAs seem busy.

  2. Now modify DoSlots so that after printing the simulated slot rolls (in the program there are seven lines printed) a message is printed as to whether the player is a winner or a loser. If the last three strings printed are all the same or are all different, the player is a winner. Otherwise the player is a loser. For example:
    prompt> slots
    orange orange orange
    cherry lemon orange
    lemon orange orange
    cherry cherry cherry
    lemon lemon cherry
    lemon cherry lime
    orange orange cherry
    
    a loser
    
    prompt> slots
    lemon cherry orange
    lemon orange orange
    lime orange lemon
    lemon orange lemon
    cherry cherry cherry
    cherry lime cherry
    orange cherry lime
    
    a winner 
    
    

  3. Now modify the function DoSlots by first commenting out the coutstatements that print the random strings so that nothing is printed in the loop when the function executes. Then change the function from void to bool and instead of printing if the user is a winner or loser, return true if the user is a winner (the last strings "rolled" are all the same or all different) and return false if the user is a loser.

    Now call DoSlots(7) 10,000 times from main (write a loop) and accumulate a count of how many times the user wins. Print this result. Here's a start at the code in main, it's not complete.

        int count=0;
        for(int k=0; k < 10000; k++)
        {
    	if (DoSlots(7)) 
            {
    
            }
        }
    

Run the program five or six times and write down the percentage of wins you expect a user will have when spinning this simulated slot machine.
   write percentage here _______________

Be sure to call the TA over to check your program.

Finishing

If you don't complete the program during lab, you can complete it within 24 hours and submit using
  submit_cps006 lab4 *.cpp

Owen L. Astrachan
Last modified: Mon Sep 24 22:34:37 EDT 2001