Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

6.22 Main Lab #6A - Plinko With Functions (C++) Background You may have felt some frustration in writing your solution for the Plinko lab because

6.22 Main Lab #6A - Plinko With Functions (C++)

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 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.

Sample Input

56 4 1 seven 5 2 -3 345 0 3 500 5 

Sample Output

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

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Data Management Databases And Organizations

Authors: Richard T. Watson

6th Edition

1943153035, 978-1943153039

More Books

Students also viewed these Databases questions

Question

List and describe three behavioral leadership theories.

Answered: 1 week ago