CISC181, Lab05, Fall 2004

Part 1: lab05a.cc, A modification to the wind chill program

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 ~/cisc181/lab04/lab04a.cc file to ~/cisc181/lab05/lab05a.cc.
Then modify the lab05a file in the following manner:

  1. Rename your function windChill to be called windChillNew
  2. Add a function windChillOld that follows the old formula (see the USA Today web site.) Note that the USA Today web site's algebra notation may not be so clear. You'll have to interpret it as best you can by comparing their version of the new formula to that on the National Weather Service web site (linked to in lab04). With that help, you should be able to figure out what their notation means.
  3. Add an enumerated type as follows:
    enum WindChillOption  {WCO_NEW = 0,
                            WCO_OLD = 1,
                            WCO_DIFF = 2};
    
    This enumeration will be use to control what type of table is printed.
  4. Add a function as follows:
    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.
  5. Write a function as follows:
    void printTable(WindChillOption opt, int windMin, int windMax, int windStep)
    { ... }

    This function essentially does everything that you used to do in your main program to print out the table. The new main program will look like the following:
    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.
  6. One more function that will help you structure your program well: write a function as follows:

    double tableEntry(int windMph, int tempF, WindChillOption opt) { switch(opt) { case WCO_NEW: return windChillNew(windMph, tempF); break; ...

    This is actually only the start of this function; you should be able to finish it off, following the pattern from the earlier similar function. The idea is that you'll replace the call to your windChill() function—the call that was nested inside the for loops to produce the table—replace that with a call to this function called tableEntry().

    The table entry function basically is like a chameleon that can change its color to be whatever the user passes in for the opt parameter: you want the old formula, you get the old formula. You want the new formula, you get the new formula. You want the difference... well, you get the idea. Good software is built up by composing building blocks such as these that can be combined in many different ways.
  7. Once you have completed the program, you should be able to get output such as the following. Script your program in the usual way in a file called lab05a.txt. (By now, you should be comfortable with the usual expectations for a script file, but if you are not sure, ask your TA during lab.)

    > CC lab05a.cpp -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 | 
    > 
    

Part 2: lab05b.cpp: Generating a five pointed star.

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 a program, lab05b.cpp, to produce a data file containing a five pointed star. Eventually, we want the program to be able to also produce a 6 pointed star, or 7 pointed star, etc.

The star will be contained within a circle centered at the origin, and with a radius of 10 units.

The "top" point of the start will be at the point (0,10), ten units above the origin, right on the y axis. The other four points will be at angles of 360/5 degrees around the circle. You can use trig 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 these functions (remembering to use radians rather than degrees).

You'll open an external file inside the C++ program (with ofstream). Call it "star.dat". Then write out the points to this file. Two nested "for loops" should do the trick. The pseudocode is as follows:

 

for each of five points of the circle (call this point p)
   {
   for each of the two points other than p and the two points adjacent to p 
     ( call each of these points q)
       {
         write out a line connecting p to q
         write a blank line in the gnuplot file
           (so gnuplot lifts the pen)
       }
   }

When you have finished writing the points, close the output file. You can generate the gnuplot script (star.gnuplot) either by hand editing it in vi or emacs. Even better would be to open it also as an output file, and write it out. Set the xrange and yrange appropriately, e.g. [-10,+10] or [-11,+11] so that the entire star appears in the output. Use star.png as the output file. (Writing it from the program allows you to make the number "10" a constant or a variable that can be modified later to make different size stars. The number of points, 5, is also a number that should be a constant or a #define so that it can be modified later.

What to turn in:

Finishing up and Submitting

  1. Upload lab05a.txt, lab05a.cc, lab05b.txt and a lab05b.cpp file to WebCT. You do not need to upload star.dat, star.gnuplot, or star.png.
  2. Submit your uploaded files to WebCT.
  3. Make sure your star.png file is readable on your web site under the URL http://udel.edu/~userid/cisc181/lab05/star.png.
  4. Print the two script files and give to your TA.

Grading

lab05a.cc:

lab05b.cpp:

Total: 100 points.