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

Introduction

Objectives

The main theme of this project is input and output 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:

  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. Output a float or double as a dollar amount, appropriately formatted.

You need to choose a "topic" before starting this project

In lecture, we took one day where each student chose a topic. Those topics are listed (or soon will be) on the web page at http://copland.udel.edu/~pconrad/cisc105/05F/classlists/topic.html. If you have not yet chosen a topic, or want to change your topic, please email your professor and cc your TA as soon as possible.

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

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. If you've finished that, then congratulations! You are already half way finished with project 1. The rest of the project builds from that point.

This project proceeds in stages

If you finished lab02, you've already finished stages 1 and 2 of this project. Even if you do no further work other than resubmitting what you've already done, you can get 50% credit for project 1. There are five additional stages; for each stage that you complete, you work towards additional "partial credit". After completing six stages, you can stop, and "settle for a B", or complete one additional stage to go for an "A" grade. This structure will be used for project 2 and 3 as well.

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 1: Posting your proj1data.txt file on the web
(Filename: proj1data.txt, 20% of total, 20% complete when done)

You completed this stage as part of lab02. See lab02.html for detailed instructions.

Stage 2: A program to read in two fields from your table, and echo them back nicely formatted.
(Filename: lab02a.c, 30% of total, 50% complete when done)

You completed this stage as part of lab02. See lab02.html for detailed instructions.

Stage 3: A program to read in one complete entry from your table and echo it back, nicely formatted.
(Filename: proj1s3.c, 20% of total, 70% complete when done)

The idea of this stage is to take what you did for the first two fields from your table back in lab02, and finish the job for all the fields you came up with.

