Lab Assignment # 2, CSE 1320 Sections 001 and 501, Fall 2004

 

Due Date:                  Section 501 – Oct 19th, 5:30 pm

Section 001 –Oct 20th, 1:00 pm. 

(see instructions on website for how to turn this in)

[Clarifications added Oct 12th are highlighted in yellow throughout lab]

 

Topic objectives:                                Pointers and dynamic memory space allocation

                                Strings and string functions

                                Arrays of pointers

                                More control structures

                                Functions, parameter passing, side effects

                                Modular design

 

Well, after working through your financial simulation you have decided to go ahead and start planning the details for your bookstore, Plus or Minus Perfect Books (or ‘±Perfect’ for short).  You’ve decided that the first things you should do would be to finalize the categories of books you will carry, select a few prominent new titles for each category, and start communicating with publishers and distributors to develop the process you will use to stock your store once it gets started.  You will be using potential customers to help decide on categories and titles and you will make decisions about publishers/distributors based on cost and availability information they supply.

 

In order to accomplish all this your program will do the following tasks.  First, the program will accept at least 3 but no more than 10 categories of books to be sold in the store.  The category names will be read in from the user as strings and the strings will be referenced by pointers in an array.  Once the user has entered all the categories they want, then for each category the program must have the user enter two titles of books that will be used to compare publishers and distributors.  For each book, the user will enter a title, an author’s last name, a retail price, and a quantity.  The title and author will be stored as arrays of strings and retail price and quantity stored in numeric arrays like in the financial simulation lab.     The following are the tasks you must do:

 

  1. First you must read in no less than three and no more than 10 categories from the user.  The category names will be read in as strings (and can be more than one word like “Science Fiction” for example.)  Each string must be saved to dynamically allocated space that is referenced from an array of pointers. 
  2. You must give the user some method of indicating that they have finished entering data for categories, i.e. you must not require them to always enter 10 categories when the user only wants 6 categories.
  3. When all the categories are entered, the user must enter two books for each category, giving a title, an author’s last name, a retail price, and a quantity.  The title and author must be dynamically allocated strings referenced from arrays of pointers (one array for titles and one for authors).  The retail prices must be stored in a floating point type array and the quantities stored in an integer array.  The indices for each book will be the same in all for arrays.  (For example, the data for the third book that is read in will be in element [2] of the title array, the author array, the price and quantity arrays.)
  4. Once all of this data is entered (tasks 1 – 3) the program should print all of the current information to the screen by category.  Give the name of the category and then print both books (with all four pieces of info) in that category before printing the next category.

 

After the user has entered books for each category then the program will begin the decision making process for publishers/distributors.  In this stage the program will be querying the user for data from a particular publisher/distributor about all of the entered books.  The program must find out from each publisher/distributor how much the wholesale price is for each book in the bookstore and how many copies of the book are available from the publisher/distributor of that book.  This data is collected for every book that was entered previously.  When all of the data for a single publisher/distributor has been collated, the program must ask the user if they wish to do the same for another publisher/distributor until the data for all potential publishers/distributors has been entered.  Then the program will make recommendations based on the publisher/distributor information as to which company or companies you, ±Perfect Books, should use.

  1. After all the data has been printed (task 4 above), then the program should ask the user how many publishers/distributors will be compared.  This value is then used to allocated storage space for the data collected about the publisher/distributor.  The maximum number of publishers is 10.
  2. The user is then asked to enter the name of a publisher or distributor that is being considered to supply the bookstore.  The program should read in this name as a string and store if with a reference in an array of pointers.
  3. The program should now prompt the user to enter wholesale price and available copies for each of the books that were previously entered.  The wholesale price should be stored in an array of floating point numbers and the available copies value should be stored in an integer array which the element indices related to the element containing the appropriate book.  (Ex: for the book [2] mentioned above, the wholesale price would be [2] in its array)
  4. When all the data for every book has been entered for a publisher/distributor, the program determines if all publisher/distributors have been entered.  If not, the program returns to task 6.  If so, the program proceeds to task 9.
  5. When all the data has been collected for the publisher/distributors, the program will make the following calculations:
    1. For each publisher/distributor, calculate the cost of a wholesale order by multiplying the wholesale price of each book by the original requested quantity, and summing these values.  Store this value for each publisher/distributor.
    2. For each publisher/distributor determine whether the available quantities of books they have is sufficient to meet your requested amounts of books. For each book, store a value indicating what percentage of the total requested quantity of books is available from the publisher/distributor.  Then calculate on overall availability percentage as follows:

                                                               i.      For each book with availability over 140%, change the book percentage to 140%

                                                             ii.      Add all the book availability percentages and divide by the number of books to get the percentage of total requested quantity for that publisher/distributor.

    1. For each publisher/distributor, compare the wholesale price and the retail prices and calculate the percentage of profit that would be made based on the requested quantities of each book.  ( If retail price is $10.00 and the wholesale price is $5.00 then for that one book the profit is 100%.)  Calculate this profit percentage as follows:

                                                               i.      For each book calculate the book profit as

bookprofit = (retail price – wholesale price) * requested book amount;

Then after each books values have been calculated for a publisher/distributor:

                                                             ii.      For each publisher/distributor calculate grossprofit as

grossprofit = sum the all bookprofits for that publisher/distributor ;

                                                           iii.      For each publisher/distributor calculate percentage profit as

percprofit = ( grossprofit / cost of a wholesale order (part a. above) ) * 100 ;

  1. When the calculations are complete, for each publisher/distributor
    1. Print the publisher/distributor name and the wholesale order cost.   (9 a)
    2. Then print a statement indicating if the publisher/distributor has less than 80% of the requested books available, between 80% and 120% available, or over 120% of the requested amount available.  (9 b ii)
    3. Then print a statement indicating if working with the publisher/distributor would give:  (9 c iii)

                                                               i.      Less than 25% profit

                                                             ii.      Between 25% and 50% profit

                                                           iii.      Between 50% and 75% profit

                                                           iv.      Between 75% and 100% profit

                                                             v.      Over 100% profit

  1. Lastly, print a list of only those publisher/distributor names which have at least 80% availability of books and which would yield at least 50% profit for ±Perfect Books.  These are the possible publisher/distributors to work with.

 

After this is done, print a concluding message and then end the program.

 

Implementation requirements:

The program should use the following data structures:

Integer and floating point arrays as described above

Arrays of pointers to char for referencing strings

Buffers for reading in strings (either char arrays or malloc’d space)

The program should NOT use the following data structures:

structs

 

The program should use the following control structures:

Switch statements to perform the tasks in parts 10b and 10c

Loops and other control structures already discussed as needed

The program should NOT use the following control structures:

breaks outside of switch structures

continues

gotos

exits

 

The program should have a main function and at least four (4) subfunctions but no more than twelve (12) subfunctions.  Each function should focus on one particular task.

 

This program must be run with at least two different sets of data.  The first data set (data set 1) should use the values given below.  You must also create at least one additional data set and run your program with it as well.  You must execute the program two different times so that you have a total of at least two different data sets.  The sample data sets that you create must meet the guidelines given in the problem definition.

 

Sample input values for data set 1:   

 

Category        Book title        Author        Retail        Quantity

Mystery:         “Shattered”        Francis        24.95        10

Mystery:         “God Bless John Wayne”        Friedman        6.00        5    

Sci-fi:             “I, Robot”        Asimov        7.99        250

Sci-fi:             “Monstrous Regiment”        Pratchett        22.49        15

Self-help:          “Life and How to Survive It”        Cleese        12.00        20

Self-help:        “How to Ruin Your Life”        Stein        12.95        2

 

Publisher/distributor name        Book title        Wholesale        Available

Amazon               “Shattered”        9.99        90

Amazon               “God Bless John Wayne”        4.00        25

Amazon               “I, Robot”        2.00        2000

Amazon               “Monstrous Regiment”        16.99        12

Amazon               “Life and How to Survive It”        10.00        100

Amazon               “How to Ruin Your Life”        5.00        250

Barbie                   “Shattered”        19.99        100

Barbie                   “God Bless John Wayne”        4.50        100

Barbie                   “I, Robot”        2.50        100

Barbie                   “Monstrous Regiment”        12.99        100

Barbie                   “Life and How to Survive It”        7.50        100

Barbie                   “How to Ruin Your Life”        15.00        100

Campbell            “Shattered”        15.00        10

Campbell            “God Bless John Wayne”        6.00        0

Campbell            “I, Robot”        6.00        10

Campbell            “Monstrous Regiment”        15.00        100

Campbell            “Life and How to Survive It”        6.00        10

Campbell            “How to Ruin Your Life”        6.00        100

Dumble                        “Shattered”        10.00        1000

Dumble                        “God Bless John Wayne”        3.00        5000

Dumble                        “I, Robot”        3.00        5000

Dumble                        “Monstrous Regiment”        10.00        1000

Dumble                        “Life and How to Survive It”        3.00        5000

Dumble                        “How to Ruin Your Life”        3.00        5000

 

 

General implementation requirements for all assignments:

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

Declare and initialize the variables

Print a welcome screen for the user that introduces the system

Get the needed input value from the keyboard

Print the appropriate outputs

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

 

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.

 

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 ask the TA or OIT how to use this function.  

 

Grading scale:

Code:     (78%)

                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 strings including pointer declaration and dynamic memory allocation  (12 points)

Correct manipulation of the arrays of pointers  (10 points)

Correct use of required control structures (6 points)

Correct function structure as required (5 points)

                Each task performs mathematically correctly (6 points per task 9a., 9b, 9c.)

Proper implementation of input and user control (7 points)

Output:          (22%)

                User clearly understands what is being requested for input (6 points)

                Output gives clear information to explain the values to the user (6 points)

                Output contains all the sample data and at least one additional data set  (10 points)

Deductions:

                Use of structs will result in a 10 point deduction

                Use of continue will result in a 20 point deduction

                Use of a break outside of a switch statement will result in a 20 point deduction

                Use of global variables will result in an overall grade of 0 (zero)

                Use of goto or exit will result in an overall grade of 0 (zero)

                Late submission of softcopy to appropriate TA will result in an overall grade of 0 (zero)

                Use of C language elements not yet discussed in class by the lab due date will result in potential deduction of points – discuss with instructor before using.