Lab Assignment # 2, CSE 1320-501, Fall 2003

 

Topic objectives:      Pointers and space allocation

                                      Strings

                                      More control structures

                                      Functions, parameter passing, side effects

 

In order to help out a friend (Chris Green), you have been given the task of collecting and analyzing data about customers in your friend’s speciality shop.  Your friend sells green things.  If it’s green, it could be in your friend’s shop.  Your friend wants to know which kinds of green things sell the best and which items bring in the most money. 

 

Your input will come from the user of the program (your friend) who will type in 4 pieces of information on each line of input from the keyboard.  Each line will have the following information:

 

Name_of_item    Category   Cost_of_one_item    Total_of_this_item_sold_this_week

 

Each of these four pieces of info will be a single value, i.e. the Name_of_item would be “lima_beans” (all one word with the underscore) not “lima beans” (two separate words).  The Name is a string, the Category is one of the following single letters - ‘A’ for animal, ‘V’ for vegetable, ‘M’ for mineral, or ‘O’ for other, the Cost is a floating point number, and the Total is an integer.  Every week your friend has at least 10 items in the shop but possibly more.  Your program should do the following tasks in a loop:

 

1)     Read in a line of data from the user

2)     If the item just read has the best sales record of the week, i.e. more of these sold than anything else, save all of the items information in a set of best_ variables (best_Name, best_Category, best_Cost, and  best_Total)

3)     If the item just read has brought in the most money, i.e. the Cost times the Total is larger than for any other item, save all of the items information in a set of most_ variables (most_Name, most_Category, most_Cost, and  most_Total)

4)     For the item just read, use a switch statement based on the Category to print the item’s information as follows:

a.      For ‘A’, print the message “We sold Total Name animals at $Cost each this week” where Total, Name, and Cost are the corresponding values for the item being printed.  (e.g. We sold 14 tree_frog animals at $42.74 this week)

b.      For ‘M’, print the message “This week, Total Name minerals at $Cost each were sold”

c.      For ‘V’, print “Even at $Cost each, we sold Total Name vegetables this week”

d.      For ‘O’, print “Whatever these Name are, we sold Total at $Cost each this week”

5)     Ask the user if there are more items to enter.  If ‘Yes’ return to step 1) above, else for ‘No’ continue to the step labeled 6)

6)     When the user has finished entering all the items, print out a message telling which was the best (from 2 above) item and which was the most (from 3 above) item along with the amount and cost info for each.

 

Implementation requirements:

The program must use the following data structures:

A string that is allocated for each item Name read in - use a char * type and either malloc or calloc

 

The program must use the following control structures:

A switch to handle the output messages by category

Function calls to perform the tasks - use at least two functions in addition to main and pass ALL parameters explicitly (no global variables)

 

The program should perform the following actions in the given order:

Declare and initialize the variables

Print a short introduction to the system for the user

Allocate space for a string for Name

Get the needed input values from the keyboard

Compare and keep track of the best and most values

Print the appropriate outputs

If Name is not still needed, free the space used for Name

Let the user enter additional values until the user indicates that they are finished.

Print the final values for best and most

 

As in Lab #1, the program should have a program header which gives, at least, your name, the number of the lab assignment, your class and section, the assignment date, the due date, and a description of the program.  If multiple files are used, each file should contain a similar header. 

Each programmer-defined function, i.e. each function you write, should have a function header similar to those used in the examples in the textbook.  This header should include at least the function name, the purpose of the function, and its inputs and outputs.

 

This program must be run using the sample data that is given below AND must be run using your own sample data as well (at least 5 additional items).  The sample data set that you create must meet the guidelines given in the problem definition.

 

The program output must be recorded in a script file from OMEGA using the gcc compiler.  If you do not know how to create a script file, it is your responsibility to read the instructions on  my website and to  ask the TA or OIT how to use this function.  

 

Sample input values:  

 

               Emeralds  M  5000.00  1

               Spinach_leaves  V  .35  209

               Green_marbles  O  1.24  12

               Green_marble_tiles  M  235.12  12000

               Garter_snakes  A  24.99  2

 

 

Grading scale:

Code:    (72%)

               Headers, Style, Modularity (20 points)

Program header and function headers for all functions         

Style (indentation, consistency, meaningful identifiers, lateral separation of code from line comments, etc.)

Modularity (division of the problem into small tasks, each one assigned to its own function and called from main() or from another function when appropriate--do not code the entire program in main!)

Correct manipulation of the string including declaration and memory allocation  (12 points)

Correct use of required control structures (8 points)

Correct maintenance of best and most information (12 points)

Correct function structure as required (8 points)

Proper implementation of input and user control (12 points)

Output:          (28%)

               Output follows the form given above - tasks 4 and 6  (16 points)

               Output (can be more than one session of running the program)  contains all the sample data and at least 5 more additional input values  (12 points)