CMSC104, Spring 2002

Programming Project 7

Shopping List

Out: Tuesday, May 7, 2002
Due: Tuesday, May 14, 2002 before midnight


The Objective

This project is designed to give you practice working with arrays and functions. It will also give you practice passing arrays to functions.

The Task

Your job is to write a program that will read in the purchase prices of several items in a grocery store. You will then display the the receipt, listing the items purchased and the price of the bill. You are to store the values of the items in an array. Here is the main function that you MUST use in your program: int main() { float groceryList [MAX]; /* Array to store prices of items purchased */ float subTotal = 0.0; /* Total amount of grocery bill before tax */ float taxAmount = 0.0; /* Amount of tax */ float finalTotal = 0.0; /* Final grocery total with tax included */ int numberOfItems = 0; /* Total items purchased */ /* Fill array with items purchased */ numberOfItems = FillGroceryList (groceryList); /* Calculate sub total */ subTotal = CalculateSubTotal (groceryList, numberOfItems); /* Calculate tax */ taxAmount = CalculateTax (subTotal, TAX); /* Add tax to sub total to compute final total */ finalTotal = subTotal + taxAmount; /* Print the receipt */ PrintReceipt (groceryList, numberOfItems, subTotal, taxAmount, finalTotal); return 0; }

You MUST use the main() function exactly as it is given. However, you need to add the following code before main:

You must also add the definitions of the functions after main(). You may add additional functions if you wish.

Input to the Program

A data file containing the items for you to use as input to your program will be provided. The items are all greater than 0. The last value in the file will be 0. This is the sentinel value that signals the program to stop reading items.

To use the data file as input to your program, you will use Linux redirection. By using redirection, you can tell Linux to read data from a file rather than from the keyboard. The scanf statement that you use in your program will look exactly the same as it would if you were getting your input from the keyboard. But since you will be getting the values from a file instead of from a user typing at the keyboard, you will not need to prompt the user. When you run your program, use the following command:

a.out < items.dat This is how Linux redirection is done. It is saying to run your executable file using the file items.dat as input.

You will need to copy the file items.dat into your directory. To do this, go to the directory where you would like to store items.dat. Then, use the following command to copy items.dat into the directory:

cp /afs/umbc.edu/users/d/f/dforna1/pub/CS104/Proj7/items.dat . Notice that the space and period at the end of the command are part of the command.

Here is a example of what the input data file could look like:

1.50 2.57 1.35 2.90 5.90 7.50 .98 4.50 3.75 1.25 0

You will also find proj7.c in the directory with items.dat. You can copy it to your own directory following the above directions.

Sample Output

linux2[34]% gcc proj7.c linux2[35]% cat items.dat 1.50 2.57 1.35 2.90 5.90 7.50 .98 4.50 3.75 1.25 0 linux2[36]% a.out < items.dat --------------------------------------------- | R E C E I P T | | | | | | You purchased the following ten items: | | | | 01) 1.50 | | 02) 2.57 | | 03) 1.35 | | 04) 2.90 | | 05) 5.90 | | 06) 7.50 | | 07) 0.98 | | 08) 4.50 | | 09) 3.75 | | 10) 1.25 | | | | | | Sub Total: $ 32.20 | | Tax: $ 1.61 | | Final Total: $ 33.81 | | | | Thank you for your purchase! | --------------------------------------------- linux2[37]% Your output does not have to match the sample exactly, with a few exceptions:

Submitting the Program

Here is a sample submission command. Note that the project name starts with uppercase 'P'.

submit cs104_0101 Proj7 proj7.c

To verify that your project was submitted, you can execute the following command at the Unix prompt. It will show the file that you submitted in a format similar to the Unix 'ls' command.

submitls cs104_0101 Proj7