I am fairly new to C++ programming and have been working on an assignment for my C++ class that involves writing a dice rolling program (rolls 2 dice). The program is to get a random seed number and a desired number of rolls from the user, keep track of the rolls of each point value, and then display actual and expected percentages and number of rolls for each point value. We are also to use functions to complete this program.

I am having trouble with a couple of things: first, using a function and/or loop to keep track of rolls to display them at the end of the program. I think I should use a static int variable but I am not sure how to initiate this using functions (which function to put the variable in?).

Secondly, I am having difficulty with using a loop to display the final results. Since the expected value is different for each point value, I can't use a constant to do the calculation.... not sure how else to do this (currently I just have some variables I've put in for filler to help me see how the output will look like when it's done).

Also, I can't figure out how to pass the value of repetitions to the ShowValue function without using it as a global variable... I have tried looking this up (call by value/reference) but seem to be having difficulty transferring the concepts to my own code.

Any suggestions would be greatly appreciated.

Here is my code so far:

#include <iostream>
#include <iomanip>

using namespace std;

// function prototypes
void DisplayHeader();
void ShowOutputHeader();
void ShowValue(int);
void NumberGenerator(static int);

// GLOBAL VARIABLE to carry over to calculations
// in multiple functions
int repetitions;

int main()
{
	// array to hold point values
	int pointValue[11] = { 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 };

	// seed is PRN & first is user selected.
	
	// function calls to display what the program does and
	// to call the PRNG calculations
	static int countRolls;
	countRolls++;

	DisplayHeader();
	NumberGenerator(countRolls);

	// display final outcomes, expected and actual	
	ShowOutputHeader();

	for(int index = 0; index <= 10; index++)
	{
		ShowValue(pointValue[index]);
         }
	system("pause");

	return 0; // indicates successful completion
}

void DisplayHeader()
{
	// output user prompts
	cout << "Dice Rolling Simulation" << endl << endl;
	cout << "This program allows you to simulate the rolling of a pair of dice."<< endl << endl;
}

void ShowOutputHeader()
{
	cout << "Final Statistics:" << endl;
	cout << endl;
	cout << "Point" << setw(15) << "Actual" << setw(15) << "Acutal"; 
	cout << setw(15) << "Expected" << setw(15) << "Expected" << endl;
	cout << "value" << setw(15) << "rolls" << setw(15) << "percentage";
	cout << setw(15) << "rolls" << setw(15) << "percentage" << endl;
	cout << endl; 
}

void ShowValue(int pointValue)
{
	cout << pointValue << setw(15) << pointValue << setw(15) << setprecision(2);
	cout << (pointValue / float(repetitions) * 100) << "%" << setw(15);
	cout << int(int(repetitions) * (0.056));
	cout << setw(15) << setprecision(2) << 0.056 * 100 << "%" << endl;
}

void NumberGenerator(static int countRolls)
{
	// declare operators to generate pseudo-random number.
	unsigned long modulus = 2147483647;	
	unsigned long multiplier = 16807, increment = 0;
	int seed;
	
	// the random number, between 0 & 1.
	float num1;
	float num2;

	// number of reps, diceroll, dice
	int roll, dice1, dice2;
	
	// prompts that require user input
	cout << "Enter seed: ";
	cin >> seed;
	cout << "How many rolls would you like to generate? ";
	cin >> repetitions;
	cout << endl;
	cout << "Generating rolls..." << endl;

	for (int i = 0; i < repetitions; i++)
	{
		seed = ((multiplier * seed + increment) % modulus);
		// num1 is 0-1
		num1 = seed / float(modulus);

		seed = ((multiplier * seed + increment) % modulus);
		// num2 is 0-1
		num2 = seed / float(modulus);

		// gives 0-5, + 1 = 1-6
		dice1 = (int(num1*6) + 1);
		dice2 = (int(num2*6) + 1);

		// roll the two dice and add the numbers together.
		roll = (dice1 + dice2);

		// output the roll
		cout << "Roll was: " << roll << endl;
	}
}

Whoops, forgot to add that I'm not supposed to use RAND, but the full pseudo-random number generator code that I included.

I am having trouble with a couple of things: first, using a function and/or loop to keep track of rolls to display them at the end of the program. I think I should use a static int variable but I am not sure how to initiate this using functions (which function to put the variable in?).

Why? What does static do for you?
If you want to "keep track of rolls", you need to keep track of them. All you are doing is generating a roll, outputting it, and throwing it away. What do you need to use to 'remember' each roll?

Secondly, I am having difficulty with using a loop to display the final results. Since the expected value is different for each point value, I can't use a constant to do the calculation.... not sure how else to do this (currently I just have some variables I've put in for filler to help me see how the output will look like when it's done).

What calculation? From the description you don't need to calculate much of anything except the expected information. What algorithm do you have for calculating the expected values?

Also, I can't figure out how to pass the value of repetitions to the ShowValue function without using it as a global variable... I have tried looking this up (call by value/reference) but seem to be having difficulty transferring the concepts to my own code.

Pass it as a parameter to the function. What trouble are you having?

This article has been dead for over six months. Start a new discussion instead.