"The best way to cheer yourself up is to try to cheer somebody else up." Mark Twain

Sunday, April 18, 2010

Write a program that simulates the rolling of two dices. The program should use rand to roll the first dice, and should read rand again to roll the second dice. The sum of the two values should then be calculated. Note: Since each dice can show an integer from 1 to 6 , then 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. Your program should roll the two dice 100 times. Use a one-dimensional array to tally the number of times each possible sum appears. Print the results in tabular format.


#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
#include<time.h>

const int Limit = 6;
void main()
{
int n1, n2, res;
int arr[100];

cout << "\n *** Roll 2 Dices 100 times Each & Check the sums *** \n\n ";
cout << "\n ******* Check these out ********* \n\n ";

for(int n=0; n< 100 ; n++)
{
cout << "\n YOUR TURN No. = " << n+1 << "\n";
// roll the first dice
cout << "\n Roll the first Dice ";
randomize();
n1 = random(Limit);


// roll the second dice
cout<< "\n Roll the sec dice :";
randomize();
n2 = random(Limit);


// sum of the drawn result
res = n1 + n2;

// store the sum in an array
arr[n] = res;
}

cout << "Now ,Guess the sum of the result of the rolling two dices~ "
//  Array for Guess No.
int g[100];
for( n=0 ; n< 100; n++)
{
cout<< "\n Predict The Sum For Turn : " << n+1;
cout << " Enter your number ( between 2 - 12) :";
cin >> g[n];
}
cout << " \n If the guess number and actual number matches You WON ";
cout<< "\n ***** TABLE *****\n";
cout << "\n TURN \t SUM  \t GuessNo. \t RESULT?  \n";

// Print the result in tabular format
for( n=0 ; n< 100; n++)
{

// if guess no is equal to the sum , then WON , else LOST
if(g[n] == arr[n])
cout << "\n " << n+1 << "\t" << arr[n] << "\t" << g[n] << "\t" << "WON by guessing";
else
cout << "\n " << n+1 << "\t" << arr[n] << g[n]  << "LOST by guessing";

// print 15 to 20 records at a time

if ( n == 15 || n == 35 || n == 55 || n == 75 )
{
cout << "\n Press any key to continue :";
getch();
clrscr();
}
}
getch();
}

No comments:

Post a Comment

C makes it easy to shoot yourself in the foot; C++ makes it harder, but when you do it blows your whole leg off.
Now Playing: Ballade Pour Adeline

About Me

My photo
I m an IT lecturer of a college. I love social-work. I want to do something beneficial for society before dying , that can promote our society, to some extent.