CS16, Winter 2018

Lab02:
I/O for a user, and simple flow control


Goals for this lab

By the time you have completed this lab, you should be able to

Step by Step Instructions

Step 0: Choose initial pair programming roles

This is your first "real" CS 16 lab working with a partner, and it may or may not be your first experience with "Pair Programming." You will learn more about it as the quarter progresses. Remember (from Lab01): there are two roles: pilot and navigator. The pilot sits at the computer and types, and the navigator helps. It is important that each of you thinks about the problems independently. The pilot directs, but does not leave the navigator behind. The navigator assists, but does not issue commands to the pilot. You are helping each other so that you both understand the work.

Choose who will be the pilot for the first part of the lab. The pilot should sit down in front of the computer now. The navigator gets a chair and sits next to the pilot. You will exchange roles about halfway through the lab.

Step 1: Log on to CSIL and bring up a terminal window.

Log onto the pilot's account. If the pilot's account is not working, allow the navigator to log in instead. As a reminder, that's the Application Menu, then System Tools, then Terminal Window.

In the steps below, and in most future labs, you will create files in the account of the day's first pilot - and that is sufficient for completing the required lab work. In other words, just one set per pair is required, and it is not necessary to turn in identical copies. On the other hand, we think you should copy your work after the lab ends to the first navigator's account, so you both will have records of your work.

Step 2: Create a ~/cs16/lab01 directory

Last week, we created your ~/cs16 directory, and the ~/cs16/lab01 directory.

This week, we'll create the ~/cs16/lab02 directory.

You can do this with the following unix command:

mkdir ~/cs16/lab02

Then you can use a cd command to go directly into that directory:

cd ~/cs16/lab02

