By the time you have completed this lab, you should be able to
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.
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.
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 /cs/faculty/pconrad/cs16/lab01 -bash-4.2$
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.
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 Ch Professional edition, version 6.1.0.13751 (C) Copyright 2001-2009 SoftIntegration, Inc. http://www.softintegration.com /cs/class/cs16> double fTemp = 50 /cs/class/cs16> (fTemp - 32.0)*(5.0/9.0) 10.0000 /cs/class/cs16> fTemp = 70 70.0000 /cs/class/cs16> (fTemp - 32.0)*(5.0/9.0) 21.1111 /cs/class/cs16> exit -bash-4.2$
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 70.0000 /cs/class/cs16> cTemp = 10 10.0000 /cs/class/cs16> CENSORED 50.0000
We assume our censored formula is correct since the results match the inputs used for Step 3.
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.
YOUR NAME(S)
" inside the comment at the top with your names.
All other changes will be inside the main function./* 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./* VARIABLE, VARIABLE */
") in the printf function call
to print the proper results.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 -bash-4.2$
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 -bash-4.2$
If no errors, proceed to Step 7. Otherwise try to figure out the problem and fix it yourselves before asking for help.
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.
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:
- Create a function for your formula that converts Celsius to Fahrenheit. It should take a Celsius value as a parameter and return the corresponding Fahrenheit value. Include a header comment that describes the function's purpose and explains its parameter. Here is a template to complete:
/* cToF: returns Fahrenheit equivalence of Celsius degrees (cTemp) */ double cToF(double cTemp) { return _______________________________; }Write another version of ctof.c (call it something like ctof2.c) that includes this function, and with the main function changed to use it instead of calculating the result by itself.- Refer back to firstCProgram.c from Lab00. Notice how that program allowed the user to keep entering values until he/she decides to quit. Write another version ctof.c to operate that way.
- Write a program to perform a different type of conversion. Convert miles to kilometers, for example, or tons to kilograms. Use the web to look up the conversion factors. Select an appropriate name for your program, and include appropriate comments.
- Write more conversion programs if you still have time on your hands.
Adapted by Kyle Dewey from Michael Costanzo from a 2009 prototype by Phillip T. Conrad.