Credits: Based on Problem 4.2 from Tan and D'Orazio.
This project is based on a problem from Tan and D'Orazio. We first give you the original problem statement, and then introduce some variations on it.
Cost analysis is an important part of engineering. You may be asked to write
programs to determine the minimum cost for a number of different potential
circumstances. Your programs can be used as decision making
tools for a project.
Consider building an airport with the runway built on landfill. The contractor
has two dump trucks, one with a capacity of 8 tons and the other with a capacity
of 12 tons. The contractor uses the trucks to haul fill from a remote site
to
the airport location. The operating cost per trip for the 8
and 12 ton trucks is $14.57 and
$16.26, respectively. One truck cannot make more than 60 percent of the total
trips.
Write a program that develops the minimum cost for a given number of tons. Prompt the user to enter the total number of tons. Display the number of trips required for each truck and the total cost.
The problem definition isn't clear on what to do with less than 8 tons, or between 8 and 12 tons. Common sense dictates that for loads of less than or equal to 12 tons, use only 1 truck (the least cost truck). Another reasonable exception to the 60/40 rule is when the number of trips between the two trucks differs only by 1. So, for example, small loads, 2 trips with truck A, and 1 trip with truck B is permissable, even though that is not a 60/40 split.
This is an interesting problem. The input is a single
number (the number of tons). The output is three numbers: the total number
of trips required
for
each of the two trucks, and the total cost.
Before thinking about how you would program
this problem in C, take a few examples and work them out by hand. If you
just had pencil and paper, and you
were given the problem of determining a schedule for 5 tons, or 15 tons,
or 150 tons, or 2000 tons, how would you approach it?
Then, try to sketch out,
in English, some instructions that you would give someone to solve the problem
using your approach. Then, convert your English into pseudocode, and finally
into C. Does your approach use any kind of "repeating" step? If so,
you may want to use
a while
loop or
a
for
loop.
Is
there a decision
to be made? In that case, an if test may be useful.
To fully test your program, we want to test it on many different values of the "number of tons". To do that, we can structure the program in the following way.
The main program will open an input file consisting entirely of numbers representing "number of tons", one per line, ended with a sentinel value of 0. It will also output an output file. Use "tons.txt" as the name of the input file, and "costReport.txt" as the name of the output file.
You will write a function called "computeScheduleAndPrint" with the following function prototype:
void computeScheduleAndPrint(double numberOfTons, FILE *outfile);
This function will take a number of tons as input, and write one line to the
output
file pointed to by the variable outfile. Each of these lines will contain four
numbers:
These four numbers will be separated by spaces. There are no headings or labels on the output, because this output file will be read as input by another program.
So the main program's job is to open the two files (using the command line arguments), then go into a while loop, reading from the input file until the sentinel value is reached. For each line in the input file, read the number of tons, pass that value and the pointer to the output file to the function, which will write one line to the output file. When the while loop is completed, close both files, and you are finished.
Call your .c file proj1.c. (or rename it to that.)
Create a proj1.txt script file showing that the program works. By now, you should have a good idea of what belongs in a script file. Consult your previous labs and speak with your TA during your scheduled lab if you are not clear on that.
Submit your proj1.c file and proj1.txt file (script) to WebCT. Print your script file and give to your TA.
When you are finished with this project, you have not necessarily seen the last of this problem. Future lab assignments may build on what you have accomplished here.