int main(int argc, char *argv[]) instead of int main(void) | Files you will use from last week's lab | lab04a.cc |
|---|---|
| Files you will create and submit via WebCT (also print ones in bold) | lab05a.cc, lab05a.txt |
In Part 1 of this lab, you will take the wind chill program you did in lab04 (lab04a.cc) and modify it to use command line arguments.
This raises a question: What Unix command will you use to make a copy of your lab04a.cc program from your lab04 directory and put that copy into a file called lab05a.cc in your lab05 subdirectory?
The correct answer depends on your current directory. Listed below are several attempts. Some of them will work, and some will not. Think about whether each of these is correct or not (you'll have a question similar to this on your first midterm exam, so take advantage of the opportunity to practice). Print a copy of this page of the lab, and circle for each of these whether you think the command is correct or incorrect.
If you are not sure, ask your TA for help before proceeding. Also, you may consult with other students on THIS ASPECT OF THE LAB only. (Or, your instructor might go over this part of the lab in lecture before lab starts.). Unless you are told otherwise in lecture or lab, you do not have to turn in your answers "correct or incorrect"; these are just practice for the exam. Once you have found at least one of the correct answers, use that command to create your lab05a.cc file as a starting point for this lab.
READ INSTRUCTIONS (not all of these comands are correct) |
|
cd ~/cisc181/lab04 |
correct or incorrect? |
| cd ~/cisc181/lab04 cp lab04a.cc ../lab05/lab05a.cc |
correct or incorrect? |
cd ~/cisc181/lab05 |
correct or incorrect? |
| cd ~/cisc181/lab05 cp ../lab04/lab04a.cc . mv lab04a.cc lab05a.cc |
correct or incorrect? |
When you are done, you want to be able to run the windchill program in the following ways:
./windChill |
same output as before |
./windChill 5 60 5 |
values 5, 10, 15, 20, 25 ... up to 60 down the left column (just like in the table on the web site.) |
./windChill 2 10 1 |
values 2, 3, 4, 5, 6, 7, 8, 9 and 10 (8 rows of wind chill factors) |
./windChill 20 40 |
values 20 30 40 only (the step value defaults to 10) |
./windChill 30 |
values 30 40 50 only (max defaults to 50, step defaults to 10) |
Here's how to do it:
int main(void)
at the top of your program to
int main(int argc, char *argv[])
// initialize from command line arguments; default to 10, 50 and 10 int windMin, windMax, windStep; windMin = (argc <= 1)? 10 : atoi(argv[1]); windMax = (argc <= 2)? 50 : atoi(argv[2]); windStep = (argc <= 3)? 10 : atoi(argv[3]);
./windChill 10 50 10 for example,
you will get the same table as before. However, you should now be able to
run the program with commands such as the following:| Files you will use from the previous step | lab05a.cc |
|---|---|
| Files you will create and submit via WebCT (also print ones in bold) | lab05b.cc, lab05b.txt |
According to a news article on the USA Today web site, the U.S. National Weather Service and Canadian weather authorities modified the formula for calculating the wind chill factor in the Fall of 2001, based on "greater scientific knowledge".
(Link to the article: http://www.usatoday.com/weather/resources/basics/windchill/wind-chill-formulas.htm)
In this exercise, you will modify your previous wind chill program so that it can produce a table of the old wind chill factor, a table of the new wind chill factor, and a table that shows the difference between the two.
Copy your lab05a.cc file to lab05b.cc.
Then
modify
the lab05b file in the following manner:
enum WindChillOption {WCO_NEW = 0,
WCO_OLD = 1,
WCO_DIFF = 2};
This enumeration will be use to control what type of table is printed.
void printTableHeader(WindChillOption opt)
{
switch(opt)
{
case WCO_NEW:
cout << " ****** NEW WIND CHILL FORMULA ******" << endl;
break;
case WCO_OLD:
cout << " ****** OLD WIND CHILL FORMULA ******" << endl;
break;
case WCO_DIFF:
cout << " ****** DIFFERENCE (NEW - OLD) ******" << endl;
break;
default:
cerr << "Unexpected case! Line number "
<< __LINE__ << " in file " << __FILE__ << endl;
exit(-1);
}
}
This function prints a different header for each kind of table that you might
want to print. You pass in one of the values WCO_NEW, WCO_OLD, or WCO_DIFF
to get the kind of header you want.void printTable(WindChillOption opt, int windMin, int windMax, int windStep)
{ ... }
int main(int argc, char *argv[])
{
// initialize from command line arguments; default to 10, 50 and 10
int windMin, windMax, windStep;
windMin = (argc <= 1)? 10 : atoi(argv[1]);
windMax = (argc <= 2)? 50 : atoi(argv[2]);
windStep = (argc <= 3)? 10 : atoi(argv[3]);
// print three tables:
// one for new wind chill
// one for old wind chill
// one showing difference between them
printTable(WCO_NEW, windMin, windMax, windStep);
cout << "\n\n"; // skip a couple of lines
printTable(WCO_OLD, windMin, windMax, windStep);
cout << "\n\n"; // skip a couple of lines
printTable(WCO_DIFF, windMin, windMax, windStep);
return 0;
}
Note that most of the work is done by the functions that you call; the main
is short and sweet; it is like an "outline" of what happens in the program.
double tableEntry(int windMph, int tempF, WindChillOption opt) { switch(opt) { case WCO_NEW: return windChillNew(windMph, tempF); break; ...
> CC lab05a.cc -o windChill
> ./windChill
****** NEW WIND CHILL FORMULA ******
Temperature (degrees F)
------------+-------+-------+-------+-------+-------+-------+
wind (mph) | 40 | 30 | 20 | 10 | 0 | -10 |
------------+-------+-------+-------+-------+-------+-------+
10 | 34 | 21 | 9 | -4 | -16 | -28 |
20 | 30 | 17 | 4 | -9 | -22 | -35 |
30 | 28 | 15 | 1 | -12 | -26 | -39 |
40 | 27 | 13 | -1 | -15 | -29 | -43 |
50 | 26 | 12 | -3 | -17 | -31 | -45 |
****** OLD WIND CHILL FORMULA ******
Temperature (degrees F)
------------+-------+-------+-------+-------+-------+-------+
wind (mph) | 40 | 30 | 20 | 10 | 0 | -10 |
------------+-------+-------+-------+-------+-------+-------+
10 | 28 | 16 | 4 | -9 | -21 | -33 |
20 | 18 | 4 | -10 | -24 | -39 | -53 |
30 | 13 | -2 | -17 | -32 | -48 | -63 |
40 | 10 | -5 | -21 | -37 | -53 | -68 |
50 | 9 | -7 | -23 | -39 | -55 | -71 |
****** DIFFERENCE (NEW - OLD) ******
Temperature (degrees F)
------------+-------+-------+-------+-------+-------+-------+
wind (mph) | 40 | 30 | 20 | 10 | 0 | -10 |
------------+-------+-------+-------+-------+-------+-------+
10 | 5 | 5 | 5 | 5 | 5 | 5 |
20 | 12 | 13 | 14 | 15 | 17 | 18 |
30 | 15 | 17 | 19 | 20 | 22 | 23 |
40 | 16 | 18 | 20 | 22 | 24 | 26 |
50 | 16 | 18 | 20 | 22 | 23 | 25 |
>
|
| Files you will copy from my web page: | readUsersFromFile.cpp, usernames.txt |
|---|---|
| Files you should look at on my web page (for reference) but don't necessarily need to copy into your directory | sample_usernames.html |
| Files you will create and submit via WebCT (also print ones in bold) | lab05c.cc, lab05c.txt |
| Files you will place on your web page when finished under the URL http://udel.edu/~userid/cisc181/lab05 | usernames.txt, usernames.html |
We already know how to read input from the keyboard (standard input) using operations such as:
cin >> x;
However, it is more interesting and challenging to write programs that process many hundreds, thousands, or millions of lines of input. When you write such programs, you really test whether your program "scales" or not... that is, is the solution that works well for 10 pieces of input is still a good solution for 10,000 pieces of input. So, we are going to learn how to read out input directly from a file on the disk.
As a side note: An example of a well-known algorithm that doesn't scale well is bubblesort: it scales with n-squared, meaning that if you go from 10 pieces of input to 20,000 pieces of input, your program doesn't take about 2000 times longer; rather, it takes about 4,000,000 times longer (since 2000 squared is 4,000,000).
Instructions:
readUsersFromFile.cpp from the lab05 directory on my website into your directory.
userInputFile >> x
will look in the file "usernames.txt" for input instead of looking for that input from the keyboard.
lab05a.cc (50 pts)
lab05b.cc (50 pts)
lab05c.cc (50 pts)
Total: 150 points