Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Right now I'm having trouble with a few things. First, if you input a string or character in the enter selection place it prints out

Right now I'm having trouble with a few things. First, if you input a string or character in the enter selection place it prints out the invalid line repeatedly with no end. I also only need to print out the plinko path when dropping a single chip in only one slot, but its printing for all options. It also outputs the wrong values when dropping multiple chips. There may be other issues, but right now I can't get past these issues. I know there are other answers to a very similar lab, but the solution uses some #includes that I have never used before and I don't think it would be wise to use for this lab. Here's the instructions, if you could help me finish the lab that would be amazing! Thank you!

Instructions:

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.

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

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! 

This is my current code:

// Plinko.cpp : Defines the entry point for the console application.

//

Description: Outputs the winnings of dropping a chip on a plinko board.

We completed this lab together in visual studios.

We completed the entire lab using pair programming.

Late Days: None

Test Case 1:

Input: (4)

Expected Output: (Goodbye!)

Result: Success

Test Case 2:

Input: (1, 1, 4)

Expected Output: ($1000, Goodbye!)

Result: Success

Test Case 3:

Input: (2, 9, 11, 2, 9, 7, 4)

Expected Output: ($4300, $477.78, Goodbye!)

Result: Success

*/

//#include "stdafx.h"

#include

#include

using namespace std;

const int LOWEST_SLOT_NUMBER = 0;

const int HIGHEST_SLOT_NUMBER = 8;

double ComputeWinnings(int slotNumber) {

const int NO_PRIZE = 0;

const int SMALL_PRIZE = 100;

const int MEDIUM_PRIZE = 500;

const int LARGE_PRIZE = 1000;

const int GRAND_PRIZE = 10000;

double prizeValue = 0.0;

if (slotNumber == 0 || slotNumber == 8) {

prizeValue = SMALL_PRIZE;

}

else if (slotNumber == 1 || slotNumber == 7) {

prizeValue = MEDIUM_PRIZE;

}

else if (slotNumber == 2 || slotNumber == 6) {

prizeValue = LARGE_PRIZE;

}

else if (slotNumber == 3 || slotNumber == 5) {

prizeValue = NO_PRIZE;

}

else if (slotNumber == 4) {

prizeValue = GRAND_PRIZE;

}

return prizeValue;

}

double DropSingleChip(double slotNumber) {

const int LOWEST_SLOT_NUMBER = 0;

const int HIGHEST_SLOT_NUMBER = 8;

const int PATH_LENGTH = 12;

const int PRECISION = 1;

const int INT_PRECISION = 0;

const double PATH_CHANGE = 0.5;

const int RANDOM_NUMBER_WIDTH = 2;

const int DOLLAR_PRECISION = 2;

double prizeValue = 0.0;

for (int i = 0; i < PATH_LENGTH; ++i) {

if (slotNumber == LOWEST_SLOT_NUMBER) {

slotNumber += PATH_CHANGE;

}

else if (slotNumber == HIGHEST_SLOT_NUMBER) {

slotNumber -= PATH_CHANGE;

}

else if (rand() % RANDOM_NUMBER_WIDTH == 0) {

slotNumber -= PATH_CHANGE;

}

else {

slotNumber += PATH_CHANGE;

}

cout << slotNumber;

if (i < PATH_LENGTH - 1) {

cout << ", ";

}

else {

cout << "]" << endl;

}

prizeValue = ComputeWinnings(slotNumber);

}

return prizeValue;

}

double DropMultipleChips(double slotNumber, int numChips) {

const int LOWEST_SLOT_NUMBER = 0;

const int HIGHEST_SLOT_NUMBER = 8;

const int DOLLAR_PRECISION = 2;

double prizeValue = 0.0;

if (slotNumber >= LOWEST_SLOT_NUMBER && slotNumber <= HIGHEST_SLOT_NUMBER) {

for (int j = 0; j < numChips; ++j) {

prizeValue += DropSingleChip(slotNumber);

prizeValue += ComputeWinnings(slotNumber);

}

}

else {

cout << "Invalid slot." << endl << endl;

}

return prizeValue;

}

double GetSlotNum(double slotNumber) {

bool validSlot = false;

while (validSlot == false) {

cout << endl;

if (slotNumber >= LOWEST_SLOT_NUMBER || slotNumber <= HIGHEST_SLOT_NUMBER) {

return slotNumber;

}

else {

cout << "Invalid slot." << endl;

}

}

}

