Question
6.23 Main Lab #6A - Plinko With Functions Background You may have felt some frustration in writing your solution for the Plinko lab because there
6.23 Main Lab #6A - Plinko With Functions
Background
You may have felt some frustration in writing your solution for the Plinko lab because there were parts of your code that you had to duplicate in two or three places. Functions allow us to remove this duplication. More importantly, functions allow us to actually give our program a manageable structure; breaking up a large problem into several smaller problems.
For this lab we will re-write the Plinko lab using functions and add a few new features. Note also that most programming in the real world is the modification of existing programs rather than writing entirely new programs. In this lab you will also have opportunity to get experience with that.
Functions are also useful for dealing with errors, such as user input errors. As explained in the style guidelines, in general it is best to deal with errors as soon as possible. In this lab you will make functions for user input which check for user input errors and re-prompt as needed.
Requirements
This lab is to be done individually; not with pair programming.
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 slotNum) { 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.
-
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, we suggest you 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 Avoiding Magic Numbers (15 points)
Sometimes it may 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 the 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. 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.
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)? 10000 Total Winnings on 10000 chips in slot 0: $7939200.00 Average winnings per chip: $793.92 Total Winnings on 10000 chips in slot 1: $10032800.00 Average winnings per chip: $1003.28 Total Winnings on 10000 chips in slot 2: $15849700.00 Average winnings per chip: $1584.97 Total Winnings on 10000 chips in slot 3: $23028300.00 Average winnings per chip: $2302.83 Total Winnings on 10000 chips in slot 4: $24754600.00 Average winnings per chip: $2475.46 Total Winnings on 10000 chips in slot 5: $22953000.00 Average winnings per chip: $2295.30 Total Winnings on 10000 chips in slot 6: $15925600.00 Average winnings per chip: $1592.56 Total Winnings on 10000 chips in slot 7: $10133800.00 Average winnings per chip: $1013.38 Total Winnings on 10000 chips in slot 8: $7973300.00 Average winnings per chip: $797.33 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