CS16, Summer 2012

Writing and Running a C Program
Pair Programming

Goals for this lab

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

Step by Step Instructions

Step 0: Select a partner, turn in Pre-lab 1, and choose initial roles

You will learn best if you are matched with someone whose experience is very similar to your own:

Does your experience match the person right next to you? Do you both want to work with each other? If not, then look around to find a different partner now.

We expect a few minutes of (hopefully not chaotic) mingling while you match up, but please try to do it quickly. Maybe ask people "What type of computer/system(s) do you have?" if that matters to you. Research also indicates that lab partners are best matched by gender, but we leave that decision to you too.

We expect you to keep the partner you select today to work with in future labs. Please inform us if you make any changes later.

Before proceeding to Step 1:
Fill in your partner's name (and CoE username if available) in item 6 of Pre-lab 1.
Then deliver your homework paper(s) to the TA.

There are two roles involved with "Pair Programming" that you will practice in these labs: 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 give the pilots commands. 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 (that was necessary only for Lab00). On the other hand, we think you should copy your work after the lab ends to the first navigator's account.

Step 2: Create a ~/cs16/lab01 directory

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

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

You can do this with the following unix command:

mkdir ~/cs16/lab01

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

cd ~/cs16/lab01

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/pconrad, you'll see your own home directory listed, e.g. /cs/student/jsmith).

-bash-4.2$ mkdir ~/cs16/lab01
-bash-4.2$ cd ~/cs16/lab01
-bash-4.2$ pwd

In future weeks, we may simply say something like "create a ~/cs16/lab02 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: Use ch to test a formula that converts Fahrenheit to Celsius

In Lab00 you compiled and ran a program that converted Fahrenheit to Celsius using the following formula (where fTemp is the Fahrenheit value):

(fTemp - 32.0)*(5.0/9.0)

The program named ch at the Cooper Lab (and CSIL) can be used to check C formulas simply, without writing and compiling whole programs. Try that out right now by (a) starting ch; (b) creating and initializing a double variable named fTemp; (c) entering the formula; (d) repeat as desired: setting a new value for fTemp, and reentering the formula (try the up arrow for repeating commands); and then (e) exiting ch.

Here is an example session executed from the cs16 class account (user input is bold):

-bash-4.2$ ch
                Professional edition, version
              (C) Copyright 2001-2009 SoftIntegration, Inc.
/cs/class/cs16> double fTemp = 50
/cs/class/cs16> (fTemp - 32.0)*(5.0/9.0)
/cs/class/cs16> fTemp = 70
/cs/class/cs16> (fTemp - 32.0)*(5.0/9.0)
/cs/class/cs16> exit

Step 4: Devise and test a formula that converts Celsius to Fahrenheit

Do the math! Say the variable named cTemp contains a temperature value in degrees Celsius. Devise and test a formula that will convert it to degrees Fahrenheit. Refer to the formula in Step 3 above, and find its inverse.

Use ch to test it, and refer to the results from Step 3 to verify your tests. For example, here is part of a ch session where our formula is CENSORED:

/cs/class/cs16> double cTemp = 21.1111
/cs/class/cs16> CENSORED
/cs/class/cs16> cTemp = 10
/cs/class/cs16> CENSORED

We assume our censored formula is correct since the results match the inputs used for Step 3.

Step 5: Complete a simple C program that converts Celsius to Fahrenheit

First: switch roles between pilot and navigator. Stay logged into the original pilot's account.

Save a copy of this skeleton of ctof.c in the original pilot's lab01 directory. There is no need to copy and paste - you can use cp to get a copy from the instructor as follows:

cp ~kyledewey/cs16/lab01/ctof.c ~/cs16/lab01

Use ls to verify it worked.

Now it is time to edit the program with emacs (you should have learned to use that for Pre-lab 1) or another editor available in the Cooper Lab if you prefer. Here is some emacs help if you need it.

First read about the changes you have to make to the program below, then start emacs as follows (if that is the editor you will use):

emacs ctof.c

Make the following edits, then save, and quit the editor.

  1. Replace "YOUR NAME(S)" inside the comment at the top with your names. All other changes will be inside the main function.
  2. Declare variables of the appropriate type to store a temperature value in Celsius, and the corresponding Fahrenheit value.
  3. Replace the comment ("/* VARIABLE NAME */") in the call to the scanf function to properly store the user's input in the Celsius variable you declared in step 5b.
  4. Add an assignment statement that applies the formula you derived in Step 4 to find the user's temperature in degrees Fahrenheit. Store the result in the appropriate variable.
  5. Replace the comment ("/* VARIABLE, VARIABLE */") in the printf function call to print the proper results.

Step 6: Run and test the program

You can use ch to test your program. Here is a test of our solution:

-bash-4.2$ ch ./ctof.c
Enter a temperature in degrees C: 18.5
18.5 degrees C is 65.3 degrees F

If all is correct, it should print the correct Fahrenheit values for the Celsius values that you input.

If errors occur: (a) don't panic, and (b) don't immediately ask the TA for help! Instead, take a deep breath, and then actually read the message that comes up. It will often give a line number, and some hint as to what might be wrong.

Then, look at your program, around that line, and also immediately before that line. Often, if the error is reported on, say, line 15, it is because of a missing semicolon, on line 13 or 14, for example.

If you've really looked, and are still stumped after two or three tries, then you might ask the TA for help. But try to fix it on your own first.

When you are satisfied it works, make an executable version of it. Then run that version to show the TA:

-bash-4.2$ make ctof
cc     ctof.c   -o ctof
-bash-4.2$ ./ctof
Enter a temperature in degrees C: 11.75
11.75 degrees C is 53.15 degrees F

If no errors, proceed to Step 7. Otherwise try to figure out the problem and fix it yourselves before asking for help.

Step 7: Get credit for the lab

Submit your code with the turnin program. You MUST have both your name and your partner's name in the file in order to receive credit. Remember that the original pilot needs to do this step, since that is whose account you have been using in Cooper Lab.

Bring up a terminal window on CSIL, and cd into the original pilot's cs16 directory, and cd again into the lab01 directory. Then type the following:

turnin lab01@cs16 ctof.c

Respond "yes" when the program asks if you want to turn in (be sure to read the list of files you are turning in), and then wait for the message indicating success.

Evaluation and Grading

Each student (pair) must accomplish the following to earn full credit for this lab:

Deadline for lab submission: Tuesday, 7/3

The pre-lab is due by the lecture on 7/3, and the lab itelf is due at 11:59 at midnight on 7/3. Note that all subsequent pre-labs and labs will be due on the same day as the lab.

Optional Extra Challenge

Do any (or all) of the following tasks, not in any particular order this week:

Adapted by Kyle Dewey from Michael Costanzo from a 2009 prototype by Phillip T. Conrad.