int GetNumChips() {

int numChips;

bool validChipNum = false;

while (validChipNum == false) {

cout << "How many chips do you want to drop (>0)? ";

cin >> numChips;

cout << endl;

if (numChips > 0) {

return numChips;

}

}

}

void GetMenu() {

cout << "Menu: Please select one of the following options: " << endl << endl <<

"1 - Drop a single chip into one slot" << endl <<

"2 - Drop multiple chips into one slot" << endl <<

"3 - Drop multiple chips into each slot" << endl <<

"4 - Show the options menu" << endl <<

"5 - Quit the program" << endl << endl;

}

int main() {

const int RANDOM_SEED = 42;

const int SINGLE_CHIP = 1;

const int MULTIPLE_CHIPS = 2;

const int MULTIPLE_SLOTS = 3;

const int OPTIONS_MENU = 4;

const int PROGRAM_QUIT = 5;

const int PATH_LENGTH = 12;

const int PRECISION = 1;

const int DOLLAR_PRECISION = 2;

const int INT_PRECISION = 0;

const double PATH_CHANGE = 0.5;

const int RANDOM_NUMBER_WIDTH = 2;

int userInput = 0;

double slotNumber = 0.0;

int numChips = 0;

int randomNumber = 0;

double prizeValue = 0.0;

double tempSlotNumber = 0.0;

srand(RANDOM_SEED);

cout << "Welcome to the Plinko simulator! Enter 4 to see options." << endl;

do {

cout << endl << "Enter your selection now: " << endl;

cin >> userInput;

cin.ignore();

if (userInput == SINGLE_CHIP) {

cout << endl << "*** Drop a single chip ***" << endl;

cout << "Which slot do you want to drop the chip in (0-8)?";

cin >> slotNumber;

slotNumber = GetSlotNum(slotNumber);

if (slotNumber >= LOWEST_SLOT_NUMBER && slotNumber <= HIGHEST_SLOT_NUMBER) {

cout << "*** Dropping chip into slot " << fixed << setprecision(INT_PRECISION) << slotNumber << " ***" << endl;

cout << fixed << setprecision(PRECISION) << "Path: [" << slotNumber << ", ";

prizeValue = DropSingleChip(slotNumber);

cout << "Winnings: $" << fixed << setprecision(DOLLAR_PRECISION) << prizeValue << endl;

prizeValue = 0;

}

else {

cout << "Invalid slot." << endl << endl;

}

}

else if (userInput == MULTIPLE_CHIPS) {

cout << endl << "*** Drop multiple chips ***" << endl;

numChips = GetNumChips();

if (numChips > 0) {

cout << "Which slot do you want to drop the chip in (0-8)?";

cin >> slotNumber;

slotNumber = GetSlotNum(slotNumber);

prizeValue = DropMultipleChips(slotNumber, numChips);

cout << "Total winnings on " << numChips << " chips: $" << fixed << setprecision(DOLLAR_PRECISION) << prizeValue << endl;

cout << "Average winnings per chip: $" << (prizeValue / numChips) << endl << endl;

prizeValue = 0;

}

else {

cout << "Invalid number of chips." << endl << endl;

}

}

else if (userInput == MULTIPLE_SLOTS) {

cout << endl << "*** Sequentially drop multiple chips ***" << endl;

numChips = GetNumChips();

for (int i = 0; i <= HIGHEST_SLOT_NUMBER; ++i) {

slotNumber = i;

slotNumber = GetSlotNum(slotNumber);

prizeValue = DropMultipleChips(slotNumber, numChips);

cout << "Total Winnings on slot " << i << " chips: " << fixed << setprecision(DOLLAR_PRECISION) << prizeValue << endl;

cout << "Average winnings per chip: " << (prizeValue / numChips) << endl << endl;

}

}

else if (userInput == OPTIONS_MENU) {

GetMenu();

}

else if (userInput != PROGRAM_QUIT) {

cout << "Invalid selection. Enter 4 to see options." << endl << endl;

}

else if (cin.fail()) {

cin.clear();

string ignore;

cin.ignore();

}

} while (userInput != PROGRAM_QUIT);

cout << "Goodbye!" << endl;

system("pause");

return 0;

}

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_2

Step: 3

blur-text-image_3

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

Linked Data A Geographic Perspective

Authors: Glen Hart, Catherine Dolbear

1st Edition

1000218910, 9781000218916

More Books

Students also viewed these Databases questions

Question

Identify and define the eight channels of nonverbal communication

Answered: 1 week ago