CMSC 106 Project #2 Spring 2002


Due date: 11:00 pm on Friday, March 1, 2002.

1. Purpose

In this project you will write a program using various conditional statements. The program will process orders for 3 different CD music sites. It will calculate the total cost based on list price, discounts, reward points, and shipping.

2. Project description

As you read this section, you may also want to refer to the ``Sample output'' section below.

The input to be read and the output which should be produced is discussed below.


  1. The program must first read the CD order information. The data will be given to the program thorugh standard input redirection from an ASCII text file. As the programmer, you can assume the lines of the input file will be exactly as described here. You do not need to do any checking of the validity of the input.

    1. The first line will contain a number which represents which site the customer is ordering from:
      • 1 means CDLater
      • 2 means MrCD
      • 3 means CDHut
    2. The second line will contain the list prices of 3 CD's. These values will be positive; the program may assume that they are all different. They may or may not contain a decimal point or a fractional part.
    3. If the site is CDLater, each price will be followed by a 1 if the CD is on the customer's wish list, or a 0 if it is not on the wish list.
    4. The third line will contain a number which represents the shipping method for the order:
      • 1 means Regular
      • 2 means 2-Day
      • 3 means Overnight
    5. The fourth line will contain the customer's current number of reward points.

  2. The output will be a summary of the customer's order, including the total cost of the CD's and shipping charges.

    1. The first part of the output consists of a list of the items ordered, including both the list price and the cost after any discounts or reward points. The message for each item must be in the form:

      • List price: $dd.cc Your cost: $dd.cc

      where dd is the portion of the price in dollars, and cc is the portion of the price in cents. The value for cents must be 2 digits, including a leading zero if necessary. The items must be listed in order from the most expensive list price to the least expensive list price. If the actual cost of an item is $0.00, then the value must be given using the word FREE!.

    2. Following the list of items, the program will print the total cost of the items using a message of the form:

      • Total Cost: $dd.cc

      where dd is the dollar portion of the cost, and cc is the cents portion. The value for total cost must align neatly in a column with the cost of the individual items, as shown in the sample output.

    3. Following the total cost of the CD's, the program will print the shipping cost using a message of the form:

      • Shipping: $dd.cc

      The shipping cost must align neatly in a column with the total cost. If the shipping cost is $0.00, then the value must be given using the word FREE!.

    4. Following the shipping cost, the program must print the new point total using a message of the form:

      • Your new reward point total is: p
      where p is the new quantity of reward points.

    5. The last message will thank the customer with a message of the form:

      • Thank you for shopping at SITE.

      where SITE is CDLater, MrCD, or CDHut.

2.1 Order Processing Rules

  1. CDLater

  2. MrCD

  3. CDHut

3. Project requirements

All your C programs in this course should be written in ANSI C with trapping of unintialized variables, which means they must compile and run correctly with cc -std1 -trapuv on the OIT UNIX Class Cluster. Even if you already know what they are, you may not use any C language features other than those introduced in Chapters 1 through 5 of your textbook. Note that as a result loops, arrays, and user-defined functions may not be used. In addition, your program must contain only one single return statement at its end. Using C features not in these chapters will result in losing credit.

Your program must have a comment near the top which contains your name, login ID, student ID, your section number, your TA's name, and an original description of the action and operation of the program. Do not put your alias in this comment! Your program should be written using good programming style and formatting, as discussed in class and throughout your textbook. For this project, style is considered to consist of:


4. Developing your program

You may want to skip this section at first, read the rest of the project, and come back to study it carefully when you are about to begin writing your program.

4.1 Possible development steps

It is very important to continue to type in only a part of your program at a time and verify that it's correct, before going on. Any mistake is much easier to locate this way. The idea is that at all times you have a correct program, which may be incomplete and solve only part of the problem but at least the part you've written so far works.

In other words, do not attempt to write your program to perform all the necessary tests or produce completely correct output at first. Add code to perform only one processing step at a time and produce its output, and then add another until your program is complete.

