I am a biology student taking a required C++ class. As a result, I am very unfamiliar with programming but I definitely have gained a new appreciation for it. We just finished cin and cout objects two weeks ago and this is our assignment. I have some of it finished but I am having a hard time with the random generators and the output. Any help would be great.

Problem:
Create a program that prompts the user for three pieces of data:
1) number of sides on each die (between 2 and 25)
2) number of dice to roll (between 1 and 50)
3) number of rolls to check (between 10,000 and 2,000,000)

Roll the right number of dice, keep track of the resultant roll as an accumulator in an array, and repeat that for the specified number of rolls.

Write out the results with the following format: (assume 2 dice w/6 sides)

Rolled 2 dice with 6 sides, 1000000 times.
Roll %
2 2.80 ****
3 5.57 ********
4 8.33 *************
5 11.02 *******************
6 13.84 **************************
7 16.74 **********************************
8 13.86 **************************
9 11.16 *******************
10 8.33 *************
11 5.53 ********
12 2.82 ****
For full credit your program must meet the following criteria:
1) must use at least 3 user written functions, one for input, one for rolling the dice, one for output.
2) Must use an array for storing the count of dice roll
3) Must validate incoming responses from the user
4) Include required Header with IPO
5) Comments in the code
6) Use Constants for values that could change
7) Proper alignment of numbers with 2 decimal points of precision (Your numbers for 2 dice with 6 sides should be very close to the example above).

For writing out the stars, I suggest 2 or 3 stars per percent (When I wrote it I used 2.5)

#include <iostream>
#include <cstdlib>
#include <iomanip>
#include <ctime>
using namespace std;


// Prototypes to follow:
int getinput (char prompt [], int, int);
void randomdice (int, int, int);






int main() 
{ 
	srand (time ( 0 ) );

	const int LOW_SIDES = 1;		//Value for low end of sides.
	const int HIGH_SIDES = 26;		//Value for high end of sides.  Range is 2-25 inclusive.
	const int LOW_DICE = 0;			//Value for low end of dice.
	const int HIGH_DICE = 51;		//Value for high end of dice.   Range is 1-50 inclusive.
	const int LOW_ROLLS = 9999;		//Value for low end of rolls.
	const int HIGH_ROLLS = 2000001;	//Value for high end of rolls.  Range is 10,000-2,000,000 inclusive.

	int numberofsides = getinput ("How many sides on each die? ", LOW_SIDES, HIGH_SIDES);	//Calls function getinput to get # of sides on die.
	int numberofdice = getinput ("How many dice? ", LOW_DICE, HIGH_DICE);					//Calls function getinput to get # of dice.
	int numberofrolls = getinput ("How many rolls to check? ", LOW_ROLLS, HIGH_ROLLS);		//Calls function getinput to get # of rolls to check.


	randomdice (numberofsides, numberofdice, numberofrolls);	//Calls the randomdice function.
	
	
	system("pause");
	return 0; 
}   







int getinput (char prompt [], int low, int high)	//Function to get input.
{
	int input;	//Temporary variable used to hold input from function to return later.
	
	do	//Loop used to ask user for input.
	{
		cout << prompt;	//Prompts user for input.
		cin >> input;
		if (input <= low)
			cout << "Please enter a number greater than " <<  low << endl << endl;	//Input validation.
		if (input > high)
			cout << "Please enter a number less than " << high << endl;	//Input validation.
	
	}
	while (input <= low || input >= high);	//Input validation. Ensures both criteria are met.

	return input;	//Returns input back to the original function.

}
       
 

void randomdice (int numberofsides, int numberofdice, int numberofrolls)	//Header for the randomdice function.
{
	
	const int size = numberofsides * numberofdice;
	
	
	int x;
	int y;
	int sum[size] = {0};

	srand( time( 0 ) );

	for (long i = 1; i <= numberofrolls; ++i)
	{
		x = 1 + rand() % 6;
		y = 1 + rand() % 6;
		++sum[x+y];
	}

	cout << setw(10) << "Sum"  << setw(10) <<"Total" << setw(10) << "Percentage" << endl;

	for (int j = 2; j < size; ++j)
		cout << setw(10) << j << setw(10) << sum[j] << fixed << setprecision(2) << setw(9) << 100.0 * sum[j] / numberofrolls << "%" << endl;
	



}

Recommended Answers

All 5 Replies

You have given your assignment.
You have given the code you have.

But you have not said what the problem you are having is.
Is it an issue with compiler errors? Then post the errors.
Is it an issue with the program not performing as expect or desired? Then post the desired behaviour and the actual behaviour including all the inputs you gave to the program.


Finally 1 + rand() % 6; is not a good way to generate a random number because of poor randomness in the low bits of the output of some random number generators. Read this.

Since you are writing a student project on rolling dice, not solving universal chaos theory, rand() is more than adequate.

I didn't say that rand() was inadequate for this purpose, only that that wasn't a very good way of calling it with a link to better ways to use rand()

I am running into multiple issues. I am not exactly sure how to go about rolling the dice, summing them together, and how all the loops will work together for the desired output. Also, when I define size as numberofsides * numberofdice and use it in the array I get an expected constant error. Once again I am new to all of this.

I am running into multiple issues. I am not exactly sure how to go about rolling the dice,

Call rand() -- the value returned is one die roll.

summing them together,

Start with a value set to 0 as a total. Add the rolled die to it.

and how all the loops will work together for the desired output.

Think. Write the ideas on paper.
** Get one die (the computer only rolls 1 at a time).
** Roll it to the specifications of possible input
** -- 3 dice - 3 rolls;
** -- # rolls to check - 10 - repeat above 10 times. You don't want to try 10,000
** Write down each and every detail of how you are getting to an answer. When you understand that, you're ready to start coding.

Once again I am new to all of this.

Duh, ya think? :icon_wink: If you're having a hard time with simple tasks, you're new. That's OK. We all were once. No need to remind everyone.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.