The main theme of this project is manipulating arrays of structs in C.
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 project02, you worked with just two columns from your table, but worked with all the lines of data, using two paralel arrays to represent those two columns.
In this final project, you will work with with all the columns, and all the rows of your table of data.
Rather than using paralel arrays, you will use a single array, but that array will be an array of structs.
As with projects 1 and 2, the project proceeds in stages. There are eight stages; for each stage that you complete, you work towards additional "partial credit". After completing stage seven, 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.
Make yourself the following two directories
~/cisc105/proj3
~/public_html/cisc105/proj3
Remember to cd into your proj3 directory when writing code (e.g. stages 2 and 3).
cd ~/cisc105/proj3
Only cd into your web directory when you are working on stuff that gets published to the web.
cd ~/public_html/cisc105/proj3
For project 2, we used a simple format for our data. Each line in the file had only two fields. For example, for the fields name and mileage, our file will looked like this:
South Kaibab,6.3 Hidden Pond,2, Bright Angel,9.3 Kelly's Run,4 |
However, for this project, we will use a more complete data file with all the fields from your original project 1, in Comma Separated Values (CSV) format. 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 |
Each line in the data file will contain all the fields values separated by commas. CSV files can be used to import data into many commercial software packages, including Microsoft Excel.
Create a file for your topic similar to the one above, with at least two character fields and at least two numeric fields. Store your data file at:
http://copland.udel.edu/~userid/cisc105/proj3/proj3data.txt
By now, you should know how to get the file there and make it readable on the web. If you need a reminder, consult the description for your previous labs and projects.
In this stage, you will write a struct that represents your data. This struct definition will go in a file called xxxxx.h. Actually, it won't really be called xxxxx.h literally; rather, the name will come from the theme of your project.
For example if your theme is hunting, your file will be called hunting.h. If your theme is ski resorts, it will be called skiResorts.h, and so forth.
As an example of how to proceed, look at the following pairs of files in the proj3 directory. Your proj3s1.c file should look something like either hiking_s1.c or pizza_s1.c, and your xxxxx.h file should look something like hiking.h or pizza.h.
| Theme | .c file | .h file |
|---|---|---|
| Hiking | hiking_s1.c | hiking.h |
| Pizzas | pizza_s1.c | pizza.h |
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 proj3start.c from the directory ~pconrad/public_html/cisc105/05F/work/proj/proj3 into your directory, and rename it to proj3s3.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:
> ./proj3start
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
location: Grand Canyon NP
length in miles: 6.3 miles
state: AZ miles
hiked: 1 time
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 proj3data.txt file. Instead of doing a #include on "hiking.h", you will do a #include on "xxxxx.h", where xxxxx.h is the struct definition file that you created in step 1.
You'll need to copy your proj3data.txt file from your web directory ~/cisc105/proj3 directory (you'll have two copies) to test the program.
If you only get as far as finishing stage 3, you'll submit proj3s3.c along with a script that shows that it works. Otherwise, you do not need to script this stage; continue on to the next stage..
This stage involves the following changes to your proj3s3.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 readFileIntoArrayOfStructs() that will read all of the lines of the file into an array of structs. 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 array of structs, 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 proj3stage3.c located in the proj3 directory of the website, or in
~pconrad/public_html/cisc105/05F/work/proj/proj3
A sample run of this program appears here:
> ./proj3stage3
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
location: Grand Canyon NP
length in miles: 6.3 miles
state: AZ miles
hiked: 1 time
hiking trail name: Hidden Pond
location: Brandywine Creek SP
length in miles: 2.0 miles
state: DE miles
hiked: 4 times
hiking trail name: Bright Angel
location: Grand Canyon NP
length in miles: 9.3 miles
state: AZ miles
hiked: 1 time
hiking trail name: Kelly's Run
location: Holtwood Recreation Area
length in miles: 4.0 miles
state: PA miles
hiked: 1 time
Enter option (type h for help): q
Thanks for using this program.
Bye now.
> ./proj3stage3
|
Here is another sample run, showing what happens when you
use a file that is too big for the array. In this case, the file
tooMuchHikingData.txt has 12 lines in it, but the
array is set to have only 10 elements.
Note that the programmer has to write code to check for this in C (also in C++). In Java, the language checks for that automatically (this is one of the reasons that C and C++ run faster than Java).
Enter option (type h for help): r
Please enter a filename: tooMuchHikingData.txt
You have more than 10 lines in your file
This program can only handle up to 10 lines
Ask a programmer to change MAX_HIKING_TRAILS and recompile
Enter option (type h for help): p
hiking trail name: South Kaibab
location: Grand Canyon NP
length in miles: 6.3 miles
state: AZ miles
hiked: 1 time
hiking trail name: Hidden Pond
location: Brandywine Creek SP
length in miles: 2.0 miles
state: DE miles
hiked: 4 times
hiking trail name: Bright Angel
location: Grand Canyon NP
length in miles: 9.3 miles
state: AZ miles
hiked: 1 time
hiking trail name: Kelly's Run
location: Holtwood Recreation Area
length in miles: 4.0 miles
state: PA miles
hiked: 1 time
hiking trail name: Fake Trail 5
location: Nowhere
length in miles: 5.0 miles
state: DE miles
hiked: 0 times
hiking trail name: Fake Trail 6
location: Nowhere
length in miles: 5.0 miles
state: DE miles
hiked: 0 times
hiking trail name: Fake Trail 7
location: Nowhere
length in miles: 5.0 miles
state: DE miles
hiked: 0 times
hiking trail name: Fake Trail 8
location: Nowhere
length in miles: 5.0 miles
state: DE miles
hiked: 0 times
hiking trail name: Fake Trail 9
location: Nowhere
length in miles: 5.0 miles
state: DE miles
hiked: 0 times
hiking trail name: Fake Trail 10
location: Nowhere
length in miles: 5.0 miles
state: DE miles
hiked: 0 times
Enter option (type h for help): q
Thanks for using this program.
Bye now.
>
|
Your job is to copy your program proj3s2.c to a program called proj3s3.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 proj3s2.c file to a file called proj3s3.c.
~pconrad/public_html/cisc105/05F/work/proj/proj3tooMuchHikingData.txt that has at least one more line in it than the size of your
array (e.g.MAX_HIKING_TRAILS). In your script, test for this as well.If you only get as far as finishing stage 3, you'll submit proj3s3.c along with a script that shows that it works. Otherwise, you do not need to script this stage; continue on to the next stage..
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 can be the same calculation you used in project 2, or a different calculation if you are getting bored with that one—it is your choice. As in project 2, this can be an option to calculate the maximum, average or sum. Which one you choose will depend on what makes sense for your particular data.
I'll even let you choose the minimum this time, if that's what you really want to do.
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.
One additional challenge though; I'm not providing you with any sample code this time. At this point, you've already done this step in project 2 for parallel arrays, and you've already seen how we do things with structs. Hopefully by now, if you've compared the stages of project 2 and the corresponding stages of project 3, you can see that working with an array of structs vs. working with parallel arrays is not that different. You should be able to figure out what to do from the code you already have, the examples shown in lecture, your excellent textbook, and your thinking and reasoning skills!
For this stage, start by copying your proj3s3.c file to proj3s4.c.
proj3s4.c program to add a "c" option. If you only get as far as finishing stage 4, you'll submit proj3s4.c along with a script that shows that it works. Otherwise, you do not need to script this stage; continue on to the next stage..
In stage 5 of projects 1 and 2, 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 this time working with your array of structs.
I don't think any further explanation should be needed; you know what to do!
Copy from proj3s4.c to proj3s5.c, and add C code to implement your decision. Add it into the printValues() function and test it.
If you only get as far as finishing stage 6, you'll submit proj3s5.c along with a script that shows that it works. Otherwise, you do not need to script this stage; continue on to the next stage..
In this step, you will add an additional option to your menu called "w" for "write web page", just like in project 2, but this time you'll do it for ALL the fields in your struct. Follow the instructions from project 2 stage 6.
If you only get as far as finishing stage 6, you'll submit proj3s6.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 proj3data.txt file, then select the w optoin, and give it the name proj3data.html as the output file. When you are finished, copy that proj3data.html file to your web page http://copland.udel.edu/~yourusername/cisc105/proj3 and make it readable (with a chmod command.)
If you are continuing on to the next stage, you do not need to script this stage separately.
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. Note that for project 3, this is not the "last optional step for an A", but is rather a required step for everyone.
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 swap the ENTIRE STRUCT. This means you'll need to write a swap function that is capable of doing that. We'll discuss how to do that 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 array of structs and the size of the array.
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 7, 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 8, and its ok if you do.
What are the consequences of skipping stage 8? 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 stage involve?
In this stage, you will add an additional menu option "a" that allows the user to add a new item to the list. This option will prompt the user for each field in the table (e.g. for the hiking data, this is the trail name, the trail location, the length in miles, and the number of times hiked). It will then increase the count variable and place the new item into the array.
Note that your program should keep track of whether the array is sorted or not! You should have a boolean flag (a true/false value) which is false if the data has not been sorted, and true if the data has been sorted.
If the data has not been sorted, you should simply add the new item at the end of the array, i.e.:
array[count].someField = newValue;
array[count].anotherField = anotherNewValue;
...
count++;
However, if the array is already sorted, you should search through the array to find the place where this item should fit, and then "scoot all the other items in the array down by one". For example, if the array contains data in positions [0],[1],[2],[3],and [4] (thus count is 5), and you discover that when the data is sorted, the new element belongs between items [1] and [2], then you need to move all the items from [2] through [4] into positions [3] through [5], and then store the new item in the space left in position [2]. (Of course, you still increase count).
Be sure to demonstrate that this works properly in your script.
Note that while it will "appear" to work the same way to an external observer if you simply stick the new element at the end and call sort, this is not as efficient, since you will end up doing extra work to sort the array, versus just scooting every element down by one position. That technique is ok for partial credit, but will not receive full marks.
To submit project 3, 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.) proj3s2.c. proj3s5.c. proj3s8.c.proj3.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.)proj3.txt file to WebCT and submit.proj3.txt using one of the following and give to your TA at the earliest possible date:qpr -q whlps proj3.txt (to print in Willard 009B)qpr -q smips proj3.txt (to print in the basement of Smith Hall)kpr proj3.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 |
|---|---|---|---|
| 0 | 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 |
| 1 | A struct definition for your project | 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). At this stage be sure you test with both a file that fits entirely into the array, as well as one that does not fit (e.g. tooMuchHikingData). See the description of stage 3 for more details. |
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) | 10 | 80 |
| 7 | Sort the array in ascending order by the numeric field ('s' option) | 10 | 90 |
| 8 | An option to add values to the list ('a' option) | 10 | 100 |