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

Introduction

Objectives

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)

  1. Be able to identify the right C-language "type" for storing various kinds of data (e.g. int, float, double, char array).
  2. Choose an appropriate length for a char array variable.
  3. Choose appropriate prompts for interacting with a user that types in data from the keyboard.
  4. Read a value into into a variable of type int, float, or double.
  5. Read some text (possibly with embedded spaces) into a char array.
  6. Printing out variables of type int, float, double, and char array.
  7. Use an if/else to make a decision and print different kinds of output.
  8. Define a function for conversion of units, and call it.
  9. Write simple arithmetic expression and print their values.
  10. (If dollar amounts are involved) Output a float or double as a dollar amount, appropriately formatted.
  11. Write values out to an output file.
  12. Write values out to an output file formatted in HTML.

In addition, you need the following skills which are new to this project:

  1. Wrting a menu driven program.
  2. Reading a line of text and separating that line into separate fields using strtok().
  3. Loading values into a struct.
  4. Declaring an array of structs.
  5. Sorting an array of structs.
  6. Searing an array for a value (linear search).

You need to choose a "topic" before starting 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.

This project builds on lab02 and project01.

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.

This project proceeds in stages

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.

Project Stages

Stage 0: Preliminaries

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

As for the other stages, I'll leave it as an exercise for you to figure out which directory you should be in for each of those stages.

Stage 1: Write a datafile and post it on the web.
(Filename: proj2data.txt, 10% of total, 10% complete when done)

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:

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

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:

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

 http://copland.udel.edu/~userid/cisc105/proj2/proj2data.txt

Filename:

 ~/public_html/cisc105/proj2/proj2data.txt

 

Stage 2: A menu driven program that reads one line of your file ('h','p','q','r' options)
(Filename: proj2s2.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 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.

Finishing and Submitting

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.

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

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

  1. Changing the variables that you used in proj2s2.c to represent one line of your file into parallel arrays that can store all of the data in the file. (Read about parallel arrays in section 8.1 of your Hanly and Koffman textbook, especially pages 370 and 371.) 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 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.
  3. Changing the function 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:

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

Step-by-step instructions

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

  2. Look at the file proj2stage3.c located in the proj2 directory of the website, or in

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

    Compare this file with proj2start.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 proj2s3.c program.

Finishing and Submitting

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

Stage 4: Add a simple calculation ('c' option)
(Filename:
proj2s4.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 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.

Step-by-step instructions:

  1. For this stage, start by copying your proj2s3.c file to proj2s4.c.

  2. Look at proj2stage4.c from the proj2 directory, especially at the lines marked with $$$.
  3. Make the necessary changes in your proj2s4.c program.

Finishing and Submitting

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.

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

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");
  }

To complete this stage

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.

Finishing and Submitting

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.

 

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


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">
<tr>
<th>nameOfTrail</th>
<th>length</th>
</tr>
<tr>
<td>South Kaibab </td>
<td>6.3</td>
</tr>
<tr>
<td>Hidden Pond</td>
<td>2</td>
</tr>
<tr>
<td>Bright Angel </td>
<td>9.3</td>
</tr>
<tr>
<td>Kelly's Run </td>
<td>4</td>
</tr>
</table>

You must ask the user for the name of the output file

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.

Hints

  1. Remember to create a function prototype and a function definition for your new 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.
  2. You may also want to use the 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.
  3. Inside your new function, you will need to output the open tag for the 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.
  4. You'll also, of course, need the 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!
  5. To test whether your web page is working, you need, each time, to copy it to your web directory. The first time you copy a file with a particular name, you need to do a chmod on that file (chmod 755 filename.html) to make it readable.

Finishing and Submitting

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.

Stage 7: Sort the array in ascending order by the numeric field ('s' option)
(Filename: proj2s7.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 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.

Final Submission

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

  1. Make sure that your proj2data.txt file and proj2data.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/proj2/proj2data.txt
    http://copland.udel.edu/~yourusername/cisc105/proj2/proj2data.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 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.)

    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 proj2.txt file to WebCT and submit.
  5. Print your proj2.txt using one of the following and give to your TA at the earliest possible date:
  6.  

Grading

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

 


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.