You can then use the pwd command to make sure that you are in the proper spot. The result should look like this (except in place of /cs/faculty/mikec, you'll see your own home directory listed, e.g. /cs/student/jsmith).

-bash-4.3$ mkdir ~/cs16
-bash-4.3$ mkdir ~/cs16/lab02
-bash-4.3$ cd ~/cs16/lab02
-bash-4.3$ pwd
/cs/faculty/mikec/cs16/lab02
-bash-4.3$ 

In future weeks, we may simply say something like "create a ~/cs16/lab03 directory and make it your current directory", without spelling out the Linux commands to do this. You can always refer back to previous labs if you forget the details, but eventually you'll want to memorize some of the most useful commands such as mkdir, cd, pwd and ls.

Step 3: Study a program to learn techniques you need later

This demo program shows techniques you will need to solve the next two steps of this lab. You must understand the first section before doing Step 4, and understand the rest before doing Step 5. The following paragraphs explain the demo program's features.

Like all of the programs in Chapter 2 of the text, this one contains just a main function. It starts with a comment that describes its purpose, and identifies its author (your instructor is cmc) and the date it was written. The executable statements are inside the main function. Here is the basic structure of any such C++ program:

// header comment with at least author name(s) and creation date
 
 #include <iostream>
 using namespace std;
 
 int main()
 {
     // statements to implement the program's purpose
 
     return 0;
 }

At the start of the main function, this program declares three integer variables named length, width and area:

int length, width, area;

Then cout is used to prompt the user to enter the length, and cin is used to get the length. The width is gotten the same way: first prompt with cout, then read with cin.

    cout << "Enter length: " << endl;
    cin >> length;
    cout << "Enter width: " << endl;
    cin >> width;

The area is calculated, and the initial results are printed by the next statements:

    area = length * width;
    cout << "A " << length << " by " << width
        << " rectangle's area is " << area << "." << endl;

The techniques above are enough for you to follow while solving the problem of Step 4.
You may want to skip to Step 4 now, and then come back here before starting Step 5.

In the rest of the demonstration program, the calculated area is compared to an arbitrary maximum area of 1000. It uses an if/else selection structure to print different messages if the area is less than, equal to or greater than the maximum. Here are three sample runs that demonstrate all three "branches" of this structure at work:

-bash-4.3$ ./lab02demo
Enter length: 
40
Enter width: 
30
A 40 by 30 rectangle's area is 1200.
Maximum area is 1000.
Your area is 200 more than that.
-bash-4.3$ ./lab02demo
Enter length: 
40 
Enter width: 
25
A 40 by 25 rectangle's area is 1000.
Maximum area is 1000.
Your area is exactly that.
-bash-4.3$ ./lab02demo
Enter length: 
55
Enter width: 
20
A 55 by 20 rectangle's area is 1100.
Maximum area is 1000.
Your area is 100 more than that.

In solving Step 5, you won't need to use an "else if" clause like this demonstration program (although you might want to use one for PA2), but it is included here so you can learn from it. Do pay close attention to the syntax of the other parts though, as you will want to use similar if and else clauses to solve Step 5.

Step 4: Complete a program to calculate for a user

First download this skeleton of coins.cpp, and save a copy in your working directory for this lab (original pilot's ~/cs16/lab02/ directory).

Edit the file with emacs (or another editor of your choice), and follow the instructions given in the upper-case comments to solve the following problem (from Chapter 1, Programming Project 3):

Write a program that allows the user to enter a number of quarters, dimes, and nickels and then outputs the monetary value of the coins in cents. For example, if the user enters 2 for the number of quarters, 3 for the number of dimes, and 1 for the number of nickels, then the program should output that the coins are worth 85 cents.

A correct solution prints a string of text to the terminal before getting each piece of input from the user. A session should look exactly like the following example (including whitespace and formatting), with possibly different inputs and results:

-bash-4.3$ ./coins
Enter number of quarters:
2
Enter number of dimes:
3
Enter number of nickels:
1 
The coins are worth 85 cents.
Each string printed by the program must include a newline at the end, but no other trailing whitespace (i.e., no whitespace at the end of the line), to receive full credit from the submit.cs system.

You can compile your program with make using the following command:

make coins
Fix syntax errors as necessary, and recompile until there are no errors. Then execute it to verify it is working properly. Do not submit yet - wait until you finish Step 5. Be prepared to fix problems revealed by the submit.cs results though (usually these are format problems if you already verified the calculations).

Step 5: Write a program that uses a selection structure

Switch pair programming roles now if you have not already done that:
pilot becomes navigator, and navigator becomes pilot.
Keep working in the original pilot's account though.

This time create a program from scratch, in a file named meeting.cpp. One way to create a new empty file is with the touch command. Type the following:

touch meeting.cpp
Then open the file with emacs or another editor, and type in the basic parts like the skeleton of coins.cpp from the previous step (or just copy them from that step, and paste them into your new file).

In the main function, solve the following problem (adapted from Chapter 2, Programming Project 5):

Write a program that determines whether a meeting room is in violation of fire law regulations regarding the maximum room capacity. The program will read in the maximum room capacity and the number of people attending the meeting. If the number of people is less than or equal to the maximum room capacity, the program announces that it is legal to hold the meeting. If the number of people exceeds the maximum room capacity, the program announces that the meeting cannot be held and tells how many people must leave.

Note: You should do the problem specified above, rather than the harder version shown in the textbook.

A correct solution prints a string of text to the terminal before getting each piece of input from the user. Sessions should look exactly like the following two examples (including whitespace and formatting), with possibly different inputs and results:

-bash-4.3$ ./meeting
Enter maximum room capacity:
50
Enter number of people present: 
40
Meeting is legal.
-bash-4.3$ ./meeting
Enter maximum room capacity:
50
Enter number of people present: 
55
Meeting cannot be held unless 5 leave.

Compile the program (fix syntax errors as necessary), and check results.

Step 6: Submit your two programs

If you are still logged on, and your current directory is still the same as your two programs, then the quickest way to submit is by entering the following command (suggest you copy/paste):

~submit/submit -p 938 coins.cpp meeting.cpp
Otherwise you can use the submit.cs interface at https://submit.cs.ucsb.edu/ from your web browser. Either way, be sure to wait for the results of the five tests (2 for coins and 3 for meeting). Fix any problems, and resubmit as necessary until your score is 50/50 points.

If you are working with a partner, be sure that both partners' names are in a comment at the top of the source code file, and be sure to properly form a group for this project in the submit.cs system.


Evaluation and Grading

Each student must accomplish the following to earn full credit [50 total points] for this lab:

If you finish early, ask the TA if you should help any of your classmates. Thanks!


Prepared by Michael Costanzo.