Your assignment must be submitted electronically by 11:59 pm on Friday, January 19 (submission instructions at the bottom).
Prior to beginning this tutorial, complete the following readings:
Your first task is to familiarize yourself with logging in onto the department’s server (classroom.cs.unc.edu). Your login account on the CS Dept systems is distinct from your UNC onyen, although in most cases your CS login ID will be the same as your onyen. If you do not have an account on the CS Dept systems, or are not sure if you have an account, or do not remember your password, use your onyen to create your account or reset your password.
There are different ways to log in onto the classroom sever depending on the OS on your personal computer:
Windows users should use PuTTY (download for free) for logging in and for transferring files. Launch PuTTY and use classroom.cs.unc.edu as host name, and your CS id as user name.
Mac / Linux users should use the terminal/shell application and ssh command, substituting your actual CS ID in the following command:
ssh yourlogin@classroom.cs.unc.edu
Note that when logging in to the classroom.cs server, your password is not echoed onto the screen (not even *s are shown). Simply press [Return] after your password.
You will also frequently need to transfer files between your personal computer and the server. Again, there are different ways of doing so, depending on the OS on your personal computer:
Windows users should use WinSCP (download for free). Use the same host and user names as for logging in. Drag and drop to copy files between your laptop and the server.
Mac / Linux users: You may download a user-friendly tool such as Cyberduck, FileZilla and Macfusion (all free). Or, you may use the terminal/shell application and scp command. In the examples below, we first navigate to the correct folder on your personal computer (using the "change directory" command cd), and then use the "secure copy" command scp to perform the copy. These examples assume that the folder on the server where the files are to be copied from/to is called "comp411lab".
To copy file from laptop to server:
cd folder_on_laptop
scp file_on_laptop yourlogin@classroom.cs.unc.edu:comp411lab/
To copy file from server to laptop:
cd folder_on_laptop
scp yourlogin@classroom.cs.unc.edu:comp411lab/file_on_server ./
Go through the UNIX tutorial in its entirety, try all of the UNIX commands described, and do all of the embedded tasks. If you do not follow the entire tutorial now, you will have difficulty with UNIX commands throughout the semester.
Specifically, go through all of the following sections of the tutorial:
Log in to classroom.cs.unc.edu. Before starting work, execute the following command to start a command interpreter ("shell") called bash, and initialize some settings to values that are convenient for our class. (You should type the following command each time you log in, for all the labs in this semester, just to ensure a predictable environment for you to work in. Moreover, for subsequent labs, the environment settings may be updated, so it is best to run this command each time you log in.)
/home/montek/comp411/bin/comp411start
Next, create a subdirectory (i.e., folder) comp411lab in your home directory by typing the following commands. Note: In these instructions, the % at the beginning of every line is simply the prompt that the command interpreter prints for you. Your prompt may look different, e.g., classroom(nn)%, where nn is the sequence number of the command you are typing. So, in the following, you are only to type whatever is shown after the % prompt.
% cd ~
% mkdir comp411lab
You must protect this folder so others cannot see inside it. You use the filesystem command (fs) to set up an access control list (ACL) that allows nobody any access (-clear) except you (all access). Be sure to replace yourloginname with your actual login id.
% fs setacl -clear -dir comp411lab -acl yourloginname all
Inside it, create a subfolder lab1 and make it your current working directory:
% cd ~/comp411lab
% mkdir lab1
% cd lab1
Download/copy the sample program intro.c into the lab1 subfolder. You can do this in either of two ways. One option is to download the file on your laptop using your browser, and then copy it to the server using the method explained above. Here's how you do it, depending on your operating system:
% scp path-to-download-folder/intro.c yourlogin@classroom.cs.unc.edu:comp411lab/lab1
The second method is to simply login onto classroom, and then copy the file from a location on that server itself, using the copy (cp) command, as follows:
% cp /home/montek/public_html/teaching/Comp411-Spring18/Lab1/intro.c .
Note: Do not ignore the trailing "." after the "intro.c" in the command above! The dot indicates that the file intro.c must be copied into the current folder.
For this tutorial, you will edit files using the editor pico (actually, it is mapped to a clone by GNU called nano, but you can use either name to launch it). View the C file using using pico as follows:
% pico intro.c
This editor is text-based, and you will need to use Control-key combinations to navigate the menu. Here are a couple of useful pico references:
Quit the editor, and enter the following Unix command to compile the program:% gcc intro.c
Or,
% cc intro.c
Note: cc stands for "C Compiler", which on our department's systems is linked (i.e., aliased) to gcc, which is the GNU C Compiler, an open-source implementation.
What do you think has happened? Have a look at the contents of your current working directory. Find out what type of data is contained in the file a.out (Hint: use the file command you learned in the Unix Tutorial Lab 1).
Execute the program by typing
% ./a.out
and see what happens. Do you understand the C text of the program?
NOTE: Always put a "./" before the program name when executing a program in your working directory.
Remove the file a.out and try compiling the program in a slightly different way:
% rm a.out
% gcc intro.c -o intro
What is the difference? This time, the result of compilation produced an executable file called intro instead of a.out. Can you run the program now? You would run it by typing
% ./intro
Run the program again, but this time redirect its output into the file intro.out (using ">" to redirect its output, as described in the Unix Tutorial). What has happened to the prompting message? Have a look into the file intro.out and make sure that you understand what is there and why. You can view the contents of the file intro.out by typing
% cat intro.out
Or, you can view intro.out by opening it in pico:
% pico intro.out
Create file intro.in with a value of the radius and run the program so that it takes its input from intro.in and writes the output into intro.out:
% ./intro < intro.in > intro.out
Carefully note the syntax of the above command to run the program with its standard input redirected from a file, and its standard output redirected to a file. The order of the two files does not matter, so ./intro > intro.out < intro.in would also be okay, but listing the input file first makes the command easier to read. Note however that intro.in > ./intro > intro.out is not a correct way to accomplish the same; the executable file always comes first.
Modify the program so that it prompts you to enter the radius value as input in centimeters using the prompt "Enter radius (in cm):\n", but displays the result in square inches. (One inch is 2.54 cm exactly.) Use pico to edit the file to make this modification, and save the file as ex1.c inside your lab1 subfolder under the comp411lab folder in your home directory.
Compile this program to create an executable file called ex1 by typing
% gcc ex1.c -o ex1
Run the executable ex1 on a couple of different inputs to verify that it is working correctly. The inputs and outputs can be from the keyboard/terminal; you do not need to use redirection of input/output from/to files for this exercise. Here is an example execution scenario (user input is shown in red, and program output in blue):
% ./ex1
Enter radius (in cm):
2.54
Circle's area is 3.14 (sq in).
Reminder: We always put a "./" before the name of the executable when executing a program in your current working directory, hence ./ex1 above.
Copy the file ex1.c to ex2.c (so you do not accidentally edit ex1.c any further). Modify the program in ex2.c to also compute and display the circumference. Compile it to an executable called ex2, and run it. Here is an example execution scenario:
% ./ex2
Enter radius (in cm):
2.54
Circle's area is 3.14 (sq in).
Its circumference is 6.28 (in).
Copy the file ex2.c to ex3.c. Modify the program in ex3.c to make it work on multiple inputs. In particular, it should repeatedly ask for a radius value (in centimeters), and print the corresponding area (in square inches) and circumference (in inches), until the user enters the value 0 as radius. At that point, the program should print the area and circumference (both 0), and then terminate.
Compile this program to make an executable called ex3, and run it, feeding it several radius values from the keyboard, and verifying that the values it displays are correct. A single run of the program should be able to compute as many of these calculations as you want, until you enter 0.
% ./ex3
Enter radius (in cm):
2.54
Circle's area is 3.14 (sq in).
Its circumference is 6.28 (in).
Enter radius (in cm):
5.08
Circle's area is 12.57 (sq in).
Its circumference is 12.57 (in).
Enter radius (in cm):
0
Circle's area is 0.00 (sq in).
Its circumference is 0.00 (in).
Sample inputs and corresponding outputs are provided under /home/montek/comp411/samples/lab1. You should try running your program on the sample input files provided, and make sure the program's output is identical to that in the sample output files. For example, the sample input files for Exercise 1 are called ex1in1 and ex1in2. Their corresponding outputs are in the files ex1out1 and ex1out2. You can, of course, manually type or cut-and-paste the contents of the input files onto your terminal when running the program. Or, you could feed that sample input to your program using input redirection (./ex1 < ex1in1). To verify that your program is working correctly, compare its output to the test output provided. Again, you could do this comparison manually, or you could use a very useful UNIX command called diff.
The full sequence of steps to run your program and verify its output is as follows. First, copy the sample input and output files to your lab1 folder:
% cd ~/comp411lab/lab1
% cp /home/montek/comp411/samples/lab1/* .
Then do the following:
% ./ex1 < ex1in1 > ex1result1
% diff ex1result1 ex1out1
The diff command prints any differences it finds between your program's output (which you redirected into the file ex1result1) and the correct output provided to you (in the file ex1out1). Only those lines that do not match are shown. Each line in the output is prepended with "<" if it belongs to the first file, and with ">" if it belongs to the second file. See Wikipedia page on diff for more information.
The variation diff -y prints the two files side-by-side (the first one on the left, the second on the right), and highlights lines where they differ. Watch out for extra spaces at end of lines, etc.!% diff -y ex1result1 ex1out1
If you only want to see the lines that differ, you can use the following command:
% diff --suppress-common-lines -y ex1result1 ex1out1
If there are no differences, it prints nothing.
Before submitting your work, be sure that your compiled program runs correctly on all of the sample inputs provided exactly, i.e., diff produces no output at all. You may receive zero credit if your program's output does not exactly match the sample outputs provided. For example, for Exercise 1, two input files are provided: ex1in1 and ex1in2. Their corresponding outputs are also provided: ex1out1 and ex1out2. To check that your program works correctly on both, you may do the following:
% ./ex1 < ex1in1 > ex1result1
% diff --suppress-common-lines -y ex1result1 ex1out1
% ./ex1 < ex1in2 > ex1result2
% diff --suppress-common-lines -y ex1result2 ex1out2
Once you become familiar with diff, you may prefer to invoke it simply without any of the modifiers:
% ./ex1 < ex1in1 > ex1result1
% diff ex1result1 ex1out1
% ./ex1 < ex1in2 > ex1result2
% diff ex1result2 ex1out2
If neither of the diff commands showed mismatches between your program's output and the desired output, then your program is assumed to work fine. If your output happens to have one or more extra space characters at the end of a line, they can be hard to tell. In that case, use the hexdump -c command to display all the characters in your file:
% hexdump -c ex1result1
The hexdump -c command will show all spaces (as spaces), tabs (as \t), newlines (as \n), returns (as \r), etc., thereby making it easier to see whitespace characters.
Repeat such tests for Exercises 2 and 3 using all the input and output sample files provided for each.
Your assignment must be submitted electronically by 11:59pm on Friday, January 19.
You will submit your work on Exercises 1, 2 and 3 (not Exercise 0). Specifically, only the files ex1.c, ex2.c and ex3.c will be collected.
How to check all your work without submitting: Navigate to the folder where these files are stored (~/comp411lab/lab1), and run the following command for checking all your exercises against all the sample inputs and outputs:
% cd ~/comp411lab/lab1
% /home/montek/comp411/bin/selfchecklab1
How to submit: Navigate to the folder where these files are stored (~/comp411lab/lab1), and run the following command for electronically submitting them:
% cd ~/comp411lab/lab1
% /home/montek/comp411/bin/submitlab1
When you invoke the submit script, it again performs a quick check of your work (including compiling, running and comparing the output of your program with the expected output, for all sample input/output files), and provides a quick summary of any errors. Your work is submitted regardless of whether errors were found or not, but you are free to fix any errors and submit again.
Resubmissions: You are allowed to submit a lab assignment as many times as you would like to, but only the latest submission will be considered for grading.
In case of any problems, please contact the instructor or the TAs.