CISC105 Project 3, Fall 2005
(sections 022-025, Instructor: P. Conrad)

Introduction

Objectives

The main theme of this project is manipulating arrays of structs in C.

This project builds on lab02, project 1 and project 2

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.

This project proceeds in stages

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.

Project Stages

Stage 0: Create a Comma-Separate Values (CSV) file for your data
(Filename: proj3data.txt, 20% of total, 20% complete if submitted )

Preliminaries

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

Creating your data file

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:

two columns of hiking data in CSV format
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:

hiking data in CSV format
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.

Stage 1: Write a struct that represents your data

(Filename: proj3s1.c and xxxxx.h , 10% of total, 30% complete when done (10% for overall correctness) )

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

Stage 2: A menu driven program that reads one line of your file ('h','p','q','r' options)
(Filename: proj3s2.c, 10% of total, 40% complete if submitted )

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.

Finishing and Submitting

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..

Stage 3: A program that reads the entire data file into an array of structs
(Filename: proj3s3.c, 10% of total, 50% complete when done)

This stage involves the following changes to your proj3s3.c file:

  1. Changing the variables single struct variable that you used in proj3s3.c to represent one line of your file into an array of structs that can store all of the data in the file. (Read about structs and arrays of structs in chapter 11—especially section 11.5—of your Hanly and Koffman textbook.)

    You will also need a constant 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.
  2. Changing the function 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.
  3. Changing the function 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:

sample run of proj3stage3.c
> ./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).

sample run of proj3stage3.c on tooMuchHikingData.txt

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.

Step-by-step instructions

  1. To get started with this stage, copy your proj3s2.c file to a file called proj3s3.c.

  2. Look at the file proj3stage3.c located in the proj3 directory of the website, or in

    ~pconrad/public_html/cisc105/05F/work/proj/proj3

    Compare this file with proj3start.c, and notice the differences. Some of these are highlighed with comments that contain the symbol $$$ so that you can search for this and pay attention to those lines.

    (Note: I used the dollar sign not because of its association with money, but because other than the symbol @, it is the only one on the keyboard that doesn't have some meaning in the C language!)

  3. Make the necessary changes in your proj3s3.c program.

    Among other things, be sure that your program can read a file with too much data in it. Make a file, similar to tooMuchHikingData.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.

Finishing and Submitting

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..

Stage 4: Add a simple calculation ('c' option)
(Filename:
proj3s4.c, 10% of total, 60% complete when done)

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!

Step-by-step instructions:

  1. For this stage, start by copying your proj3s3.c file to proj3s4.c.

  2. Make the necessary changes in your proj3s4.c program to add a "c" option.

Finishing and Submitting

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..

Stage 5: Add a simple comparison (modification to 'p' option)
(Filename: proj3s5.c: 10% of total, 70% complete when done)

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!

To complete this stage

Copy from proj3s4.c to proj3s5.c, and add C code to implement your decision. Add it into the printValues() function and test it.

Finishing and Submitting

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..

Stage 6: Create an output file in HTML format ('w' option)
(Filename: proj3s6.c: 10% of total, 80% complete when done)


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.

Finishing and Submitting

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.

Stage 7: Sort the array in ascending order by the numeric field ('s' option)
(Filename: proj3s7.c: 10% of total, 90% complete when done)


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.

Stage 8: Add values to the list ('s' option)
(Filename: proj3s8.c: 10% of total, 100% complete when done)

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.

Final Submission

To submit project 3, you need to do each of the following:

  1. Make sure that your proj3data.txt file and proj3data.html pages are readable on the web at the following URL. Note that you'll need to substitute your actual strauss username in place of "yourusername".

    http://copland.udel.edu/~yourusername/cisc105/proj3/proj3data.txt
    http://copland.udel.edu/~yourusername/cisc105/proj3/proj3data.html


  2. For whatever stage you stopped at, upload the appropriate .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.)

    Examples:
  3. Create a script for that stage, and call it 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.)

    You are responsible for determining what constitutes a script that completely and fully tests all of the options of your program. If your program is capable of producing error messages, you should do additional runs that demonstrate that these error messages work! For example, if there is a default case that checks for input that is not one of the options in your menu, you should run that case.

  4. Upload your proj3.txt file to WebCT and submit.
  5. Print your proj3.txt using one of the following and give to your TA at the earliest possible date:
  6.  

Grading

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

 


Copyright 2005, Phillip T. Conrad. Permission to copy all or any part of this assignment for non-commercial, non-profit, educational use granted to anyone, provided appropriate credit is given, and this copyright notice is maintained. All other rights reserved.