Question
C++ programming. Here is my code. I need it updated to meet the new instructions below. #include #include #include using namespace std; int main() {
C++ programming. Here is my code. I need it updated to meet the new instructions below.
#include
#include
#include
using namespace std;
int main() {
const int RANDOM_SEED = 42;
const int MENU_EXIT = 4;
const int MENU_OPTIONS = 3;
const int MENU_DROP_MULTIPLE_CHIP = 2;
const int MENU_DROP_SINGLE_CHIP = 1;
const int MIN_SLOT_NUM = 0;
const int MAX_SLOT_NUM = 8;
const int NUM_PEGS = 12;
const int ONE_DECIMAL_PLACE = 1;
const int TWO_DECIMAL_PLACE = 2;
const int SLOT_0_WINNINGS = 100.0;
const int SLOT_1_WINNINGS = 500.0;
const int SLOT_2_WINNINGS = 1000.0;
const int SLOT_3_WINNINGS = 0.0;
const int SLOT_4_WINNINGS = 10000.0;
const int SLOT_5_WINNINGS = 0.0;
const int SLOT_6_WINNINGS = 1000.0;
const int SLOT_7_WINNINGS = 500.0;
const int SLOT_8_WINNINGS = 100.0;
const double PEG_VALUE = 0.5;
int userMenuChoice = 0;
int numSlot = 0;
int numChip = 0;
int randNum = 0;
double winnings = 0.0;
double totalWinnings = 0.0;
double totalAvgWinnings = 0.0;
double numSlotForLoop = 0.0;
srand(RANDOM_SEED);
cout << "Welcome to the plinko simulator! Enter 3 to see options." << endl;
do {
cout << "Enter your selection now: " << endl;
cin >> userMenuChoice;
if (userMenuChoice > MENU_EXIT || userMenuChoice < MENU_DROP_SINGLE_CHIP) {
cout << "Invalid selection. Enter 3 to see options." << endl;
}
if (userMenuChoice == MENU_OPTIONS) {
cout << "Menu: Please select one of the following options:" << endl;
cout << "1 - Drop a single chip into one slot" << endl;
cout << "2 - Drop multiple chips into one slot" << endl;
cout << "3 - Show the options menu" << endl;
cout << "4 - Quit the program" << endl;
}
if (userMenuChoice == MENU_DROP_SINGLE_CHIP) {
cout << "*** Drop a single chip ***" << endl;
cout << "Which slot do you want to drop the chip in (0-8)? " << endl;
cin >> numSlot;
if (numSlot < MIN_SLOT_NUM || numSlot > MAX_SLOT_NUM) {
cout << "Invalid slot." << endl;
}
else {
cout << "*** Dropping chip into slot " << numSlot << " ***" << endl;
numSlotForLoop = numSlot;
cout << "Path: [";
for (int i = 1; i <= NUM_PEGS; i++) {
cout << fixed << setprecision(ONE_DECIMAL_PLACE) << numSlotForLoop << ", ";
if(numSlotForLoop == MAX_SLOT_NUM){
numSlotForLoop = numSlotForLoop - PEG_VALUE;
}
else if(numSlotForLoop == MIN_SLOT_NUM){
numSlotForLoop = numSlotForLoop + PEG_VALUE;
}
else{
randNum = rand() % 2;
if ((randNum == 0 && numSlotForLoop > MIN_SLOT_NUM)){
numSlotForLoop = numSlotForLoop - PEG_VALUE;
}
else if ((randNum == 1 && numSlotForLoop < MAX_SLOT_NUM)){
numSlotForLoop = numSlotForLoop + PEG_VALUE;
}
}
}
cout << fixed << setprecision(ONE_DECIMAL_PLACE) << numSlotForLoop << "]" << endl;
if (numSlotForLoop == 0.0) {
winnings = SLOT_0_WINNINGS;
}
else if (numSlotForLoop == 1.0) {
winnings = SLOT_1_WINNINGS;
}
else if (numSlotForLoop == 2.0) {
winnings = SLOT_2_WINNINGS;
}
else if (numSlotForLoop == 3.0) {
winnings = SLOT_3_WINNINGS;
}
else if (numSlotForLoop == 4.0) {
winnings = SLOT_4_WINNINGS;
}
else if (numSlotForLoop == 5.0) {
winnings = SLOT_5_WINNINGS;
}
else if (numSlotForLoop == 6.0) {
winnings = SLOT_6_WINNINGS;
}
else if (numSlotForLoop == 7.0) {
winnings = SLOT_7_WINNINGS;
}
else if (numSlotForLoop == 8.0) {
winnings = SLOT_8_WINNINGS;
}
cout << fixed << setprecision(TWO_DECIMAL_PLACE) << "Winnings: $" << winnings << endl;
winnings = 0.0;
numSlotForLoop = 0.0;
}
}
else if (userMenuChoice == MENU_DROP_MULTIPLE_CHIP) {
cout << "*** Drop multiple chips ***" << endl;
cout << "How many chips do you want to drop (>0)? " << endl;
cin >> numChip;
if (numChip <= 0) {
cout << "Invalid number of chips." << endl;
}
else {
cout << "Which slot do you want to drop the chip in (0-8)? " << endl;
cin >> numSlot;
if (numSlot < MIN_SLOT_NUM || numSlot > MAX_SLOT_NUM) {
cout << "Invalid slot." << endl;
}
else {
numSlotForLoop = numSlot;
for (int j = 0; j < numChip; ++j) {
for (int i = 1; i <= NUM_PEGS; ++i) {
if(numSlotForLoop == MAX_SLOT_NUM){
numSlotForLoop = numSlotForLoop - PEG_VALUE;
}
else if(numSlotForLoop == MIN_SLOT_NUM){
numSlotForLoop = numSlotForLoop + PEG_VALUE;
}
else{
randNum = rand() % 2;
if ((randNum == 0 && numSlotForLoop > MIN_SLOT_NUM)){
numSlotForLoop = numSlotForLoop - PEG_VALUE;
}
else if ((randNum == 1 && numSlotForLoop < MAX_SLOT_NUM)){
numSlotForLoop = numSlotForLoop + PEG_VALUE;
}
}
}
if (numSlotForLoop == 0.0) {
winnings = SLOT_0_WINNINGS;
}
else if (numSlotForLoop == 1.0) {
winnings = SLOT_1_WINNINGS;
}
else if (numSlotForLoop == 2.0) {
winnings = SLOT_2_WINNINGS;
}
else if (numSlotForLoop == 3.0) {
winnings = SLOT_3_WINNINGS;
}
else if (numSlotForLoop == 4.0) {
winnings = SLOT_4_WINNINGS;
}
else if (numSlotForLoop == 5.0) {
winnings = SLOT_5_WINNINGS;
}
else if (numSlotForLoop == 6.0) {
winnings = SLOT_6_WINNINGS;
}
else if (numSlotForLoop == 7.0) {
winnings = SLOT_7_WINNINGS;
}
else if (numSlotForLoop == 8.0) {
winnings = SLOT_8_WINNINGS;
}
totalWinnings = totalWinnings + winnings;
winnings = 0.0;
numSlotForLoop = numSlot;
}
cout << fixed << setprecision(TWO_DECIMAL_PLACE) << "Total Winnings on " << numChip << " chips: $" << totalWinnings << endl;
totalAvgWinnings = totalWinnings / numChip;
cout << fixed << setprecision(TWO_DECIMAL_PLACE) << "Average winnings per chip: $" << totalAvgWinnings << endl;
totalWinnings = 0.0;
totalAvgWinnings = 0.0;
}
}
}
} while (userMenuChoice != MENU_EXIT);
cout << "Goodbye!" << endl;
//system("pause");
return 0;
}
Part 1 - Refactoring (35 points)
"Refactor" your Lab 4 code (i.e. preferably do not rewrite Lab 4 from scratch) using functions. Wikipedia explains that "Code refactoring is the process of restructuring existing computer code... Advantages include improved code readability and reduced complexity... Typically, refactoring applies a series of standardised basic micro-refactorings, each of which is (usually) a tiny change in a computer program's source code that ... preserves the behaviour of the software". In the end we really cannot tell if you refactored your lab 4 code or just re-wrote it from scratch, but we encourage you to start with your code for lab 4 and move the code around to achieve a simpler, better program. Before refactoring, you are strongly encouraged to COPY your Lab 4 code into a new solution for Lab 6 (rather than to edit your Lab 4 code directly). This way, if your Lab 6 code gets too scrambled, you still have your Lab 4 code from which to start over. In particular we are expecting to see functions for:
Computing the amount of prize money won for a single chip given the slot the chip ended up in-- this function must be defined as "double ComputeWinnings(int slot) {... your code here ...}. (See the original Plinko lab for the complete list of these values). If you have not declared and named this function exactly as specified you will no pass the initial unit tests in auto-grade which just test this function. Also, this time we want you to represent the winnings as a constant so that it would be easy to create a new Plinko board by just changing the constant. Hint: Think of using a constant array of values to represent the winnings for each slot.
Simulating one chip falling-- you will have to use your discretion on how you name and code this, but it should make both the "Drop a single chip into one slot" option and the next function (Simulating multiple chips falling) easier to write. This function must make use of the "ComputeWinnings" function described above.
Simulating multiple chips falling-- similarly this function should make both the "Drop multiple chips into one slot" option and a new "Drop multiple chips into each slot" option (described below) easier to write. This function must make use of the "Simulating one chip falling" function described above.
Your Lab 6 solution must include all the features of Lab 4.
Add a new menu item for "Drop multiple chips into each slot", but you do not have to implement it yet. You will implement it in Part 3.
We will update the error handling in Part 2, so for Part 1, you may assume that there will be no input errors.
Part 2 - Better Error Handling (30 points)
Add a function for getting the number of chips and a function for getting the slot number. Within these functions check for user input error and reprompt until the user provides valid input. You must be able to recognize erroneous input like "-1", "x", "Not even a number", and in the case of a slot number, invalid integers like "9". You may assume that there will be no real-valued inputs like "2.3". Add a function for getting the menu option also, reprompting if an erroneous option is input.
If you think about it for a minute you will realize that you could use a single function rather than three separate functions for inputting a correct integer. Obviously it would need additional parameters to work as required in each case, but it is a better approach. We encourage you to do it with just one function. More general functions like this have the advantage of being re-usable in later programs which need an integer input, rather than having to write a new function for each specific variation on inputting an integer.
Part 3 - Functions and the Power of Code Reuse (20 points)
In this part of the lab, you will take advantage of functions to add an additional menu option to your Plinko program, without having to duplicate code from the other menu options.
Add a new menu option 3 that allows the user to drop the same number of chips into each of the 9 slots. For example, if the user enters 5, you simulate dropping 5 chips into each of the 9 slots.
Prompt the user to enter one number, which is the number of chips to drop into every slot. If the number of chips is non-positive or a string, etc., reprompt until you get valid input (hint: just use the function you wrote in Part 2)
For each slot, report the total and average amount of money won for all chips dropped into that slot.
Hint: You should be able to write this new option easily by just using functions you have already created. No new functions should be necessary. Also, try this option with a large number of chips (try a million) and you will see which slots really are best on average. See if you can notice a type of "Bell curve."
Part 4 - The "Magic" of Magic Numbers (15 points)
Sometime it feels like a nuisance to make sure to properly use constants for magic numbers, so here we give you the opportunity to see how useful it can be. By just changing 3 constants, your program can simulate any Plinko board, without having to touch any other part of your code. Those constants are the number of slots, the number of rows, and the rewards arrays; just 2 ints and an array is all you have to change. Make sure that you have written your code so that by changing only those three values, and re-compilng, you can simulate an arbitrary Plinko board.
Note that when declaring constants for magic numbers they should be:
Declared at the very top of the program (globally) if the constant is meant to be used by multiple functions.
Declared at the top of the enclosing function if only used in that function.
In particular, think hard about where the 3 constants you will be updating for Part 4 should reside (where are they actually used?).
Since you will be changing constants and recompiling for Part 4, you will be submitting a different compiled program than used in Parts 1, 2, and 3. Thus, do not submit this updated program here. Instead, copy your program from here into the following zyBook section (Main Lab#6B), change the 3 constants, and submit the program from there. The grading TA will combine the points from the auto-grade of both 6A (possible 85 points) and 6B (possible 15 points) to get your full auto-grade points. The new values you will use for your plinko board are given in section Main Lab #6B.
Implementation
To make sure that the auto-grader works correctly, you must follow the same instructions from the Plinko lab 4 regarding how to use rand(), including the initial seed, etc.
Deductions graded by TA's
Adherence to the style guidelines. (up to 20 points)
Note this is the first time that the function section of the style guide comes into play, so read that section over carefully before beginning the lab. Note that one of the function style expectations is "Make sure that your naming of functions and parameters are sufficiently clear that you rarely need a comment header to describe the function and parameters."
Make sure that for Part 4, only the 3 constants specified have been changed. (5 points)
New functions for:
Computing the amount of prize money won for one chip given the slot it ended up in (called "ComputeWinnings"). (5 points)
Simulating one chip falling (must use "ComputeWinnings"). (5 points)
Simulating multiple chips falling (must use "Simulate one chip falling"). (5 points)
New functions for menu, slot and chip input, with error handling. (5 points) It could be a single function with additional parameters as noted.
Notes
The test cases start with just the input part, so you can start there and then build your solution step by part. Also, you can submit your code as many times as you like.
Sample Input
56 4 1 seven 5 2 -3 345 0 3 500 5
Sample Output
Since we are now running in Visual Studio, we can see the input and output in a single stream. The test cases are still written in terms of separate input and output, but this sample shows the operation as the program would appear running in Visual Studio.
Welcome to the Plinko simulator! Enter 4 to see options. Enter your selection now: 56 Invalid selection. Enter 4 to see options. Enter your selection now: 4 Menu: Please select one of the following options: 1 - Drop a single chip into one slot 2 - Drop multiple chips into one slot 3 - Drop multiple chips into each slot 4 - Show the options menu 5 - Quit the program Enter your selection now: 1 *** Drop a single chip *** Which slot do you want to drop the chip in (0-8)? seven Invalid slot. Which slot do you want to drop the chip in (0-8)? 5 *** Dropping chip into slot 5 *** Path: [5.0, 5.5, 5.0, 5.5, 5.0, 5.5, 6.0, 5.5, 5.0, 5.5, 6.0, 6.5, 6.0] Winnings: $1000.00 Enter your selection now: 2 *** Drop multiple chips *** How many chips do you want to drop (>0)? -3 Invalid number of chips. How many chips do you want to drop (>0)? 345 Which slot do you want to drop the chip in (0-8)? 0 Total Winnings on 345 chips: $277000.00 Average winnings per chip: $802.90 Enter your selection now: 3 *** Sequentially drop multiple chips *** How many chips do you want to drop (>0)? 500 Total Winnings on slot 0 chips: 353300.00 Average winnings per chip: 706.60 Total Winnings on slot 1 chips: 940900.00 Average winnings per chip: 1881.80 Total Winnings on slot 2 chips: 1718500.00 Average winnings per chip: 3437.00 Total Winnings on slot 3 chips: 2766100.00 Average winnings per chip: 5532.20 Total Winnings on slot 4 chips: 4180700.00 Average winnings per chip: 8361.40 Total Winnings on slot 5 chips: 5331200.00 Average winnings per chip: 10662.40 Total Winnings on slot 6 chips: 5970300.00 Average winnings per chip: 11940.60 Total Winnings on slot 7 chips: 6464000.00 Average winnings per chip: 12928.00 Total Winnings on slot 8 chips: 6838200.00 Average winnings per chip: 13676.40 Enter your selection now: 5 Goodbye!
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started