Step-by-step instructions

  1. To get started with this stage, create a directory called ~/cisc105/proj1 and change directory into it:

    mkdir ~/cisc105/proj1
    cd ~/cisc105/proj1


    Then make a copy of your program lab02a.c and store it in your proj1 directory under the name proj1s3.c, as follows:

    cp ~/cisc105/lab02/lab02a.c proj1s3.c

  2. Now, edit the proj1s3.c file (that means "use emacs to go into the file to make changes"). You should already have code that reads in two fields from your table (e.g. name of hiking trail, and length of trail). In your case, the field names will be different, but the idea is the same.

    Your task now is to add code to read all the remaining fields from your table. For each field, you need to
    1. define a variable (either a char array, an int variable, a float, or a double
    2. add a prompt for your field
    3. right after the prompt, add a line of code to read in the value into your variable
    4. later, in the part that echoes back the data entered, add a printf to print out the value entered

    Here are some details about each of these points:

    1. define a variable (either a char array, an int variable, a float, or a double)
      • use int for integer data, double for numbers with decimal points
      • double is almost always a better choice than float for real numbers (I can't think of any exceptions)
      • later we'll see that we shouldn't use float or double for dollar amounts, but for now it is ok.
      • when defining a char array, also add a #define to indicate the length of the array instead of using a number
        • Example: #define NAME_OF_RESORT_LENGTH 20
        • Matching declaration: char nameOfResort[NAME_OF_RESORT_LENGTH];
        • Note that there is no semicolon on the #define, but there is one on the variable declaration.
        • Note that in this case, only 19 characters are available for the name itself; one character is reserved for the \0 that terminates the string.
    2. add a prompt for your field
      • Remember, a prompt is a printf() statement that tells the user what to type in.
      • Remember to end your prompt with a colon (:) and don't put a \n at the end.
      • Example: "Please enter price of lift ticket in dollars: "
      • Example: "Please enter year this album was released: "
    3. right after the prompt, add a line of code to read in the value into your variable
      • use scanf() (for int, float or double)
      • use fgets() (for character data) so you can have embedded spaces
      • Remember for scanf() to put an & in front of your variable name
      • With scanf(), use %d for int variables, %f for float, and %lf for double.
      • when using fgets(), be sure to use fflush(stdin); right before you read in the variable.
    4. later, in the part that echoes back the data entered, add a printf to print out the value entered
      • remember to use %d, %f, %lf or %s as appropriate in your format string.
      • when printing out the values, line them up neatly
      • for dollar amounts, a format of "$%6.2lf", for example, can be used for double, or "$%6.2f" for float. This gives you a dollar sign, 3 places before the decimal point, and 2 afterwards (3 + 2 = 5, but you have to count the decimal point as well, giving a total of 6, hence 6.2)

Stage 4: Add a simple calculation
(Filename:
proj1s4.c, 10% of total, 80% complete when done)

The main work of this stage is to add something to your output that involves a simple calculation. Take one of your numeric fields, and in addition to printing it out in its original form, also print it out in some new form after doing a calculation on it.

For example: this might be converting the units of one of your fields (e.g. from miles to kilometers, or ounces to grams. You could do Fahrenheit to Celsius, but that's so "five minutes ago". Try to choose something more original.

Another possible calculation: converting a "year" into "how many years ago was that". For example, if you are doing "albums by Jessica Simpson", you could add a variable for the current year (e.g. 2005) and add a separate prompt at the beginning of the program to have the user enter the current year. For example:

What year is this (e.g. 2005) ?

The user would enter 2005, and then you could subtract from that value the year each album came out. That way you are converting from "year of release" to "how many years ago the album came out."

The calculation doesn't have to be fancy. I just want to see that you know how to do simple calculations as part of your program. If you need help coming up with an idea, ask your TA or professor for advice.

Step-by-step instructions:

  1. For this stage, start by copying your proj1s3.c file to proj1s4.c.
  2. If your calculation involves a conversion, create a conversion constant at the top of your program with a #define such as

    #define CMS_PER_INCH 2.54 /* the number of centimeters in an inch */
  3. If your function involves a change of units, define a function for your conversion. Follow the example of the celsiusToFaren and farenToCelsius functions from lab01.
  4. If your function is a simple subtraction (e.g. "how many years ago"), but you need another variable such as "currentYear", add in the declaration of that variable and the prompt for that variable.
  5. Add in the output statement for your calculation. If the calculation is a simple one (involving no more than one arithmetic operator, i.e. one +, -, * or /), you can put the math right in the printf() call, for example:

    printf("How many years ago this album came out: %d\n", currentYear - yearOfRelease);

    Otherwise, use a function call to your conversion function:

    printf("Weight of digital camera in ounces    : %5.1lf\n", gramsToOunces(cameraWeight));

Stage 5: Add a simple comparison
(Filename: proj1s5.c: 10% of total, 90% complete when done)

In this stage you want to add a simple comparison (i.e. an if/else) to one of your numeric fields.

As an example, 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 < yearUserWasBorn)
  {
    printf("yes\n");
  }
else
  {
    printf("no\n");
  }

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 <= MAX_LENGTH_OF_SHORT_TRAIL)
  {
    printf("short\n");
  }
else
  {
    printf("long\n");
  }

To complete this stage, copy from proj1s4.c to proj1s5.c, and add C code to implement your decision. Add it into the end of the section of your program that produces the output, and test it.

 

Stage 6: Add code to create an output file in HTML format.
(Filename: proj1s6.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 5, 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 6, and its ok if you do.

What are the consequences of skipping stage 6? 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 code at the end of your program to write out your table as an HTML file—that is, as a nicely formatted web page. This is a very typical thing that real-world computer programs do these days. Instructions for how to do it will be provided in lab03 (which will not be available until 9am 9/23). That will still give you a full week to complete this step, so if you plan on trying to complete it, you should try to finish up stages 1 through 5 before coming to lab on 9/23.

Further instructions for this stage will be posted no later than 9am on 9/23/2005 in this space.

Final Submission

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

  1. Make sure that your proj1data.txt file is readable on the web at the following URL. Note that you'll need to substitute your actual strauss username in place of "yourusername". (You should have already completed this step as part of finishing up lab02.)

    http://copland.udel.edu/~yourusername/cisc105/proj1/proj1data.txt
  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.

    Examples:
  3. Create a script for that stage, and call it proj1.txt. In the script, do the following steps.

    If you did not complete stage 6, use these instructions. (If you did do stage 6, scroll down further for special instructions)
    1. Type cd ~/public_html/cisc105/proj1
    2. Type cat proj1data.txt
    3. Type cd ~/cisc105/proj1
    4. cat the .c file that you uploaded (the one for the stage where you stopped)
    5. compile it with either cc or gcc (doesn't matter which)
    6. Run it several times (e.g. ./a.out or ./proj1s5 or ./proj1s6).

      You should not do just one run (i.e. don't just type ./a.out once, and then type in some values, and that's it). Do several runs (for this project, at least three, but no more than five) typing in different values for each run.

      In particular, show that (a) your character fields can accept values with embedded spaces, and (b) show that your "decision" can go either way. For example, if your decision is to print "short" for a hiking trail that is less than 5 miles long, and "long" for a hiking trail that is longer than 5 miles long, you should show at least one run where your program ends up printing "short", and another one where it ends up printing "long".
    7. Type exit to complete the script

    If you did complete stage 6, use these instructions.
    1. Type cd ~/public_html/cisc105/proj1
    2. Type cat proj1data.txt
    3. Type cd ~/cisc105/proj1
    4. cat the .c file that you uploaded (the one for the stage where you stopped)
    5. Compile it with either cc or gcc (doesn't matter which)
    6. Type the command rm *.html to remove all the old .html files from your ~/cisc105/proj1 directory.
    7. Run it several times (e.g. ./a.out or ./proj1s6 or ./proj1s7). (Read all the instructions for this step before starting.)

      You should not do just one run (i.e. don't just type ./a.out once, and then type in some values, and that's it). Do several runs (for this project, at least three, but no more than five) typing in different values for each run.

      In particular, show that (a) your character fields can accept values with embedded spaces, and (b) show that your "decision" can go either way. For example, if your decision is to print "short" for a hiking trail that is less than 5 miles long, and "long" for a hiking trail that is longer than 5 miles long, you should show at least one run where your program ends up printing "short", and another one where it ends up printing "long".

      When you do each run of your program, before you do the run, do an ls command to show the contents of your directory. Each time you create a new .html file, give it a new name (i.e. a name that isn't already in your directory) when you run the program (you'll pass in the new name as a command line argument). Then, after each run, type ls again to show that the file was created by running your program (i.e. it didn't exist before, but it does exist afterwards.) On the printed copy of your script file, use a highlighter (or circle in pen or pencil) to show the name of the file you pass on the command line, and then where that name appears in the ls listing after you run the program.
    8. Type the following commands to publish your generated .html files to a web site where the TA can see them:
      mkdir -p ~/public_html/cisc105/proj1/stage6
      cp *.html ~/public_html/cisc105/proj1/stage6
      chmod -R a+rx ~/public_html/cisc105


      Them, be sure your files are readable on the web at http://copland.udel.edu/~yourusernamehere/cisc105/proj1/stage6
    9. Type exit to complete the script

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


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.