School of Engineering and Applied Science
Department of Computer Science
CSci 49 -- Introduction to C Computing
http://www.seas.gwu.edu/~csci49/spring06
Prof. Michael B. Feldman
mfeldman@gwu.edu

Project 6
Due Date: beginning of lecture, Thursday, March 30, 2006

The goal of this project is to give you more practice with nested loops and other control structures, and to introduce external text files. Your compilation errors will probably be easy to fix, but your execution results may well be incorrect. If you don't start on it right away, you won't have time to debug the execution errors. Think carefully about your algorithm!

Project:

Assume the definition of a calendar date we used in Project 4.

Develop a program that will ask the user for an arbitrary starting date and an arbitrary ending date, then output, to a file named "test.dat" a line for each day from the starting date to the ending date, inclusive. The starting and ending dates do not have to be in the same year.

For example, if the user enters a starting date of May 10, 1999 and an ending date of September 15, 2002, the file will contain a line for each day from May 10 through December of 1999, all of 2000, all of 2001, and all dates in 2002 through September 15. Remember, when turning in your results,be sure to include these output files in their complete and unedited form. This may mean that there will be several pages to turn in.

Your dates should be output in the following format: May 3, 2002When your program completes its run, test.datwill be an ordinary text file in your directory, which you can read with vi, type out (cat) to the screen, print, download, or whatever.

In this project you are NOT required to validate the dates or to test with invalid ones; you can assume the user will always enter valid data. However, your test plan must include several date ranges that will test whether your program "rolls over" properly from month to month and year to year. In the next project we'll combine this project with the Project 4 validation ideas.

Notes about Files

It is important, even in an introductory course, to learn something about external files, because almost all "real" programs use them. Here's an example, programs49/ShowFile.c.

   1. /*------------------------------------------------------------
   2. | ShowFile.c
   3. | You can use this as a starting point for your projects.
   4. | Demonstrates the use of an output file.
   5. | Writes a 10 x 10 multiplication table to a file.
   6. | Author: Michael Feldman, The George Washington University
   7. | Last Modified: March 21, 2006
   8. ------------------------------------------------------------*/
   9. #include <stdio.h>
  10. int main()
  11. {
  12.   const int MAX = 10;
  13.   int row, column;        /* rows and columns of table */
  14.   FILE *myOutput;
  15.
  16.   /* The next statement associates the program variable myOutput
  17.      with a new output file that will be named multi.txt       */
  18.   myOutput = fopen ("multi.txt", "w");    /*
  19.
  20.   /* now write row x column products into file */
  21.   for (row = 1; row <= MAX; row++)
  22.   {
  23.      for (column = 1; column <= MAX; column++)
  24.      {
  25.         fprintf (myOutput, "%4d", row * column);
  26.      }
  27.      fprintf (myOutput, "\n");
  28.   }
  29.
  30.   /* Be sure to close the file at the end! */
  31.   fclose(myOutput);
  32.   printf ("Output file multi.txt has been created.\n");
  33.    
  34.   return 0;
  35. }

Here are the key things to notice and incorporate in your program:

  1. line 14 declares a program variable that represents the file;
  2. line 18 "opens" the file -- that is, actually creates the file multi.txt in your file system, makes it available for output (hence the "w"), and associates it with the program variable myOutput;
  3. lines 25 and 27 write data into the file using fprintf, which works just like printf but puts the output into the file instead of on the screen;
  4. line 31, which closes the file and is important to do just before your program ends.
NOTE: Every time you run this program, the file is re-created and the old version is erased! So if you want to save several versions of the file, be sure to rename each one (using mv) before you run thje program again!
 
Before writing your own program, make sure you copy, compile, and run this one so you can see how the file is created.

What to submit:

You must follow the project preparation and submission documents on the website.

Your grade will be calculated on a 20-point basis, as follows:

Extra credit:

We'll continue the 2-point bonus for getting an early start. If you e-mail your "framework" listing file to Prof. Feldman, and the time stamp on the e-mail is no later than 5 PM, Monday, March 27, 2006, you will be awarded 2 extra project points. The "framework" must be a listing (.txt) file, with no compilation errors or warnings, that contains the declared variables, and a set of comments inserted for the main algorithm steps.

MBF 3/21/06