CS202 - Assignment One

Due: 2016-02-24 11:55p

Objectives

[8 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 |
+-------+------------+

[7 points] Harden the conversion program

The most logical way to have done the first section is to have started with 02-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.

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.

[2 points] Challenge: Allow mile values larger than 99999

Challenge problems are problems that are optional, but will make the difference between a solid high B and an A

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.


Turning in your work

Call your file hw1.c. Make sure that the assignment runs on Linux before you submit it. Do not assume that it will – check it. Submit the completed assignment on Moodle.