This code is from my main function. In a nutshell, I want the function multchip_slot to evaluate for the integers 0 - 8.

However, I don't know how to get the function to evaluate for different values without duplicating the function for each new input.

Although the way I've done executes without errors, I feel there must be a simpler way to do it.

Do you know a way that I can have my function evaluate for values 0-8 inclusive without having to rewrite the function?

 ``123456789101112131415161718192021222324`` ``````int main() { cout << "Enter a number of chips: "; double num_chips; cin >> num_chips; // if user enters 5, drop 5 chips to every slot between 0 - 8 double num_chip_saver = num_chips; // preserves the initial entered value of chips to be used as a divisor in computing average chips double a = multchip_slot(num_chips, 0); double b = multchip_slot(num_chips, 1); double c = multchip_slot(num_chips, 2); double d = multchip_slot(num_chips, 3); double e = multchip_slot(num_chips, 4); double f = multchip_slot(num_chips, 5); double g = multchip_slot(num_chips, 6); double h = multchip_slot(num_chips, 7); double i = multchip_slot(num_chips, 8); double j = a + b + c + d + e + f + g + h + i; // sums the total points earned from each of the slots cout << "Total points: " << j << endl; cout << "Average points: " << j / num_chip_saver << endl; // computes the average points earned per drop ``````

Additional info: The function returns "Total Points." My assignment is to create a program that enables users to drop multiple chips into all nine slots of a Plinko board simultaneously. In other words, if a user enters "5" as their number of chips (num_chips) value, it drops 5 chips into each of the nine slots, labeled 0-8. My assignment is to compute the total and average points earned from this.
One word for you.... Loop.

Let's use a container to store all the points values and use a loop to load it up.

Starting off simple, we'll use an array.

 ``123456`` ``````const int NUM_TURNS = 8; double pointsArray[NUM_TURNS]; for (int i = 0; i < NUM_TURNS; i++) pointsArray[i] = multchip_slot(num_chips, i);``````

This should in theory, give you an array containing all the points values.

To get their sum you need to once again, use a loop.

 ``1234`` ``````double totalPoints = 0; for (int i = 0; i < NUM_TURNS; i++) totalPoints = pointsArray[i];``````

If you want to save the extra loop you can add the sum while you load up the container.

Hope this helps.
Last edited on