CS 010 - Introduction to Computer Science I
Lab 7.
Functions

Points (10 overall)

  1. 2 pts: Attendance(at start and end of lab)
  2. 7 pts: Programs
  3. 1 pt:  Commenting and Style

Collaboration policy:
You will be working in pairs during labs.  Pairs will be randomly selected and will be announced by the TA.  Each week you will have a new random partner.  You will discuss the programs with your partner, but you will still be typing in your own code to show to the TA.  You can help each other debug, give plenty of suggestions and hints, **explain** why things work or don't work, etc.


Lab Objectives

To gain experience with

  1. writing functions
  2. using functions
  3. good programming and testing

Commenting and Style

  1. Be sure to use good variable names.
  2. Use comments throughout your code to explain your code.
  3. Use good spacing and keep it consistent throughout your program.

 


Program 1: Functions

Read this entire description before writing any code to be sure to follow the directions.

 

You will be writing a program that allows the user to specify whether they want to be tested in addition, subtraction, or multiplication.  Then your program will randomly generate 5 questions of that type.  The user will have the chance to give the correct answer.  Keep track of how many questions the user gets correct the first time. Output this amount after they complete their test.  Use the following functions for your program:

 

int rand_range(int lower, int upper)

The rand_range function generates a random number within a given range.  The lower and the upper end of the range are the parameters to the function.  The function should return the random number. 

 

int question(char type)

The question function outputs to the screen an addition, subtraction, or multiplication question.  The type parameter specifies which type of question will be used.  This parameter is expected to be a ‘+’, ‘-’, or ‘*’.    Each operand should be randomly generated (use above function).  For addition and subtraction, each operand should be in the range of 0-100.  Multiplication questions should use the range of 1-12 for the operands.  The question should be outputted as a math expression (ie: 14 + 31 =   ).  The answer to the question should be calculated and returned from the function.

 

int user_guess(int answer)

The user_guess function should allow the user to have two chances to give the correct answer. The correct answer is passed as a parameter to this function.  After each guess, specify if they got the right answer or not.  If after two guesses they still have not given the correct answer, the correct answer should be shown.  The function should return a 1 if they got the answer correct on their first guess, and should return a 0 if they did not.

 

Be sure to work wisely when writing this program.  You should write it in parts, compiling and testing before moving onto another part.

 

Write the rand_range function first.  Then verify that it is working properly.  Calling the function once with one set of parameters and having it work, does not verify correctness.  You need to think about the test cases and try various function inputs.  To do this, write a short main (this will not be part of the program you will eventually be writing).  The following is a sample main:

 

int main()

{

     srand(time(0));

     bool again = true;

     int upper, lower;

     string choice;

 

     while (again)

     {

          cout << "Enter lower and upper range:  ";

          cin >> lower >> upper;

 

          cout << "The random number chosen between ";

          cout << lower << " and " << upper << " is ";

          cout << rand_range(lower, upper) << endl;

 

          cout << "Do it again? (y/n) ";

          cin >> choice;

          if (choice != "y")

          {

              again = false;

          }

     }

     return 0;

}

 

When entering ranges, think about what test cases you should use?  Does your code work for large values, small values, negative values (ranging from a negative value to a positive value, as well as both bounds being negative)?  It could be tough to figure out if the function would choose a value that is out of bounds of the range.  Just because it chooses a value once within each range, doesn’t mean it wouldn’t go out of bounds with another choice.  To verify this, make a very small range (Example: 32 – 35) and check many randomly chosen numbers within this range. 

 

After verifying that this function is working properly, you should do something similar for each of the other functions.  Finally, you will write the main program once all functions have been verified.

 

You must follow the plan below:

    1. Before writing any code, you and your partner, should come up with test cases for each function.
    2. Write the rand_range function and verify correctness using your test cases, write down the results of each test case.
    3. Write the question function and verify correctness using your test cases, write down the results of each test case.
    4. Write the user_guess function and verify correctness using your test cases, write down the results of each test case. 
    5. Show your TA your test cases and the results you obtained for each function.
    6. After your TA has verified that the functions were test properly, write your main program.
    7. Verify that you main program is working properly and show it to your TA.

 


Bonus Program (1 pt):

Write a program that allows a user to randomly draw two cards and see if they win a prize.  Each card’s value (2-10, Jack, Queen, King, and Ace) and suit (hearts, diamonds, spades, and clubs) will be randomly chosen.  You will need to use integer values as codes for those things that are not integers (Jack, Queen, hearts, diamonds, …).  This will allow you to use the rand function which only generates integers.  However, when printing the two cards, you should translate the integers into what they are representing.  For example, you should output, “You drew a 3 of Hearts”, or “You drew a Jack of spades”.  Do not output, “You drew a 12 of suit type 2”.  Lastly, output whether they win.  A pair (two of the same value (suit doesn’t matter)) wins a large prize, and drawing a Jack (of any suit) wins a small prize.  You may be creative in choosing the prizes for your program.  You should use the rand_range function from the above program to generate your card value (2-10, Jack, Queen, King, Ace) and your suit.  You should also write a few other functions to complete this program (maybe one to generate and output the card’s value, and another for the prize).