Due February 17, 2003
For this assignment, you should compile all of your programs with gcc -Wall...
and your programs should generate no errors or warnings when compiled this way.
There will be some marks for style (especially in part 2). Your program should be easy to "read"; variables should have good names; symbolic constants should be used when appropriate; there should be comments used to clarify parts that are harder to understand.
All floating point calculations should be done with double
s.
Part 1: Skill Building
In this part, you should familiarize yourself with some of the constructs we have been discussing in lectures.
- Ask the user for an integer. The program should do some output, depending on the value entered:
The required input and outputs for the program. Input Output 1
That's one.
2
A pair.
12
A dozen.
-1
That's negative one.
anything else I have nothing to say.
Write a program called
a31.c
to do this, using anif/else-if/else
conditional. -
Write a program called
a32.c
to do the same thing, using aswitch/case
conditional. -
Ask the user for three integers, a, b, c. The program should count from a to b in steps of c. For example (these are four separate runs of the program):
1 8 1
1 2 3 4 5 6 7 8
2 10 2
2 4 6 8 10
20 0 -4
20 16 12 8 4 0
0 10 3
0 3 6 9
a33.c
to do this, using awhile
loop. - Write a program called
a34.c
to do the same thing, using afor
loop.
Part 2: Data Analysis
For this part, you will have to write a program called data.c
to do some data analysis.
Your program must read in a data file with several columns, each separated by some whitespace.
The data file contains two different kinds of data rows.
The first column is a character which indicates the observation type. The observation type must be either A
or B
.
A type 'A' observation has these columns:
- observation type (a character, 'A')
- the length (a double)
- the width (a double)
A type 'B' observation has these columns:
- observation type (a character, 'B')
- the count (an int)
- the length (a double)
- the width (a double)
A few lines from the data file might look like this (the "columns" don't actually have to line up in columns, fscanf
figures it out):
A 10.2 5.5 A 8.4 3.4 B 4 3.4 8.4 A 2.3 7.8 B 3 6.43 2.32
We will assume that the lengths and widths describe the dimensions of a rectangle. For type 'B' observations, the count will indicate the number of these rectangles there are. We want to calculate the total area described by the data file.
In this case the area from type 'A' observations is 10.2*5.5 + 8.4*3.4 + 2.3*7.8 = 102.6. The total area from type 'B' observations is 4*3.4*8.4 + 3*6.43*2.32 = 158.9928. So, the total area for this data file is 102.6 + 158.9928 = 261.5928.
The data file might have some errors in it. If a line of data doesn't have the correct format, all of the data on the line and all the data after it should be ignored. Here are some bad lines of data:
C 3.3 4.2 B no data snargle
When the program detects an error, it should print a message to the user that the file contained bad data and stop reading the file. It should output the data for the file up to that point.
Your program should produce output like this:
Type A observations: 3 Type B observations: 2 Total area: 261.59
The floating point value should be formatted %9.2f
.
Some sample data files, with output, can be found on the web site.
When you submit the program, it should read a file called a3data.txt
in the current directory, but obviously you can change this while you're testing it.
You won't be able to write this program all at once. Anyone who comes and asks for help with a large program that's obviously never been compiled will be told to go away. Here is a suggested plan of attack. Each step here should be accomplished by several add-and-test cycles.
- Start the program and get it working for files with only type A observations.
- Modify the program so it reads the type first, then deals with the rest of the data line.
- Put your type 'A' handling in a conditional statement that checks for a type A line first. If you give this program a file with type A and B lines, it should only pay attention to the type A lines.
- Add handling of type B calculations.
- Put in the error handling code.
Submitting
You have to use the Submission server to submit your work. You should submit the files a31.c
, a32.c
, a33.c
, a34.c
, data.c
.
You can do this by typing these commands:
tar cvf a3.tar a31.c a32.c a33.c a34.c data.c
gzip a3.tar
Then, submit the file a3.tar.gz
.