The main theme of this project is manipulating parallel arrays in C.
To be able to complete this project, you need to be able to do all of the following things as a C programmer (this list is a repeat from project 1)
In addition, you need the following skills which are new to this project:
In lecture, we took one day where each student chose a topic for project 1. You should use the same topic for this project. Your topic will guide you through this project, and ensure that your project is your own creation, individual and unique, a reflection of your own interests.
In lab02, you wrote up a proj1data.txt file, describing several "fields" of data that might be in a "table of values" for your topic. You also produced a short table of example values, and wrote a short C program that read in two of those fields, and echoed them back to the screen.
In project01, you elaborated on that, writing code to read in values representing one line in your table.
In this project, you are taking that a step further by working with a data file of values representing data from your topic, and reading it into an array.
As with project1, the project proceeds in stages. There are 7 stages; for each stage that you complete, you work towards additional "partial credit". After completing stage 6, you can stop, and "settle for a B", or complete one additional stage to go for an "A" grade.
Remember that each project is worth 10% of your final grade in the class, while each lab is worth 2% or less of your final course grade. So it is important to do well on the projects.
With that said, let's get started.
It should go without saying, but to get started with the project, you should create a directory called ~/cisc105/proj2. Most of your work for the project should go in that directory:
mkdir -p ~/cisc105/proj2
You should also create a directory under your web page for project 2:
mkdir -p ~/public_html/cisc105/proj2
Remember to cd into your proj2 directory when writing code (e.g. stages 2 and 3).
cd ~/cisc105/proj2
Only cd into your web directory when you are working on stuff that gets published to the web (as we will be in stage 1)
cd ~/public_html/cisc105/proj2
In this stage, you will write a datafile that contains data values separated by commas. This format of datafile is called "Comma Separated Values (CSV)", and can be used to import data into many commercial software packages, including Microsoft Excel.
As an example, here is a Comma Separated Value file for hiking data:
South Kaibab,Grand Canyon NP,6.3,AZ,1 Hidden Pond,Brandywine Creek SP,2,DE,4 Bright Angel,Grand Canyon NP,9.3,AZ,1 Kelly's Run,Holtwood Recreation Area,4,PA,1 |
The fields are name, location, mileage, state, numTimesHiked. There is one record per line, and the fields are separated by commas.
For project 3, we may use a file similar to the one above that has several fields per line of data. However, for project 2, we are going to use a simpler format for our file. Each line in the file will have only two fields; one should be character, and the other should be numeric (either int or float; your choice). For example, if we choose the fields name and mileage, our file will look like this:
South Kaibab,6.3 Hidden Pond,2, Bright Angel,9.3 Kelly's Run,4 |
Your job in this stage is to create a file with two columns from your "topic" (the same one you chose in project 1.)
You should post your data file on the web in the file:
http://copland.udel.edu/~userid/cisc105/proj2/proj2data.txt
You probably should know this by now, but I'll remind you one last time: that means it needs to be stored in the file:
~/public_html/cisc105/proj2/proj2data.txt
To understand this relationship, look at the URL below and the corresponding filename; notice that the parts in green are different, but the parts in yellow are the same. This shows the relationship between filenames and web addresses on strauss/copland.
URL: |
|
Filename: |
|
This stage of the project involves writing a menu driven program that gives you several options for working with your data. This first stage will only include some very basic commands. At each stage, you'll add additional commands to your program.
To start out, copy the program proj2start.c from the directory ~pconrad/public_html/cisc105/05F/work/proj/proj2 into your directory, and rename it to proj2s2.c. This program contains a basic menu driven program with several very simple options, using the hiking data example above.
Here is a sample run of this program, illustrating the various options:
> ./proj2start
Enter option (type h for help): r
Please enter a filename: hikingData.txt
Data read successfully from file
Enter option (type h for help): p
hiking trail name: South Kaibab
length in miles: 6.3 miles
Enter option (type h for help): h
h: help
p: print values
q: quit
r: read values
Enter option (type h for help): q
Thanks for using this program.
Bye now.
> |
Your job is to write a similar program to read in the data in your own proj2data.txt file. You'll need to change the variables in the program so that instead of having variables like hikingTrailName and hikingTrailLengthInMiles, you have variables that are suitable for your topic.
You'll need to copy your proj2data.txt file from ~/public_html/cisc105/proj2/proj2data.txt into your ~/cisc105/proj2 directory (you'll have two copies) to test the program.
If you only get as far as finishing stage 2, you'll submit proj2s2.c along with a script that shows that it works. Otherwise, you do not need to script this stage; continue on to stage 3.
This stage involves the following changes to your proj2s2.c file:
ARRAY_SIZE that stores the size of the arrays (the maximum number of lines you could read from your file into the arrays), and a variable count or numTrails or something similar that indicates how many elements in the array are actually used. readOneLineFromFile() to a function
called readFileIntoParallelArrays() that will read all of the lines of the file into parallel arrays. This function will return an int value which is the number of elements read into the arrays. If the file cannot be read, or the file is read, but is empty, the value 0 will be returned. printValues() so that you pass in the two arrays, and print them out in their entirety. You'll also have to pass in a count (or variable such as numTrails) of the number elements to be printed into that function. Look at the file proj2stage3.c located in the proj2 directory of the website, or in
~pconrad/public_html/cisc105/05F/work/proj/proj2
A sample run of this program appears here:
>./proj2stage3
Enter option (type h for help): h
h: help
p: print values
q: quit
r: read values
Enter option (type h for help): r
Please enter a filename: hikingData.txt
Data read successfully from file
Enter option (type h for help): p
hiking trail name: South Kaibab
length in miles: 6.3 miles
hiking trail name: Hidden Pond
length in miles: 2.0 miles
hiking trail name: Bright Angel
length in miles: 9.3 miles
hiking trail name: Kelly's Run
length in miles: 4.0 miles
Enter option (type h for help): q
Thanks for using this program.
Bye now.
>exit
|
Your job is to copy your program proj2s2.c to a program called proj2s3.c and make it behave in a similar fashion so that it can read and print out the entire file from two separate arrays.
To get started with this stage, copy your proj2s2.c file to a file called proj2s3.c.
~pconrad/public_html/cisc105/05F/work/proj/proj2If you only get as far as finishing stage 3, you'll submit proj2s3.c along with a script that shows that it works. Otherwise, you do not need to script this stage; continue on to stage 4.
The main work of this stage is to add something to your output that involves a simple calculation across all the numeric fields in your data. This could be an option to calculate the maximum, average or sum. Which one you choose will depend on what makes sense for your particular data. For example, it doesn't make sense to calculate the sum of a series of years, but it might make sense to calculate the maximum (which in that case, would be labelled as the "most recent year").
However, one restriction: you may NOT choose the minimum, since I've providing you with sample code to calculate the minimum.
You'll add a new menu option "c" for calculate. This option will print the maximum, or average, or sum, or whatever makes sense for your data.
For this stage, start by copying your proj2s3.c file to proj2s4.c.
proj2s4.c program.If you only get as far as finishing stage 4, you'll submit proj2s4.c along with a script that shows that it works. Otherwise, you do not need to script this stage; continue on to stage 5.
In project 1, stage 5, you added a simple comparison (i.e. an if/else) to one of your numeric fields. In this project, you will do the same, but you will add this as part of your printValues() function.
As an example, inside the printValues() function, you might ask the user to enter, as a separate variable, the year the user was born. You can then print output such as:
Year this album was released : 1972
Did this album come out before you were born: Yes
Year this album was released : 1990
Did this album come out before you were born: No
In this case, the code would take the following form:
printf("Did this album come out before your were born: ");
if (yearAlbumReleased[i] < yearUserWasBorn)
{
printf("yes\n");
}
else
{
printf("no\n");
}
Note that this code looks just like the code from the example given in project 1, except that now, the variable yearAlbumReleased is an array, so it has a subscript [i], and the code given here would appear inside a for loop that steps through all the elements in the array.
You can also define a constant and compare against that. For example, if your topic is hiking trails, you might decide that a hiking trail of 5 miles or less is a short trail, and a trail of more than 5 miles is a long trail. You could then use a #define to define a constant:
#define MAX_LENGTH_OF_SHORT_TRAIL 5
and use an if/else such as:
printf("Type of trail : ");
if (trailLength[i] <= MAX_LENGTH_OF_SHORT_TRAIL)
{
printf("short\n");
}
else
{
printf("long\n");
}
Copy from proj2s4.c to proj2s5.c, and add C code to implement your decision. Add it into the printValues() function and test it. As an extra challenge, I'm not giving you any sample code for this stage. By now, you should be catching on as to how this works.
If you only get as far as finishing stage 4, you'll submit proj2s4.c along with a script that shows that it works. Otherwise, you do not need to script this stage; continue on to stage 5.
In this step, you will add an additional option to your menu called "w" for "write web page". This option is similar to the code that you added to the end of the program in stage 6 of project 1, however, in this case, you'll be adding multiple <tr> elements in your table, one for each element in the array.
For example, the table element in the output HTML file for the hikingData.txt file, might look like this when it appears on the web page:
| nameOfTrail | length |
|---|---|
| South Kaibab | 6.3 |
| Hidden Pond | 2 |
| Bright Angel | 9.3 |
| Kelly's Run | 4 |
The HTML for this table would look like this:
<table border="2" cellpadding="3" cellspacing="4"> |
Inside the new function you write that implements the w option, ask the user for the name of the file to write the web page to. Then, open that file, and write the HTML code out to that file. Close the file using fclose() before the function returns to the main program.
w option. Model it after the printValues() function, since it is similar, except that instead of printing to the screen, it is printing values to a web page.readValuesFromFile() as a model to the extent that it asks the user for a filename and then opens a file (though in this case, the file is an output file, not an input file, so you need to use "w", not "r" on the fopen() call.). The htmlBuilder program from lab03 may also be useful for model code. table element and the row with the headers once. Then use a for loop to output one tr element for each element of the array (with a td element for the character field, and another one for the numeric field). After that for loop, output the closing tag for the table element. html, head, title and body elements; you can figure out where they go yourself. If you leave these out, your web page won't work!chmod on that file (chmod 755 filename.html) to make it readable. If you only get as far as finishing stage 6, you'll submit proj2s6.c along with a script that shows that it works.
However, regardless of whether you stop here, or go on, there is one more thing you must do for this stage!
Run your program, select the r option to read in your proj2data.txt file, then select the w optoin, and give it the name proj2data.html as the output file. When you are finished, copy that proj2data.html file to your web page http://copland.udel.edu/~yourusername/cisc105/proj2 and make it readable (with a chmod command.)
If you are continuing on to stage 7, you do not need to script this stage separately.
Typically, the last stage of each project involves some step that is a bit more challenging, even though it is only worth 10% of the grade. The purpose of this stage is to provide some extra challenge to the students that will not be satisfied with anything less than an "A" on the project.
Should you skip this stage? It is a perfectly reasonable choice for many people. While I expect all students to try their best to complete up through stage 6, and will be a bit disappointed if you don't make it that far, I expect that many of you might choose to skip stage 7, and its ok if you do.
What are the consequences of skipping stage 7? If you skip this step, the maximum grade you can earn is a 90%, which an A-, but to earn that grade everything else must be perfect—so, more realistically, you are looking at earning a B. Note that a "B" is a perfectly good grade, and there is no dishonor in being satisfied with a B, and skipping this stage altogether. It is often a more challenging and time consuming stage, and you may need to devote that extra time to some other course.
What does this step involve?
In this step, you will add an additional menu option s that sorts the two parallel arrays in ascending order according to the numeric field. We'll talk about sorting in class on Friday 11/04/05 (don't let me forget!).
Note that this means that you will compare array elements based on the numeric field, but that each time you swap two elements in the numeric array, you must ALSO swap the two elements in the character array. You'll need to use strncpy to swap character elements; you can't just use assignment (ask about this in lecture!)
To demonstrate that this option works in your script, you will need to read in a file that is NOT sorted in ascending order numerically, use the p option to print out the values, then use the s option to sort the values, then the p option again to show that they are now sorted. You'll need to write a sortArrays() function, where you pass in the two arrays.
To submit project 2, you need to do each of the following:
.c file (and for now, that file only) to WebCT for submission, but don't submit yet (wait until you have the script file as well.) proj2s2.c. proj2s5.c. proj2s7.c.proj2.txt. In the script, cat your .c file, and then do whatever steps are necessary to show that your .c file compiles cleanly, and that all of your menu options work (up through the stage you completed.)proj2.txt file to WebCT and submit.proj2.txt using one of the following and give to your TA at the earliest possible date:qpr -q whlps proj2.txt (to print in Willard 009B)qpr -q smips proj2.txt (to print in the basement of Smith Hall)kpr proj2.txt (to print to your local printer if you are on a Windows machine, and using the ssh client downloaded from http://udeploy.udel.edu)
| Stage | Description | Percent | Cumulative Total |
|---|---|---|---|
| 1 | Write a datafile and post it on the web | 10 | 10 |
| All | Correct Submission and Following Directions: Stapling printed pages together, naming files correctly, including both .c and .txt files when uploading to WebCT, putting name on paper, etc. |
10 | 20 |
| All | Style: comments, variable names, indentation, etc. | 10 | 30 |
| 2 | A menu driven program that reads one line of your file ('h','p','q','r' options) | 10 | 40 |
| 3 | A program that reads the entire data file into an array (same options) | 10 | 50 |
| 4 | Add a simple calculation (e.g. max, average, sum) ('c' option) | 10 | 60 |
| 5 | Add a simple comparison (modification to 'p' option) | 10 | 70 |
| 6 | Create an output file in HTML format ('w' option) | 20 | 90 |
| 7 | Sort the array in ascending order by the numeric field ('s' option) |
10 | 100 |