For example, here is one way to begin developing the program one part at a time. Write the code for each of the following steps, and compile and test at each stage to make sure the correct results are printed for different inputs before going on to the next step listed.


  1. First you could write code which reads the order information. To be sure they are being read correctly, add statements which echo each one immediately after reading.

  2. If your code so far works, write the output lines which correctly format the list prices for the order summary as described above.

  3. When you have verified that these input and output statements work properly, you could determine which set of pricing rules will be used for this order.

  4. Then go through different cases - one for each type of order and make sure the output produced is correct for that situation.

  5. Finish the remaining cases one at a time, and thoroughly test your program using a variety of input combinations.

4.2 Finding compilation errors

  1. As before, if you get the error ``Invalid statement'' and the compiler identifies the very last line in your program, it usually means you have mismatched braces. Carefully match up all of your braces, and make sure that there is a closing brace to match every opening brace. This is much easier to do using a printout, and is far easier when your program is formatted neatly using proper indentation and placement of braces. Draw a line pairing up each set of matching braces until you can tell where one is missing.

  2. If you get the error ``Invalid statement'' and the compiler points right to an else, the else is not placed so it's inside an if statement. Only one statement can appear as the subsidiary statement after an if, and only one statement can appear after an else. If you want more than one statement to appear in a place where one statement belongs, you can use braces to group them into a compound statement. You may have forgotten braces around the statements which were supposed to be executed if the condition in the if/else was true (the first group of subsidiary statements). Alternatively, it's possible you made a mistake and have more elses than ifs. You are much less likely to make this mistake when your code is properly formatted and indented, with each else lined up to clearly indicate which if statement it belongs to.

4.3 Program debugging

If your program doesn't produce the results you think it should, the debug printf statement is the best tool you can use. If, for instance, you're not sure why one branch of an if/else statement is being executed when you think the other should be, put a debug printf statement right before the whole if statement which prints the values of all the variables used in the condition. Maybe one of them doesn't have the value which you think it should have at that point. On the other hand, if you run your program and see that the variables all have correct values, then the condition itself is probably not expressed correctly. Or perhaps nothing prints in some case where you think something should. Try adding an else to every if statement which doesn't already have one, which prints a message indicating what must be true for that statement to be printed. Also in other cases, add as many printfs as you need to determine what is true about the data currently stored.

Make sure the debug printfs you add are descriptive in nature to tell you any information you may need to determine what your program is doing, such as what line in in the program is producing this output or what the values of all relevant variables are at that point. This is much more useful than printing something like ``I am here'' in your debug printfs. Also, make sure the debug printfs do not change what statements are controled by the if or the else.

Also for technical reasons not worth explaining, it is very important that every debug printf ends with the newline character '\n'. Although these debug printfs will make your output look incorrect, they will help you develop your program correctly.

Make sure that all debug printfs are removed before you submit your source code file for grading. After removing them, before submitting your source file, make sure that you have compiled, run and tested your project again to be sure you did not accidentally change it when you were removing those lines.

If you can't find the problem after trying to debug your program using these techniques, bring printouts of your program and any compiler errors or execution results to office hours. To help us find the mistake as quickly as possible, be prepared to demonstrate and explain how you have tested your program, and what results you got.

4.4 Program testing

It is very important to test your program against all different possibilities of input data, not just the values shown in the sample output section below. Doing this correctly involves thinking of what other input values are possible based on the project description, and what results the program should produce in those cases.

As an example, say you had to write a program which reads three integers into variables a, b, and c, and prints the name of the variable which had the largest value. Imagine that you wrote the following program:



#include <stdio.h>

main() {
  int a, b, c;
  scanf("%d%d%d", &a, &b, &c);

  if (a <= b) {

    printf("b");

    if (b <= c)
      printf("c");

  } else printf("a");

  printf("\n");
  return 0;
}


Try typing, compiling, and executing this program.

If you run the program and enter 2 6 4 it prints ``b'', which is correct.

If you run it and enter 6 4 2 it prints ``a'', which is correct.

You might assume the program works since you ran it twice with different input values, but it is not correct.

Try entering 4 2 6 and it prints ``a'', which is wrong.

Try entering 2 4 6 and it prints ``bc'', which is also wrong.

There are other possibilities of input values which cause incorrect results as well.

The conclusion is that it is always necessary to think of all the different possible combinations and possibilities of input values which might appear in a program's input, and test it so you are certain it works correctly in all cases, not just for one or two different inputs.

