CSCE 2004 - Laboratory Assignment 6

    The objective of this lab is to give students hands on exposure to functions in C++. In particular, we will be calling built in functions, calling user defined functions, modifying functions, and creating functions. As we will see, functions are very important for top-down incremental software development. This assignment has the following steps:

  1. Getting Started
  2. Check your Grades

    Before we start this lab, use the class website to check your grades in this class so far. Go to the "Grades" link and type in your UAID and website password. If you have "-1" grades for labs or homeworks, it means that the GTA has not graded this yet. If you have "0" grades, it means that the GTA graded this lab or homework, but you did not hand anything in. If you find any errors, please inform your GTA.

    Update Your .bashrc File

    Login to turing using your UARK username and password. Now run "ls -la". This will show you all of the files in your home directory. Notice that there are several files that you have not seen before that all start with a ".". The "-a" flag shows these hidden files.

    The file ".bashrc" is read by the system when you login to perform user specific initializations. Use nano to edit this file and add the following lines at the top of the file:

    alias rm='rm -i'
    alias cp='cp -i'
    alias mv='mv -i'
    alias lab='cd ~/2004/labs'
    alias indent='indent -i3 -bli0 -npsl -npcs'
    

    The first three lines define aliases for the rm, cp and mv commands so they will always ask you before deleting a file or overwriting a file. The next line gives you a quick way to jump into the labs directory. Whenever you type "lab" it will perform the "cd ~/2004/labs" command. Finally, the indent alias will let you run "indent hw.cpp" without having to type the long list of options.

    Logout of turing, and then login again to start using the .bashrc aliases above.

    Tidy Your 2004 Directory

    Since you have been creating programs and output files for this class for several weeks it may be time to do some tidying. Go to your home directory and run "ls -R 2004". This will show you all folders and subfolders and all files inside the 2004 directory. You should see something like this:

    2004:
    hw  labs
    
    2004/hw:
    hw1  hw2  hw3
    
    2004/hw/hw1:
    (some files)
    
    2004/hw/hw2:
    (some files)
    
    2004/hw/hw3:
    (some files)
    
    2004/labs:
    lab1  lab2  lab3  lab4	lab5
    
    2004/labs/lab1:
    (some files)
    
    2004/labs/lab2:
    (some files)
    
    2004/labs/lab3:
    (some files)
    
    2004/labs/lab4:
    (some files)
    
    2004/labs/lab5:
    (some files)
    

    If your 2004 directory does not look like this, you should use "mkdir" to make any missing directories, and "mv" to put files into the correct directories. Keeping things tidy will save you a lot of time down the road.

  3. Calling Built In Functions
  4. There are a wide range of C++ libraries of functions that are available on most systems. In this section we will be using several of the functions in the "cstdlib" library in our main program.

    Step 1: Before we start, use "mkdir" to create 2004/labs/lab6 for this lab. Then change directories into lab6. Now copy and paste the following code into a file called "fun1.cpp" and compile using "g++ fun1.cpp -o fun1".

    #include <iostream>
    #include <cstdlib>
    using namespace std;
    
    int main()
    {
       int count = 10;
       for (int i=0; i < count; i++)
       {
          int num = rand();
          cout << num << " ";
       } 
       cout << endl;
       return 0;
    }
    

    Step 2: When you run this program, you should see 10 random numbers printed out. Run the program again and see what you get? The rand() function returns a pseudo-random value between [0..RAND_MAX]. Every time you run a program it will generate the same "random looking" sequence of values.

    Step 3: The output values from rand() are larger than we need for most applications. Edit your fun1.cpp program to add the line "num = num % 10;" after the rand() statememnt. Increase the count from 10 to 30 and run the program again. You should see roughly the same number of each data value.

    Step 4: To get a different sequence of values, you can specify a "seed" to the pseudo-random number generator. To do this, declare an integer called seed and initialize this variable to some non-zero value. Then add the function call "srand(seed);" before your for loop. Now run the program to see what happens with different values of seed.

    Copy and paste your final program below.

    Copy and paste two samples of your program output below.

  5. Calling User Defined Functions
  6. In this section, you will be writing code in the main program that calls several user defined functions.

    Step 1: Cut and paste the following code into fun2.cpp and add your name and the current date to the top of the program. Now compile the program using g++.

    //---------------------------------------------------
    // Author:	TBA
    // Date:	TBA
    // Purpose:	Program to calculate the cost tickets
    //---------------------------------------------------
    #include <iostream>
    using namespace std;
    
    //---------------------------------------------------
    // Ask the user which concert they wish to attend
    //---------------------------------------------------
    char GetConcert()
    {
       char Choice;
       cout << "The following concerts are scheduled for the Walton Center:\n";
       cout << "     C for Canadian Brass\n";
       cout << "     J for Julliard String Quartet\n";
       cout << "     N for NSync\n";
       cout << "Enter the letter (in uppercase) for the concert you wish to attend: ";
       cin >> Choice;
       return Choice;
    }
    
    //---------------------------------------------------
    // Ask the user how many tickets they would like
    //---------------------------------------------------
    int GetNumTickets()
    {
       int NumTickets;
       cout << "Enter the number of tickets you would like to purchase: ";
       cin >> NumTickets;
       while ((NumTickets < 1) || (NumTickets > 10))
       {
          if (NumTickets < 1)
    	 cout << "You must purchase at least one ticket.\n";
          else if (NumTickets > 10)
    	 cout << "You may not purchase more than 10 tickets.\n";
          cout << "Please enter the number of tickets you would like to purchase:\n";
          cin >> NumTickets;
       }
       return NumTickets;
    }
    
    //---------------------------------------------------
    // Main program to test functions
    //---------------------------------------------------
    int main()
    {
       // Declare variables 
    
       // Call function to find out the concert they want to attend
    
       // Call function to find out how many tickets they want
    
       // Print out the information you have collected.
       // cout << "\n\nThe customer has placed the following order:\n";
       // cout << "Concert: " << Concert << endl;
       // cout << "Number of Tickets: " << NumTickets << endl;
       return 0;
    }
    

    Step 2: As you can see, this program has two user defined functions and a mostly empty main program. When you run the program it should do nothing because the main program is mostly comments.

    Step 3: Take a look at the definition of the two functions above to see what data types they return, and declare two variables in the main program called "Concert" and "NumTickets" to store these return values.

    Step 4: Edit the main program again, and fill in the two function calls to initialize the two variables above. Then uncomment the cout statements so the values of these variables are printed.

    Step 5: Finally, run the program with a variety of input values to see how the program works. What happens if the user follows instructions correctly? What happens if they enter incorrect or unexpected values?

    Copy and paste your new main program below.

    Copy and paste a sample of your program output below.

  7. Modifying User Defined Functions
  8. As you can see, the function "GetConcert" is supposed to return 'C', 'J', or 'N' to indicate the concert choice. Unfortunately, if the user enters 'X' the function will just return this character.

    Step 1: Your next task is to modify the GetConcert function to perform error checking to ensure that the correct values are returned. If the user enters an incorrect value, they should be prompted to try again using a loop.

    Step 2: Compile the code using g++ again, and test your modified function with a variety of input values to demonstrate that the code is working correctly.

    Copy and paste your new GetConcert function below.

    Copy and paste a sample of your program output below.

  9. Creating User Defined Functions
  10. Your final task is to write (and call) a new C++ function called "CalculateCost" which is given NumTickets and Concert as input parameters and calculates and returns the cost of the ticket order as a return value.

    Step 1: Create an empty "CalculateCost" function with the correct return type, function name, and parameter types. For now, you can have the function return a fixed value (like 42).

    Step 2: Extend your main program to call this skeleton function with the correct parameters, and save and print your return value. When you run the program, it will always print 42 because the function is not doing any real work yet.

    Step 3: Now fill in the body of the "CalculateCost" function. Assume that the tickets to the Canadian Brass are $25 each, tickets to the Julliard String quartet are $20 each and tickets to NSync are $30 each. The printout of the cost of the tickets should occur in the main program, not the function.

    Step 4: Test your completed program with a variety of inputs to verify that cost calculations for all three concerts are working correctly. Congratulations! You have completed your first program writing and calling C++ functions.

    Copy and paste your final program below.

    Copy and paste a sample of your program output below.

  11. Submit Work
  12. This lab assignment will be submitted electronically to the TAs once you fill in the fields below and click on the "submit" button. You do NOT need to print a copy of this document to hand in.

    Your UAID number:
    Your website PASSWORD: