CS 54 - Lab 4


Review: enum

An enum (short for enumeration) is a way symbolically naming integer constants. Enumerations are useful when you have a group of related objects with different names:
enum cs53Instructor {CLAYTON, ALTON, JOSH};
enum orange {NAVEL, VALENCIA, MANDARIN, BLOOD};

enum by default will start counting from 0 and then assign values of 1, 2, ..., n for the items that follow. So, in the orange example, NAVEL will map to 0, VALENCIA to 1, MANDARIN to 2, and BLOOD to 3.

/* present the list of instructors */
int prof;
cout << "0) Clayton Price" << endl;
cout << "1) Alton Coalter" << endl;
cout << "2) Josh Wilkerson" << endl;
cout << "Who is your CS 53 Instructor? ";
cin >> prof;

/* tell the user who they chose */
if (prof == CLAYTON)
{
  cout << "You have class with Clayton Price." << endl;
}
else if (prof == ALTON)
{
  cout << "You have class with Alton Coalter." << endl;
}
else if (prof == JOSH)
{
  cout << "You have class with Josh Wilkerson." << endl;
}

Perhaps the only problem with the example above is the way enum counts from 0 and humans count from 1. You can explicitly assign the first member to be 1. This will make the enum count from 1:
enum cs53Instructor {CLAYTON = 1, ALTON, JOSH};
Explicit assignment is not limited to just the first item. You can assign any value to any item. If you don't assign a value, C++ will just increase the count by 1 for the next item:
enum number {ZERO, ONE, TEN = 10, ELEVEN, ANSWER = 42, HUNDRED = 100, HUNDREDONE};


Lab Assignment

Recursive Automobile Rip Offs (RARO, also defined as "RARO is Automobile Rip Offs") has an old database system to track its fleet. The problem is that the database system is buggy and poorly written. You have been hired to rewrite the inventory section of the system. The following models were available for sale in the old system:

CarCostPrice
Sawdust 325i$50$7300
Jalopy 2400$22$6600
BeatupMobile ZX7$42$9200

Lab Assignment - Part I (Required - 8 Points)

Write a program that will present the user with the possible cars to put in the warehouse and then ask how many of the car they want to insert. Be sure to use an enum to maximize code readability.


Lab Assignment - Part II (Quasi Required - 10 Points)

Take your assignment for Part I and loop while they still want to enter more cars in the warehouse. At the end, report how many cars are in the inventory. Note that this will require to have three running totals of how many of each car we have. The output for this part should like something like this:

Select the car you want to add to inventory.
1) Sawdust 325i
2) Jalopy 2400
3) BeatupMobile ZX7
4) Exit

Which car do you want to enter into inventory? 1
How many more Sawdust 325i's do we have? 3
Which car do you want to enter into inventory? 2
How many more Jalopy 2400's do we have? 4
Which car do you want to enter into inventory? 3
How many more BeatupMobile ZX7's do we have? 2
Which car do you want to enter into inventory? 1
How many more Sawdust 325i's do we have? 4
Which car do you want to enter into inventory? 4

We have:
7 Sawdust 325i's
4 Jalopy 2400's
2 BeatupMobile ZX7's


Lab Assignment - Part III (Optional - 12 Points)

Take your program from Part II and calculate the projected profit of the inventory. If they sell everything, the profit, y, from each car would simply be:
y = p - c
where p is the price of the car and c is the cost. But the fact of the matter is they didn't sell all cars in their inventory. So, to simulate sales that did not go through, we will randomly decide whether a particular car was sold or not. If the car was determined as sold, add it to the net profit. If not, skip it and try the next one. In summary, we have the following algorithm:

The real challenge here is finding a way to “stochastically” (randomly) decide whether the car was sold. I will leave it to you to invent how to make the decision. But in order to get randomness, you need to learn to use the srand() and rand() functions. Part of the extra credit is learning to use them by researching it on the web. The true measure of your stochastic decision process is that, for the same configuration of the inventory, the amount of profit varies each time.


Grading & Submission

Part I is worth 8 points. Part II is worth 10 points (Part I + 2 more points). Part III is worth 12 points (Part II + 2 points extra credit). The lab is out of 10 points. When you're done, submit your lab04.cpp file on Blackboard. I will be grading your program on the following criteria:


[Dilbert]