CSCI 159 Lab exercise 1, F26N01/N02

Introductory lab notes

Most lab exercises will consist of two parts:

Ideally you complete all of part 1 and get started on part 2 while you're in your weekly lab, then complete and submit it on your own time over the following two weeks (specific deadlines can be found on the main labs page).

For this first lab, however, the entire lab will be done in a very paint-by-numbers fashion to introduce the core tools and processes we'll need to follow for the rest of the term, and you'll finish and submit it prior to the start of your next lab (a week later).

In this first lab we'll also have to go through a couple of setup steps to get your account and files/directories set up and organized for the term.

For each lab, there are several key C++ syntax elements you'll need to be familiar with.
The core part 1 syntax elements can be found here.

NOTE THAT I'LL BE DISTRIBUTING YOUR CSCI LOGIN IDS IN THIS LAB
you will be unable to login on our linux servers to work on your labs until you have this information

The objectives for this lab are to master some basic linux skills, learn how to obtain and submit labs, and learn how to edit, compile, and run C++ programs in a linux environment,

The layout of instructions for this part of lab 1 will be quite different than for the remaining labs. In this lab, instructions are laid out in a very step-by-step fashion to walk you through an introduction to our linux labs, your accounts, and the use of C++.

Be sure to go through each of the steps carefully, and in the order given, successfully completing each step before moving on to the next one.


1. Get logged in to your CSCI account and open some needed tools

