CS 22300 - Programming Assignment 1

This assignment makes use of files contained in this zip file. This assignment is due Thursday, September 8.

In this assignment you will write a C filter program that formats a stream of input numbers into nicely organized columns. Your program will make use of command-line arguments and environment variables to determine parameters for the output formatting.

Write a C program called filter.c that reads from standard input and writes to standard output. The input to your program will be decimal numbers separated by white space. The output of your program will be the input numbers formatted into columns.

The decimal point of every number in a column must line up. The number of digits after a decimal point is called the precision of a decimal number. All the numbers in the output will have the same precision. The default precision for your output is 13 decimal places. If there is an environment variable called CS223HW1PRECISION, then the value of that variable overrides the default precision. If there is a command line parameter, then that command line parameter overrides both the environment variable (if there is one) and the default value of precision.

The default number of output columns for your output is three. If there is an environment variable called CS223HW1COLUMNS, then the value of that variable overrides the default number of columns. If there is a second command line parameter, then that command line parameter overrides both the environment variable (if there is one) and the default number of columns.

Your program should use the Standard C Library function getenv() to see if there are environment variables named CS223HW1PRECISION or CS223HW1COLUMNS. If either one exits, its string value should be converted to an integer value by using the Standard C Library function atoi() ("atoi" is an abbreviation of "ascii to integer")..

Your program will read in values from standard input by using the Standard C Library function scanf() and its associated formatting strings.

Your program will write formatted output numbers (with the decimal points lined up and the proper number of decimal digits) to standard output by using the Standard C Library function printf() and its associated formatting strings. (Hint: You will need to make use of the * character in your format strings.)

Formatted input and output is not as hard as it might seem by looking at the last few references. After a few examples, you quickly get the hang of it.

Here are a couple of examples. If the input stream to your program looks like this:

5163.1575670644243                 6806.8180791650138
                 8977.2739646595655
      2598.0792870876185                 7162.5237586596268          6691.2041993469038
 1043.6422009949033
    6922.8216193121125          3.0480056154058  9926.6081118198181  100.3082369457076
5135.1567125461588      7808.2382885219886     1439.6542863246559           249.6179692983795
  214.0065309610279                 9280.5883968626968             2687.3871883297220
7612.8426770836513         6644.2027649769589  8364.5604419080173         4740.7550279244360
254.6181218909269         2500.3814813684498
      2293.6803491317482                  835.3306680501725
        5962.7923215430155    9622.5988341929387    57.3069246498001
        1557.9630726035341                8398.5614795373385         5958.4870143742182
   2568.3835566270945             9935.9135715811644              3410.1040681173131
982.0299691763055
  8393.5613269447913           9066.2766808069100                 4896.4546037171549
     7597.8422193060087            8551.5661488692895   1076.6432081057162
 1911.3635059663686         7586.8418836024048       9282.8936429944770        4696.1433149204995
 1388.0423596911528
                 1936.3642689291055     3408.4091921750542                   3556.4137089144565
     9241.8923917355878          5003.4578691976685               3366.7130954924160
  4270.1303140354621   620.6292916653950    4700.7538071840572         1766.0538956877347
              441.6238288521989            8153.8591875972779

then the default way to format this is with three columns and 13 digits of precision.

5163.1575670644243   6806.8180791650138   8977.2739646595655
2598.0792870876185   7162.5237586596268   6691.2041993469038
1043.6422009949033   6922.8216193121125      3.0480056154058
9926.6081118198181    100.3082369457076   5135.1567125461588
7808.2382885219886   1439.6542863246559    249.6179692983795
 214.0065309610279   9280.5883968626968   2687.3871883297220
7612.8426770836513   6644.2027649769589   8364.5604419080173
4740.7550279244360    254.6181218909269   2500.3814813684498
2293.6803491317482    835.3306680501725   5962.7923215430155
9622.5988341929387     57.3069246498001   1557.9630726035341
8398.5614795373385   5958.4870143742182   2568.3835566270945
9935.9135715811644   3410.1040681173131    982.0299691763055
8393.5613269447913   9066.2766808069100   4896.4546037171549
7597.8422193060087   8551.5661488692895   1076.6432081057162
1911.3635059663686   7586.8418836024048   9282.8936429944770
4696.1433149204995   1388.0423596911528   1936.3642689291055
3408.4091921750542   3556.4137089144565   9241.8923917355878
5003.4578691976685   3366.7130954924160   4270.1303140354621
 620.6292916653950   4700.7538071840572   1766.0538956877347
 441.6238288521989   8153.8591875972779

On the other hand, if we use seven columns and only two digits of precision, the same input should produce the following output.

5163.16   6806.82   8977.27   2598.08   7162.52   6691.20   1043.64
6922.82      3.05   9926.61    100.31   5135.16   7808.24   1439.65
 249.62    214.01   9280.59   2687.39   7612.84   6644.20   8364.56
4740.76    254.62   2500.38   2293.68    835.33   5962.79   9622.60
  57.31   1557.96   8398.56   5958.49   2568.38   9935.91   3410.10
 982.03   8393.56   9066.28   4896.45   7597.84   8551.57   1076.64
1911.36   7586.84   9282.89   4696.14   1388.04   1936.36   3408.41
3556.41   9241.89   5003.46   3366.71   4270.13    620.63   4700.75
1766.05    441.62   8153.86

In the zip file for this assignment you will find an program called source.c that you can use to test your program. The program source.c writes to standard output a stream of random decimal numbers (all with 13 digits of precision). The numbers are separated by random amounts of white space and there are a random number of random numbers on each line of output. You can test your filter.c program by "piping" the standard output of source into the standard input of your filter. For example, the first output shown above might have been created by a command like the following.

     > source | filter

On the other hand, the second output above could have been produced by the following two shell commands.

     > set CS223HW1COLUMNS=7
     > source | filter 2

or by the following single shell command.

     > source | filter 2 7

If you have never worked with filters and pipes before, the last command is equivalent to the following two commands (that use I/O redirection).

     > source > temp
     > filter 2 7 < temp

The first command "redirects" the standard output from source into the temporary file temp and then the second command "redirects" the contents of temp into the standard input of filter. (How would you save the resulting output from the filter in a file called data.txt?) The piped version of the command has the advantage of not needing to use a temporary file.

In the zip file for this assignment you will also find two Windows executable programs called source.exe and filter.exe (and two Linux executable programs called source and filter) that you can use to demo this assignment on a Windows (respectively Linux) computer.

Here is an outline of how you can go about attacking this problem.

  1. First, get your filter to print all of the input numbers in a single column with their decimal points lined up (and all 13 digits of precision).
  2. Second, have a command line argument determine the precision (in your single column of output).
  3. Third, have the environment variable determine the precision (in your single column of output).
  4. Fourth, create three columns of output. Be sure to take into consideration that the last row need not have three columns.
  5. Last, let a command line argument or an environment variable determine the number of columns.

Turn in a zip file called CS223Hw1Surname.zip (where Surname is your last name) containing your version of filter.c.

This assignment is due Thursday, September 8.

Below are some references about I/O redirection, command-line arguments and environment variables.