Files for this lab come from the subdirectory:
~pconrad/public_html/cisc181/05S/work/labs/lab04
To get started, you should copy all the files from that subdirectory into your own ~/cisc181/lab04 directory.
Note: Previous labs cover the commands for creating
a
new ~/cisc181/lab04
subdirectory and
copying
all
the
files from
my lab04
subdirectory
into your
subdirectory,
so refer to those labs if you are not sure what to do at this step. Next lab
I'll just give you the name of the directory where can find the files and
tell
you to copy them into your own directory.
| Files you will copy from my web page: | multTable.cpp |
|---|---|
| Files you will create and submit via WebCT (also print ones in bold) | lab04a.cc, lab04a.txt |
The file multTable.cpp illustrates an example of a program that uses nested for loops to produce a multiplication table similar to the one you might see hanging in a 3rd or 4th grade classroom. I want to suggest that you compile and run this program, and try to understand the source code as a preliminary step to this assignment.
Your task is to write a program called lab04a.cc, which will produce a table of wind chill factors. The formula that you should use is given on the web site: http://www.nws.noaa.gov/om/windchill/index.shtml (see the formula at the bottom of the table.) You'll need the "pow" function to compute the exponents; you can read about it in your Deitel/Deitel.
You are only going to produce a subset of that table; here is an example of what your output should look like:
> CC lab04a.cc -o windChill
> ./windChill
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 |
>
|
Your program should include a function as follows:
double windChill(int windMph, int tempF)
{
return ... // you fill in the rest
}
You will call that function inside two nested for loops that print the table; one iterates over windMph, and the other over tempF. For example, for temperature, you might use a for loop such as: for (w = 10; w<=50; w+=10)
Now make a lab04a.txt script in which you list out, compile, and execute your
lab04a.cc program (in the usual fashion. See lab02 from this semester for guidance
if you are not sure about all the steps required).
| Files you will copy from my web page: | none (you might find A.dat and A.gnuplot files from lab03 helpful as models, though) |
|---|---|
| Files you will create and submit via WebCT (also print ones in bold) | lab04b.cc, lab04c.cc, lab04b.txt |
| Files you will create and make available on the web | ~/public_html/cisc181/lab04/pentagon.png, ~/public_html/cisc181/lab04/star.png |
In a previous lab, you have seen how to use gnuplot to plot points that you entered by hand using emacs or vi to create a data file (e.g. initials.dat).
Now, you will write two programs, lab04b.cc and lab04c.cc. lab04b.cc will draw a pentagon, while lab04c.cc will draw a five pointed star.
Both the pentagon and the star will be contained within a circle centered at the origin, with a radius of r units. (Prompt the user to enter a value for r at the beginning of the main function). You won't be drawing that circle of radius r, but you will be using that circle as a "reference" for plotting the points.
In both cases, you need to locate five points on the x,y plane. The first point will be at (0,r). On the unit circle (remember trigonmetry?) this point is at 90 degrees (or
/2 radians). The other four points will be at angles of 360/5 degrees around the circle from this point (or 2
/5).. You can use trigonmetry to determine the x,y coordinates of these points (sin(), cos(), etc.... .remember SOHCAHTOA?). Consult your Deitel/Deitel text on details of using #include <cmath> to access the sin() and cos() functions (remembering to use radians rather than degrees).
In both lab04b.cc and lab04c.cc, you'll open an external file inside the C++ program (with ofstream). For lab04b.cc, call this file "pentagon.dat". Write out the five points to this file, in order, and then write the first point at the end again (to close up the pentagon). Then, close the pentagon.dat file, and open another external file (you'll need a second ofstream object) called "pentagon.gnuplot". Use the A.gnuplot and CIS.gnuplot from lab03 as a model. Write out the contents of pentagon.gnuplot using C++ stream insertion operators. Set the xrange and yrange appropriately so that the entire pentagon is visible in the output file.
Then, create a shell script file penatgon.sh, similar to the following:
#!/bin/sh #pentagon.sh (your name here, date here, section number here) TARGET=$HOME/public_html/cisc181/lab04 gnuplot pentagon.gnuplot mkdir -m 755 -p $TARGET cp pentagon.png $TARGET chmod -R 755 $TARGET |
After you create this script (with a text editor), use this command to make it executable:
chmod u+x pentagon.sh
For lab04c.cc, you'll be writing out the points in a way that draws a star. Call your output file "star.dat". Define two functions called:
double xStarPoint(int pointNum, double radius);
double yStarPoint(int pointNum, double radius);
The function xStarPoint() will take a point number on the star (where point 0 is the first point, point 1, is the second point, etc. through point 4) and the radius of a circle, and then calculate a point on that circle (using trig functions). The function yStarPoint() does the same thing for the y coordinate.
(Note that when we write "xStarPoint()" with the empty set of parentheses as part of standard english text, this is a convention understood among C, C++ and Java programmers as a way of indicating in text that "xStarPoint" is the name of a function. It doesn't mean that xStarPoint() it is a function that takes no parameters. If we wrote "xStarPoint();" with an empty set of parentheses and a semicolon after it in a program, however, as a function call, that would mean that xStarPoint() takes no parameters.)
For the star, you need to connect point 0 to point 2, point 1 to point 3, point 2 to point 4, etc., all the way around the circle. You may find that the % operator is useful for doing this in a loop (mod, or "remainder after division").
You should write the program in such a way that the number of points in the star can be changed by only changing a #define, or a constant.
When you have finished writing the points, close the output file. Then, n generate the gnuplot script (star.gnuplot) by also opening as an output file, and writing it out with stream insertion operations. Set the xrange and yrange appropriately, e.g. if the radius r is 10, use [-10,+10] or [-11,+11] so that the entire star appears in the output.
What to turn in:
General Grading Rubric (your TA may provide a more detailed rubric)
| lab04a.cc | correctness of code | 10 |
|---|---|---|
| style | 10 | |
| lab04a.txt | following instructions | 10 |
| lab04b.cc | correctness of code | 10 |
| style | 10 | |
| lab04c.cc | correctness of code | 10 |
| style | 10 | |
| lab04b.txt | following instructions | 10 |
| pentagon.png | correctness | 10 |
| star.png | correctness | 10 |
| TOTAL | 100 | |