"The best way to cheer yourself up is to try to cheer somebody else up." Mark Twain
Showing posts with label rand() func. Show all posts
Showing posts with label rand() func. Show all posts

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

Wednesday, April 7, 2010

Computers are playing an increasing role in education. Write a program that will help elementary school students to learn multiplication. Use rand to produce two positive one-digit integers. Your program should ask a question such as : 'How much is 6 times 7 ?' The student then type the answer. Your program checks the student's answer. If correct, print "Very Good!" and then ask another question. If the answer is wrong, print "No, Please Try Again." And then let the student try the same problem again until correct response is received.


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

const int Limit = 9;

void main()
{
int n1, n2, pro = 1, ans = 0 ;

char ch = 'y';

cout << "\n ******* LEARN MULTIPLICATION ********* \n\n ";

while(ch=='y' || ch == 'Y')
{

randomize();
n1 = random(Limit);
randomize();
n2 = random(Limit);
pro = n1 * n2;

while(pro != ans )
{
cout << "\n How much is " << n1 << "times" << n2 << "?" ;
cout << "\n Enter your answer : ";
cin >> ans;
if(pro == ans )
     cout << "\n Very Good!!!!";
else
      cout<< "No, Please Try again .";
}
pro = 1;
ans = 0;
cout << " Do you continue (Y/N)? ";
cin >> ch;

}

getch();
}
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.