Question
in c++ ***Here is my code (it needs to be re-written with the following instructions listed below)*** #include #include #include using namespace std; int main()
in c++
***Here is my code (it needs to be re-written with the following instructions listed below)***
#include
int main() { int i; int k; int j; int userNumChips = 0; int userInput = 0; double originalUserSlot = 0.0; double userSlot = 0.0; double coinMove = 0.0; double userWinnings = 0.0; double averageWinnings = 0.0; const int TOTAL_NUM_SLOTS = 8; const int MIN_NUM_SLOTS = 0; const int DROP_LENGTH = 11; const int RIGHT_OR_LEFT = 2; const int DROP_SINGLE = 1; const int DROP_MULT = 2; const int OPTIONS_MENU = 3; const int QUIT_PROGRAM = 4; const int RAND_SEED = 42; srand(RAND_SEED); const double MOVE_LEFT = -0.5; const double MOVE_RIGHT = 0.5; const double NO_WINNING = 0.0; const double LOW_WINNING = 100.0; const double MID_WINNING = 500.0; const double HIGH_WINNING = 1000.0; const double JACKPOT_WINNING = 10000.0; const double MAX_LEFT_DEFAULT = 0.5; const double MAX_RIGHT_DEFAULT = 7.5; cout << "Welcome to the Plinko simulator! Enter 3 to see options. " << endl << endl;
while (userInput != QUIT_PROGRAM) { cout << "Enter your selection now : " << endl << endl; cin >> userInput;
if (userInput == DROP_SINGLE) { cout << "*** Drop a single chip ***" << endl << endl; cout << "Which slot do you want to drop the chip in (0-8)?" << endl << endl; cin >> userSlot;
if (userSlot >= MIN_NUM_SLOTS && userSlot <= TOTAL_NUM_SLOTS) { cout << "*** Dropping chip into slot " << static_cast
cout << "Path: [" << setprecision(1) << fixed << userSlot;
for (i = 0; i <= DROP_LENGTH; ++i) {
cout << ", "; if (userSlot == MIN_NUM_SLOTS) { userSlot = MAX_LEFT_DEFAULT; cout << MAX_LEFT_DEFAULT; } else if (userSlot == TOTAL_NUM_SLOTS) { userSlot = MAX_RIGHT_DEFAULT; cout << MAX_RIGHT_DEFAULT; } else { coinMove = rand() % RIGHT_OR_LEFT; if (coinMove == MIN_NUM_SLOTS) { userSlot += MOVE_LEFT; } else { userSlot += MOVE_RIGHT; } cout << userSlot; } } cout << "]" << endl;
cout << "Winnings: $" << setprecision(2) << fixed; if ((userSlot > -0.5 && userSlot < 0.5) || (userSlot > 7.5 && userSlot < 8.5)) { cout << LOW_WINNING; } else if ((userSlot > 6.5 && userSlot < 7.5) || (userSlot > 0.5 && userSlot < 1.5)) { cout << MID_WINNING; } else if ((userSlot > 5.5 && userSlot < 6.5) || (userSlot > 1.5 && userSlot < 2.5)) { cout << HIGH_WINNING; } else if ((userSlot > 4.5 && userSlot < 5.5) || (userSlot > 2.5 && userSlot < 3.5)) { cout << NO_WINNING; } else { cout << JACKPOT_WINNING; } cout << endl << endl; } else { cout << "Invalid slot." << endl << endl; }
} else if (userInput == DROP_MULT) { cout << "*** Drop multiple chips ***" << endl << endl; cout << "How many chips do you want to drop (>0)?" << endl << endl; cin >> userNumChips; userWinnings = 0;
if (userNumChips > 0) { cout << "Which slot do you want to drop the chip in (0-8)?" << endl << endl; cin >> originalUserSlot; for (k = 0; k < userNumChips; ++k) { userSlot = originalUserSlot; if (userSlot >= MIN_NUM_SLOTS && userSlot <= TOTAL_NUM_SLOTS) { for (j = 0; j <= DROP_LENGTH; ++j) {
if (userSlot == MIN_NUM_SLOTS) { userSlot = MAX_LEFT_DEFAULT; } else if (userSlot == TOTAL_NUM_SLOTS) { userSlot = MAX_RIGHT_DEFAULT; } else { coinMove = rand() % RIGHT_OR_LEFT; if (coinMove == MIN_NUM_SLOTS) { userSlot += MOVE_LEFT; } else { userSlot += MOVE_RIGHT; } } } } else { cout << "Invalid slot." << endl << endl; break; } if ((userSlot > -0.5 && userSlot < 0.5) || (userSlot > 7.5 && userSlot < 8.5)) { userWinnings += LOW_WINNING; } else if ((userSlot > 6.5 && userSlot < 7.5) || (userSlot > 0.5 && userSlot < 1.5)) { userWinnings += MID_WINNING; } else if ((userSlot > 5.5 && userSlot < 6.5) || (userSlot > 1.5 && userSlot < 2.5)) { userWinnings += HIGH_WINNING; } else if ((userSlot > 4.5 && userSlot < 5.5) || (userSlot > 2.5 && userSlot < 3.5)) { userWinnings += NO_WINNING; } else { userWinnings += JACKPOT_WINNING; } if (k == (userNumChips - 1)) { cout << "Total winnings on " << userNumChips << " chips: $" << setprecision(2) << fixed << userWinnings << endl; averageWinnings = userWinnings / userNumChips; cout << "Average winnings per chip: $" << setprecision(2) << fixed << averageWinnings << endl << endl; } } } else { cout << "Invalid number of chips." << endl << endl; }
} else if (userInput == OPTIONS_MENU) { cout << "Menu: Please select one of the following options : " << endl << 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 << endl; } else if (userInput == QUIT_PROGRAM) { break; } else { cout << "Invalid selection. Enter 3 to see options." << endl << endl; } } cout << "Goodbye!" << endl;
//system("pause"); return 0; }
***Here are the instructions***
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
Review Lab #4 to remind yourself of implementation details. For example, following is part of the lab #4 notes. You must set the random seed in the main function. Normally we would use "srand(time(0))" to get (more nearly) random behavior. However, in this lab you must use srand(42) so that you get the same random stream each run for matching the test cases. Use a constant for the 42 (no magic numbers) and put the statement after your declarations and just when you start the main section of your code. Note also that you may get different random number streams on different systems. Thus your random stream, even given a constant seed (e.g. 42), may differ when developing on VS versus passing off on zyBooks.
Deductions graded by TA's
Adherence to the style guidelines. (up to 20 points)
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 (code should be able match this 100% if the input above is used)
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