COMP 530: Lab 0: C Refresher

Due 11:59 PM, Friday, September 2, 2016

Introduction

This lab will be a quick refresher on C programming on Linux, and will serve as a warm-up for more intensive programming assignments this semester. You will need to complete these assignments on a department Linux machine.

Working in pairs

You may complete this assignment alone or with one teammate. You may start working alone, and decide to join with a teammate later. Once you start working with a teammate, please do not switch teammates midway through the assignment; you may switch teammates on the next Lab.

Pointer Refresher

One of the hardest parts of moving from programming in a higher-level language, like Java, to C is dealing with pointers. We will start with a few ungraded, but highly recommended, exercise to refresh your understanding of pointers.

Exercise 1. Read about programming with pointers in C. The best reference for the C language is The C Programming Language by Brian Kernighan and Dennis Ritchie (known as 'K&R'). We recommend that students purchase this book (here is an Amazon Link). There is a copy on reserve in the library.

Read 5.1 (Pointers and Addresses) through 5.5 (Character Pointers and Functions) in K&R. Then download the code for pointers.c, run it, and make sure you understand where all of the printed values come from. In particular, make sure you understand where the pointer addresses in lines 1 and 6 come from, how all the values in lines 2 through 4 get there, and why the values printed in line 5 are seemingly corrupted.

There are other references on pointers in C, though not as strongly recommended. A tutorial by Ted Jensen that cites K&R heavily is available in the course readings.

We also recommend reading the Ksplice pointer challenge as a way to test that you understand how pointer arithmetic and arrays work in C.

Warning: Unless you are already thoroughly versed in C, do not skip or even skim this reading exercise. If you do not really understand pointers in C, you will suffer untold pain and misery in subsequent labs, and then eventually come to understand them the hard way. Trust us; you don't want to find out what "the hard way" is.

Input Processing

The main part of the assignment will be to write a C program that reads in a stream of characters from standard input (i.e., the keyboard) and writes them as 80 character lines to standard output (i.e., the "screen") with the following changes:

For this program, a "line of input" is defined as any sequence of zero or more printable and "whitespace" characters (tabs, spaces, and newline characters). Each "80 character line" to be written to standard output is defined as 80 non-newline characters plus a newline character. (Thus, each “80-character line of output” you generate technically will consist of 81 characters.)

Your program should process lines of input until the end-of-file (EOF) is reached, at which point the program should terminate. Note that when reading input, the enter/return (newline) character is counted as an input character. That is, when reading from stdin, every character that is read is counted---including newlines, white-space characters, etc. Thus, an input string of 10 printable characters plus a enter/return (newline) character is an 11-character line of input (even though you only see 10 characters on the console).

Depending on the library function you use to read the input characters, you may need some variant of EOF to test for the end of the file. The libc read() function is actually simplest: returning zero when the end of file is reached

.

Your program should output only 80 character lines (with a newline after each line). For example, if your program has read in a line of input and is waiting for more data in order to make an 80-character line for output, and end-of-file is reached on standard input, then the program should terminate without printing out the partial output line. In addition, your program should not output any user prompts, debugging information, status messages, etc. The only output your program should generate is zero or more 80-character lines.

Note that if you execute your program interactively ( i.e., you type in characters from the keyboard directly into your program), your output may be interleaved on the console (your terminal window) with the input. Do not concern yourself with this! This is perfectly fine. Just concentrate on ensuring that your program only outputs zero or more 80-character lines.

For the second requirement, pairs of percent signs should be replaced as they are seen in the input stream . Thus, the string "abc%%%def" contains only one pair of percents and should be converted to the string "abc*%def". The string "abc%%%%def" contains two pairs of percents and should be converted to the string "abc**def".

The purpose of this assignment is to review the basics of the C programming language and the use of UNIX/Linux program development tools. As stated here, the problem has nothing to do with operating systems. A later assignment will build upon this assignment and turn it into an operating systems problem.

