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;
}