CS202 - Assignment One
Due: 2018-09-19 09:00a
Solution
Objectives
- Make sure you can compile and run C programs
- Get some basic familiarity with C syntax
- Learn some more about printf
- Learn about
man
- Think about defensive programming
[25 points] Print out a conversion table
Write a program that prints out a table showing the conversions from miles to kilometers. The program will take three integer arguments: start
, end
and step
. These control which values will appear in the table. The first value should be start
, and the list should contain miles up to (but not including) end
. The step
input controls the step size between entries in the table.
The table should look exactly like mine. The miles and kilometers should be right aligned and the kilometers should be expressed with two digits worth of decimal precision. This can (and should) be done with formating within printf
(you will need to do a little research). Don't worry about mile values greater than 99999 (which will overflow the formatting). Example:
> ./hw1 1 20 2
+-------+------------+
| miles | kilometers |
+-------+------------+
| 1 | 1.61 |
| 3 | 4.83 |
| 5 | 8.05 |
| 7 | 11.27 |
| 9 | 14.48 |
| 11 | 17.70 |
| 13 | 20.92 |
| 15 | 24.14 |
| 17 | 27.36 |
| 19 | 30.58 |
+-------+------------+
[18 points] Harden the conversion program
The most logical way to have done the first section is to have started with power.c
as a template. This is a fine approach. However, The code I wrote in class was intended to illustrate some basic functionality and was not remotely robust. We would like your program to detect when it has been given bad input and fail gracefully.
- Not crash when given non-numerical data
- Not crash when insufficient data is provided
- Allow
step
to be optional (i.e., step is set to 1 if not provided)
When I say that your program should fail gracefully, I mean that it should print out a message explaining why it cannot carry out its function and then exit.
To handle non-numerical data, you will have to learn a little bit more about the atoi
function that I used. While you could open up a web browser and look it up, I want you to learn about another resource that is closer at hand: the man
pages. There is a tremendous amount of documentation tucked away on all *nix systems in the man
pages. You can access them by typing man
topic on the command line. You will find that there is documentation for pretty much every standard C library function and command line tool you might use hiding away in there. It should be your first stop if you want to know what parameters a function takes, what it does, and what it returns. Some man
pages provide you with usage information, and sometimes they will even suggest better alternatives.
Add a block comment at the top of the file (between the documentation comment described in the submission instructions and the code). Use this comment to list every modification you made to the code to harden it.
[5 points] Challenge: Allow mile values larger than 99999
Challenge problems are problems that are nominally optional, but will make the difference between a solid high B and an A (i.e., they are still part of the total possible points)
Adjust your program so that if the miles exceed 99999, the table width adjusts. The columns should always be as narrow as possible and still display either the column heading or the largest value in the column with a space on either side.
[2 points] Follow the submission directions
- The file must be called uid_hw1.c, where uid is your Middlebury username.
- Make sure that the assignment runs on Linux before you submit it. Do not assume that it will -- check it.
- At the very top of the file, you will place a block comment that follows this format exactly:
/*
Assignment 1
Name:
Email:
Date:
Collaborators:
To compile:
To run:
*/
The name
, date
, and email
fields should be self explanatory. In the collaborators
field, you should list everyone who helped you with this assignment including classmates, tutors, and myself. In the to compile
and to run
fields, provide the commands required to compile and execute your code (i.e., someone should be able to copy these commands directly onto a command line and run your code). The to run
should include usage instructions (what arguments need to be provided and what they do).
The assignment should be submitted on Canvas.