(you'll do this every time you want to work on one of your CSCI labs)

THE PRINTED HANDOUT ENDS HERE, THE REST OF THE LAB INSTRUCTIONS FOLLOW BELOW ON THE WEB PAGE


2. Practice some basic linux:


3. Get a special file that we'll use for the rest of the term

We're going to copy a special 'makefile' from the instructor's account to your home directory.

This allows us to bundle up sets of linux commands, and run them through a program called 'make'. This semester I'll be providing a variety of makefiles to simplify the process of obtaining, compiling, and submitting your lab exercises.

This very first makefile belongs in your home directory and will be named make159. Instructions for obtaining using it are as follows:


4. Go to today's lab directory and try out the starting code


5. Try editing, compiling, running, and submitting the starting code

The notes below suggest using pico as the text editor, so these will work even if you're using your own laptop and following the ssh instructions to connect to our servers.
While you're physically in our lab room, a nicer graphical editor is pluma, run by using the command pluma lab1.cpp, which will open up the file in a separate window.


6. Try closing things up and logging out (then we'll sign back in to do more work)

You've now completed one cycle of our typical lab process, so next we'll try logging out, logging back in, and getting back to our lab1 code to work on it further.


7. Sign back in and get set up to continue working

Now let's log back in (on the same machine or any other in the lab, they're all accessing the same shared file system, so your account contents will look the same on each).


8. Now let's turn our lab1.cpp into a real program and submit it

  1. For lab1 we're writing a short program to compute a (10pin) bowler's league handicap based on the scores from their last three games. We'll get them to provide their name and the three scores, then we'll compute their average and handicap. Our program will then display a summary of their information back to them.

    The steps below will walk through the construction of the program

  2. We've got the basic structure of a C++ program in our lab1.cpp already,
    #include <iostream>
    
    int main()
    {
       std::cout << "Welcome to lab1!" << std::endl;
    
       return 0;
    }
    

  3. First we'll add another library we're going to need: string (we'll use a string variable to hold their name eventually).

    With the new libary, the top of the program will now look like:
    #include <iostream>
    #include <string>
    
    Change the file content, save, then try recompiling in the terminal window
    make lab1x
    If you get any compilation errors then go back to the editor and keep fixing/compiling until it's ok, then try running it. (For the moment, the behaviour will be exactly the same as before.)
    ./lab1x

  4. Next we'll display a short intro to the program so the user has some idea of what's going on.

    We'll add this using more cout statements, replacing the single cout we originally had, e.g.
       std::cout << "Welcome to BCapper: the program that computes your bowling handicap";
       std::cout << std::endl;
       std::cout << "You will be asked to enter your name and the scores of your last three ";
       std::cout << "ten-pin bowling scores (0-300)." << std::endl;
       std::cout << "BCapper will then tell your your league handicap." << std::endl;
       std::cout << std::endl;
    

    Again try compiling/fixing until it compiles cleanly, and now when we run it we should see the message above.

  5. Now we can add some variables to hold the bowler's name, the three game scores, and the computed average and handicap.

    The name will be entered and stored as a single word, so we'll use a string variable to hold that. The three game scores can be stored as integers, while the average and handicap can be stored as floats.

    For each variable we'll pick a meaningful name and we'll put a descriptive comment beside the variable.

    We'll put all this just after the { at the start of the main routine, e.g.
    int main()
    {
       std::string name; // the bowler's name
       int game1, game2, game3; // the bowler's past three scores
       float average, handicap; // computed based on the three scores
    
        ... the cout's from the previous step would be down here ...
    

    Again try compiling/fixing until it compiles cleanly other than warnings about unused variables (those will go away as we actually get the user to input some data).
    If we run it now (i.e. the ./lab1x command) it should still just give the descriptive message then end.

  6. Now, one-by-one, we'll prompt the user to enter part of the information and we'll read it into a variable, e.g.
       std::cout << "Enter the your name (one word only): ";
       std::cin >> name;
    

    These will go below our earlier cout (so they see the program description *then* it starts asking for data) but above the return 0 (since that effectively ends a run of the main routine).

    Do similarly for each of the game score variables (game1, game2, game3) after giving the user a clear description of what they should be entering.

    As usual, compile and fix until it compiles ok. (Notice the warnings for the variables have changed, now it tells us we've set values for them but aren't using them. This will be fixed in the next step.)
    Now if we run the program it should prompt us to enter the various values, stopping and waiting for us to enter a value each time.

  7. Next we'll do the calculation of the average and handicap.

    For the average, it is simply the sum of the three games divided by 3.0, e.g.
       average = (game1 + game2 + game3) / 3.0;
    
    For the handicap, a typical league calculation might be 90% of the difference between the bowler's average and a designated league base score such as 200:
       handicap = (200 - average) * 0.9;
    
    However, in accordance with our coding standards, we'll designate named constants to use in place of the 'hard-coded' 200 and 0.9, e.g.
       const int BaseScore = 200; // expected league-average score, used for handicap calculation
       const float HCadj = 0.9;   // league-designated adjustment (generally 0.8 to 1.0)
    
    Our calculation of handicap would then become
       handicap = HCadj * (BaseScore - average);
    

  8. Finally we'll add a section to display all the stored data back to the user in a meaningful way. This will go after (below) the completed calculations (but before/above the return 0).
       std::cout << "Based on your average score (" << average << "), ";
       std::cout << "your league handicap is " << handicap << std::endl;
       std::cout << std::endl;
    

    Compile and fix (as usual), and hopefully the program should behave as expected when run: giving the user a description of the program, prompting the user to enter their data, doing the calculations, then displaying the final results.

    Keep tweaking and fixing the program until you're satisfied it is working correctly and you're happy with the appearance of the output.


9. Sample output

The example below shows a sample run of the program.

Just to make the output below clearer, things the user typed have been emphasized in bold italics. (That wouldn't be the case when you run your programs, it's just to make the example a little clearer.)

Welcome to BCapper: the program that computes your bowling handicap 
You will be asked to enter your name and the scores of your last three ten-pin bowling scores (0-300). 
BCapper will then tell your your league handicap. 
 
Enter the your name (one word only): Dave
Enter your last three 10-pin bowling scores: 129 151 137 
Based on your average score (139), your league handicap is 54.9 
 


10. Code standards

In future labs your code will also be required to meet the course code standards, but for this first lab we'll focus on just getting the functionality correct and making sure we can submit it successfully.


11. When you're done:


Continuing your lab work from home


12. Deadlines and late penalties

Submission deadlines and late penalties for all lab exercises are provided on the main labs page.


13. Supporting tools

Most of the time we won't complete the entire lab in the two-hour lab block, so you'll want to come back to the lab later in the week or connect from home.

Discussion of the basic tools and steps for doing so can be found here.