Exercise 2. (8 points) Complete the input processing exercise as described above.

Hint: You can generate an end-of-file (EOF) in an interactive shell with Control+d.

Hand-In Procedure

For all programming assignments you will "turn in" your program for grading by placing it in a special directory on a Department of Computer Science Linux machine and sending mail to the TA alias (comp530ta-f16 at cs dot unc dot edu). To ensure that we can grade your assignments in an efficient and timely fashion, please follow the following guidelines precisely. Failure to do so will potentially result in your assignment receiving a failing score for this assignment.

Log on to a CS Department Linux machine. (Any CS machine will work, however the machines classroom.cs.unc.edu (primary) and snapper.cs.unc.edu (secondary) have been specifically dedicated for use by undergraduates.)

In your Linux home directory, create the directory structure with the following command:

mkdir -p comp530/submissions
(That is, create the directory comp530 in your home directory and inside this directory, create the directory submissions.)

To allow the instructors to access the directory while restricting access to your solution by other students, execute the following commands:

fs sa ~/comp530/submissions system:anyuser none 
fs sa ~/comp530/submissions porter read 
fs sa ~/comp530/submissions sytang read 
fs sa ~/comp530/submissions rohan read 
All subsequent subdirectories you create should inherit these permissions, however, to be safe you should explicitly check that this is the case. If you enter the command:
fs la ~/comp530/submissions/x
(where x is the name of any subdirectory you might create) you should see something like below, assuming your userid is massalin:
Access list for /home/massalin/comp530/submissions/x is
Normal rights:
   system:administrators rlidwka
   massalin rlidwka
   porter rl
   sytang rl
   rohan rl
If you see a line for any other user or a line of the form system:anyuser rl then something is wrong and you should contact either the TA or the instructor immediately.

Important note: You must execute the commands above before proceeding to the next step!

For each assignment you will create a subdirectory with a name specified in the assignment. You must name your subdirectory and your program exactly as specified in the assignment. For this assignment you should name your final program "warmup.c" and store it in a directory named lab0 (inside your ~/comp530/submissions directory). Note that Linux file names are case sensitive and hence case matters!

When you have completed your assignment you should put your program and any other necessary parts (header files, etc.) in the specified subdirectory and send mail to the TA (comp530ta-f16 at cs dot unc dot edu), indicating that the program is ready for grading. Be sure to include "COMP 530" in the subject line. In this email, indicate if you worked alone or with a partner (and both team members' names and CS login names, as many of you have emails different from your CS login); be sure to cc your partner on this email. If you used any late hours, please also indicate both how many late hours you used on this assignment, and in total, so that we can agree.

After you send this email, do not change any of your files for this assignment after sending this mail (unless you are re-turning in late, as prescribed by the lateness policy).! If the timestamps on the files change you wi ll be penalized for turning in a late assignment. If your program has a timestamp after the due date it will be considered late. If you wish to keep fiddling with your program after you submit it, you should make a copy of your program and work on the copy and should not modify the original.

All programs will be tested on classroom.cs.unc.edu. All programs, unless otherwise specified, should be written to execute in the current working directory. Your correctness grade will be based solely on your program's performance on classroom.cs.unc.edu. Make sure your programs work on classroom!

Generally, unless the homework assignment specifies otherwise, you should compile your program without using any special command line arguments ("flags") or compiler options.

The program should be neatly formatted (i.e., easy to read) and well-documented. In general, 75% of your grade for a program will be for correctness, 25% for "programming style" (appropriate use of language features [constants, loops, conditionals, etc.], including variable/procedure/class names), and documentation (descriptions of functions, general comments [problem description, solution approach], use of invariants, pre-and post conditions where appropriate). For this first assignment, correctness and programming style/documentation will each count for 50% of your grade.

Make sure you put your name(s) in a header comment in every file you submit. Make sure you also put an Honor pledge in every file you submit.

This completes the lab.


Last updated: 2016-11-21 21:42:42 -0500 [validate xhtml]