lab04 for this lab~wcbrown/courses/IC221/labs/L04/lab04.tar.gz
mma.c
and compile from it an executable
program named mma.
The program computes the minimum,
maximum, and average of the the real numbers it reads from
stdin. Your program may not assume there are a particular
number of input values: it must work for ANY number of
values. Use tabs to separate output values, and print results
to three decimal places.
Here's the expected beahvior and output for the input file data.txt:
chessie [80] [~/]> mma < data.txt 2.350 999.268 497.530 chessie [81] [~/]>(Recall that we're using the shell here to do input redirection: you're writing mma so it reads from stdin, but the shell arranges for data.txt to be the actual source of the data) Note that we could have accomplished the same thing, this way:
chessie [82] [~/]> cat data.txt | mma 2.350 999.268 497.530 chessie [83] [~/]>(Recall that we're using the shell here to pipe stdout of cat to stdin of mma) Here's the expected behavior for an input file that has no data (i.e., is empty):
chessie [84] [~/]> mma < empty.txt
mma: Error - empty input, min/max/average undefined!
chessie [85] [~/]> echo $?
1
Your program should follow the Unix conventions we've discussed so far:
mma.c to add the
four command line options:
--min, --max, --ave and --help. If
one or more of the first options is present on the command line,
the corresponding result is printed.
If the last option (i.e. --help) is given, the program should
print out a description of what the program does and how to
use it, then exit immediately. The exact message is
mma [--min] [--max] [--ave] [--help]Here's the expected behavior and output for the input file
data.txt:
chessie [80] [~stahl]> mma --min < data.txt
2.350
chessie [80] [~stahl]> mma --max < data.txt
999.268
chessie [80] [~stahl]> mma --max --ave < data.txt
999.268 497.530
chessie [80] [~stahl]> mma --max --max < data.txt
999.268 999.268
chessie [80] [~stahl]> mma --sum --ave < data.txt
mma: Error! invalid option --sum
497.530
chessie [80] [~stahl]> mma < data.txt
2.350 999.268 497.530
chessie [80] [~stahl]>
Here's the expected behavior for an input file that has no data (i.e., is empty):
chessie [80] [~stahl]> mma < empty.txt
mma: Error - empty input, min/max/average undefined!
chessie [80] [~stahl]>
Note that mma invoked with no options prints out all three
quantities, separated by tabs, to three decimal places -
nothing more, nothing less.
mma invoked with duplicate options prints the requested information as many times as it is requested.
mma invoked with an invalid option prints the error message as
shown, to stderr, but otherwise simply ignores the invalid option.
mma.c file so that
the mma program can alternately read from a file
whose name is given on the command-line. You may assume that
any string that does not begin with "--" is to be
treated as an input file name. Without a filename on the
command-line, mma reads from stdin,
just as before.
chessie [81] [~stahl]> mma data.txt
2.350 999.268 497.530
chessie [82] [~stahl]> mma < data.txt
2.350 999.268 497.530
chessie [83] [~stahl]> mma --max data.txt
999.268
chessie [84] [~stahl]> mma lkjlkjlkj
mma: Error! File "lkjlkjlkj" not found!
chessie [85] [~stahl]> mma --sum --ave data.txt
Error: invalid option --sum
497.530
chessie [86] [~stahl]> mma --help
mma [--min] [--max] [--ave] [--help] [filename]
Note: If you're clever, this can be done with very little modification to your original code. Some additions, but very little within the code you've already written needs to be changed.
lab04.
This must include the file mma.c, and the input
files dat.txt and empty.txt that
came from untarring the file lab04.tar.gz, and a
text file called README that includes your name
and alpha code, a clear statement of whether your submission
is a Part 1, Part 2 or Part 3 submission, and the answers to the
following questions (20 Points):
1. What information does the value "returned" by a process usually convey?
2. Why would it be a bad idea to print out 0 for the min/max/ave
values when the input is empty?
3. Assuming the declarations:
int k, n;
double x, y;
char *s;
char *t;
Give the type of each of the following expressions assuming
a. fscanf(stdin,"%i %lf",&k,&x)
b. s
c. t[3]
d. &t
e. k/n
f. x/n
g. *s
h. malloc(10*sizeof(double)
i. fopen("foo.txt","w")
j. fclose(stdout)
k. stdout
4. What does it mean if strcmp(s,t) returns a negative number?
HINT: RTFM (i.e. Read The F****** Manpage).