If you are curious, one correct program to perform this task is as follows. The program could also be written using the logical operators.



#include <stdio.h>

main() {
  int a, b, c;
  scanf("%d%d%d", &a, &b, &c);

  if (a <= b)

    if (b <= c)
      print("c");
    else print("b");

  else

    if (a <= c)
      print("c");
    else print("a");

  printf("\n");
  return 0;
}


4.5 Helpful hints

One development policy which is extremely important is to use proper indentation from the beginning. Many students type in their programs any way at all, with poor or no indentation, figuring once their project works correctly they will clean it up before submitting it. This is a very bad idea, because it is much harder to find certain errors in a program with poor indentation, and it's much easier to make these types of mistakes. You are much more likely to have problems such as mismatched braces, or misplacing an else, or misunderstanding which statements are subsidiary to which if statements if your code is improperly formatted or indented, or the braces are incorrectly placed. In the Q+A sections your textbook shows several popular styles of indentation. It doesn't matter which you use, as long as your code is clear and your indentation is consistent, subsidiary statements are all indented, and statements at the same level are all indented the same way. If you come to office hours for help with a program which has missing or very poor indentation, you will be asked to correct and reformat your program before we are able to help you further with it.

5. Academic integrity statement

Any evidence of unauthorized use of computer accounts or cooperation on projects will be submitted to the Student Honor Council, which could result in an XF for the course, suspension, or expulsion from the University. Projects are to be written INDIVIDUALLY. For academic honesty purposes, projects are to be considered comparable to a take-home exam. Any cooperation or exchange of ideas which would be prohibited on an exam is also prohibited on a project assignment, and WILL BE REPORTED to the Honor Council.


VIOLATIONS OF ACADEMIC HONESTY INCLUDE:


  1. failing to do all or any of the work on a project by yourself, other than assistance from the instructional staff.

  2. using any ideas or any part of another student's project, or copying any other individual's work in any way.

  3. giving any parts or ideas from your project, including test data, to another student.

  4. having programs on an open account or on a PC that other students can access.

  5. transferring any part of a project to or from another student or individual by any means, electronic or otherwise.


IT IS THE RESPONSIBILITY, UNDER THE UNIVERSITY HONOR POLICY, OF ANY STUDENT WHO LEARNS OF AN INCIDENT OF ACADEMIC DISHONESTY TO REPORT IT TO THEIR INSTRUCTOR.

6. Submitting your project

Your project must be electronically submitted by the date above, before 11:00pm, to avoid losing credit as indicated on the syllabus. No projects more than two days late will be accepted for credit without prior permission or a valid medical excuse, as described on your syllabus. Only the project which you electronically submit, according to the procedures provided, can be graded; it is your responsibility to test your program and verify that it works properly before submitting. Lost passwords or other system problems do not constitute valid justifications for late projects, so do not put off working on your program or wait to submit it at the last minute!

Turn in your assignment using the ``submit'' program as before, except using ``2'' for the project number. You are to submit only the .c file containing your source code, not the executable version of your program! If your program is in a file named ``order.c'', submit would be run as shown.


% submit  2  order.c

Before you submit your project, you must exactly follow the specific submission checklist in the ``Testing projects before submitting'' handout separately posted by your instructor!


7. Sample output

Assuming the name of the executable version of the program is ``order.x'', here are two executions for two different sets of input values. Note that the input file is shown separately before each output set, and nothing is typed by the user as the program is run.

Be sure to test your program against a variety of inputs, so you are sure it works in all circumstances!



% cat input1
1
15 1 19 0 13 1
1
100

% order.x < input1

List price: $19.00 Your cost: $13.30
List price: $15.00 Your cost: $ 8.50
List price: $13.00 Your cost: $ 7.10
Total Cost:                   $28.90
Shipping:                     $ 4.00
Your new reward point total is: 128
Thank you for shopping at CDLater

% cat input2

2
13 15 19
1
20

% order.x < input2

List price: $19.00 Your cost: FREE!
List price: $15.00 Your cost: $ 7.50
List price: $13.00 Your cost: $ 3.90
Total Cost:                   $11.40
Shipping:                     $ 6.87
Your new reward point total is: 4
Thank you for shopping at MrCD



  



Steve Scolnik
2002-02-18