Computer Science 340
Programming Languages

Fall 2019, Siena College

Problem Set 1: C Programming
Due: 11:59 PM, Friday, September 20, 2019

In this assignment, you will implement some fairly significant programs in C. Hopefully, none of the tasks will be especially difficult to understand, so you can concentrate on how to implement them in C. That will undoubtedly present plenty of challenges. Consider yourselves thrown into the deep end.

You may work alone or with a partner on these tasks.

Do not hesitate to ask your instructor for help when (note the choice of "when" over "if") you get stuck.

On noreaster.teresco.org, you can find and run the executables for my solution code for each program in /home/cs340/probsets/ps1/ .

Commit and push often, and use meaningful commit messages.

Getting Set Up

You will receive an email with the link to follow to set up your GitHub repository for this problem set (ps1-yourgitname). If you are working on this problem set with a partner, one member of the group should follow the link to set up the repository on GitHub. Your problem set partner will receive a subsequent email with a link to follow that will grant them the rights to clone the repository and commit and push changes to the origin on GitHub.

By 11:59 PM, Friday, September 13, 2019, all repositories must be created and if working in a group, the partner who did not create the repository must have write access to the repository. Your name(s) must be in the repository by this deadline as well.

Your repository should include a directory examples, in which you will find several C programming examples that might be useful as you develop your solutions.

Argument Adder

You have likely seen Java applications that take command-line parameters (the String args[] parameter to main). Write a C program that takes an arbitrary number of command-line parameters, each of which should represent an integer value. Print out the sum of the values provided. Call your C program argadder.c.

Some things to know:

Input Adder

Next, we will create a modification of the "adder" program to take its input from the keyboard rather than from the command line. Your program should read in integer values one by one, accumulating a sum as you go, until you encounter an invalid (non-integer) or the end of the input (someone types Ctrl-d). At that point, print out the sum and exit.

There are several ways to read in values from the input, but we will use scanf. Some details about scanf may be found in the C example in the repository that computes greatest common denominators.

Hint: you may wish to control your program with a loop that continues as long as your scanf calls continue to report that there was a number successfully matched and stored in a variable.

Call your C program inputadder.c.

Extended Input Adder

For one more bit of practice before getting to the somewhat more interesting tasks, extend your input adder program to remember all the values read in by storing them in a dynamically-allocated array. At the end, the program should print the addition problem and its solution in a standard format. For example, given the input values 9, 4, and 5, the program would print

9+4+5=18

Your task here is to be able to create the array and continue to make it larger as needed. Start with a reasonably-sized array (perhaps 10 slots). If the user inputs 10 or fewer values, you are all set. But if an 11th value is input, you will need to resize the array to make space. Use the same strategy as Java's Vector or ArrayList class - double the size of the array each time it fills.

Some things to consider:

Call your C program inputadder2.c.

Implementing Some Unix Commands

Your programming tasks are to implement clones of 4 Unix commands in C.

File Copy
 

cp filenameA filenameB - copy the contents of filenameA to a file named filenameB; see the Unix cp command. You need not implement any additional command-line options. A simple copy of one file to another is sufficient. You should report errors meaningfully.

Write this in a C file cp.c.

Hints:

Display File Contents
 

cat [OPTION] filename - display the contents of filename, where the options can be either (or neither) of:

Note: you should not make any assumptions about the maximum length of a single line of the input file. You may wish to write your own function that reads a line from the file into an array you maintain to be the correct length. Your resizeable array from inputadder2 should provide a good model for how to proceed.

Note also that line numbers start at 1. When line numbers are specified, the line number should occupy the first 6 columns and be followed by 2 spaces before the line from the file. A format string conversion specifier of %6d in printf will print a number such that it will take 6 characters of output, using spaces to pad on the left if needed (i.e., exactly what you want).

See the Unix cat command.

Write this is a file cat.c.

Display Initial Lines of File
 

head [-n] filename - display the first n lines of filename, default is to display 10 lines if the flag is not specified. If the file has fewer than the requested number of lines, the entire file should be displayed.

See the Unix head command.

Word Count
 

wc filename - find the number of lines, words, and bytes (characters) in filename.

Notes: Your function from cat that reads in a line will likely prove useful here. Look at strsep for a way to separate a string into words by separating on whitespace. Note that blank lines count as a line, but a blank line contributes no words to the total. Also, the new line character at the end of each line should be included in the character count.

See the Unix command wc.

Write this in a file wc.c.

General Requirements

Your code should be commented appropriately throughout. Please also include a longer comment at the top of your program describing your implementation. And, of course, it should include your name.

All of your programs should compile without warnings using gcc on noreaster when the -Wall flag is included. This flag turns on extra warnings that will help you avoid some of the pitfalls of C programming. If you encounter any warnings that you don't know how to fix, ask!

Any memory you allocate with malloc or realloc should be returned to the system with a corresponding free call.

Include a Makefile that compiles all 7 programs with the -Wall flag. This Makefile should produce 7 executable programs: argadder, inputadder, inputadder2, cp, cat, head, and wc. My Makefile is on noreaster.teresco.org in /home/cs340/probsets/ps1/ . Please feel free to use or modify as you see fit.

Submission

Commit and push all of your code to your group's GitHub repository. That's it.

Grading

This problem set will be graded out of 65 points.

Grading Breakdown

argadder program correctness 3 points
inputadder program correctness 3 points
inputadder2 program correctness 5 points
cp program correctness 10 points
cat program correctness 10 points
head program correctness 10 points
wc program correctness 10 points
Program documentation 7 points
Program efficiency, style, and elegance 6 points
Working Makefile 1 point