Hello, Everyone:
My friend and I were doing a C++ program which relates to dice. Here is the problem:
Write a program that simulates the rolling of two dice. The program should use rand to roll the first die and should use rand again to roll the second die. The sum of the two values should then be calculated. [Note: Each die can show an integer value from 1 to 6, so the sum of the two values will vary from 2 to 12, with 7 being the most frequent sum and 2 and 12 being the least frequent sums.] Figure 7.32 shows the 36 possible combinations of the two dice. Your program should roll the two dice 36,000 times. Use a one-dimensional array to tally the numbers of times each possible sum appears. Print the results in a tabular format. Also, determine if the totals are reasonable.
Here is the code:
Code:
// Include files
#include <iostream> // used for cin, cout
#include <conio.h>
#include <iomanip>
#include <cstdlib>
#include <ctime>
using namespace std;
// Global Type Declarations
// Function Prototypes
void instruct (void);
void pause ();
//Global Variables - should not be used without good reason.
int main ()
{
// Declaration section
const int arraySize = 13;
int die1,
die2,
frequency[ arraySize ] = { 0 };
// Executable section
instruct ();
srand( time( 0 ) );
//Roll Dice 36000 times store result in frequency array
for ( int roll = 0; roll < 36000; ++roll ) {
die1 = 1 + rand() % 6;
die2 = 1 + rand() % 6;
++frequency[ die1 + die2 ];
}
//Column Headers
cout << setw( 10 ) << "Dice Combination"
<< setw( 17 ) << "Frequency\n\n";
//Start at Element 2 To Skip Elements 0 and Element 1
for ( int combo = 2; combo < arraySize; ++combo )
cout << setw( 9 ) << combo << setw( 18 ) << frequency[ combo ]
<< endl;
pause ();
return 0;
}
void instruct (void)
{
// Declaration section
cout << "This program will simulate the rolling of two dice 36000 times. "
<< "It will then\nprint out the results of how many times each "
<< "combination of the two dice\nappeared. You should see in the "
<< "results that the 7 combination will appear\nthe most with the 2 "
<< "combination and the 12 combination appearing the least.\n"
<< "_______________________________________________________________"
<< "_____________"
<< "\n" << endl;
// Executable section
}
void pause ()
{
// Declaration section
// Executable section
cout << "\nPress any key to continue...";
getch();
cout << "\r";
cout << " ";
cout << "\r";
}
However, the g++ compiler says that <conio.h> does not exist:
Code:
dice.cpp:3: conio.h: No such file or directory
I understand <conio.h> is used in C not in C++, however, I need help finding out what <conio.h> means in C++. Please someone help us figure out the problem and what does conio.h means in C++. I will appreciate any help. Thank you.