Can someone help me refactor my original code to *include functions*. It's a Plinko simulator. I am a beginner, pretty awful at coding, and do not enjoy it so much. I understand the basics of C++ (and the basics of functions), and for the most part can describe what a compiler is doing throughout a code, but when it comes time for myself to construct a much bigger program it is too much. Any help or advice would be GREATLY appreciated.
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.
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
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
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., re-prompt 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."
I am a beginner, pretty awful at coding, and do not enjoy it so much. I understand the basics of C++ (and the basics of functions), and for the most part can describe what a compiler is doing throughout a code, but when it comes time for myself to construct a much bigger program it is too much.
Watch what you say. This tends to imply that you are not interested and want someone to do the work for you. There are many good people here that are willing to help you understand what you did wrong, but few if any willing to do your homework for you.
Part 1 - Refactoring
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 not 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.
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.
Start with lines 3 and 5 first. It tells you everything you need to know to get started. Then look at the code you have and see what part(s) match what the function is to do. Hint any code repeated more than once is good candidate for a function.
Lines 11 and 12 along with line 14 go together part of which you have already done.
Everything is the same, but I called the function ComputeWinnings and executed as you can see below. I think next I need to call a function for dropping one chip into one slot, multiple chips into one slot, and multiple chips into each slot. Is the way to do this similar? Should I wait to call the function for "multiple chips into each slot"?
The "ComputeWinnings" function looks good. That is one way I envisioned this function. What I would do different is starting at line 6 I would define any variable that begins with "const". This way you will not be redefining these variables in each function you write. One of the few times global variables are useful because they can not be changed when thay are "const".
On line 142 you call the "ComputeWinnings" function which is good, but at line 185 you retype the switch when you need to call the function. This section is why the function was written.
Next work on taking "drop one chip" and put that into a function. This will be very similar to what you have done. I will have to work on this one to see if there is anything extra needed.
When I started working on your new code I put the menu into a function and changed the beginning of main this way:
1 2 3 4 5 6 7 8
srand(RAND_SEED);
//cout << "Welcome to the Plinko simulator! Enter 3 to see options." << endl << endl;
do {
Menu();
cout << "Enter your selection now: ";
"Menu" is a simple void function that takes no parameters. This works better because when you return to the beginning of the do/while loop the first thing you get is the menu. As you have it the first time the program runs it works. After that the do/while loop only ask the user to enter choice. But what choice? This way the user will know what to enter. And the menu options need to be changed. Option 3 is no longer needed.