Simple C code Writing two functions to simulate rolling a di
Simple C++ code: Writing two functions to simulate rolling a dice
Problem 3. Write a program to simulate the roll of a six-faced die. You\'ll need to write two functions for this. The first function (let\'s call it Function A) calculates the value of the roll (1 through 6) and for this. The first function (let\'s call it Function A) calculates the value of the roll through 6) and returns the value. You\'ll need to implement randomization in this function. The second function (let\'s call it Function B) implements the following logic i) If the sum of two die rolls is greater than 8, the player wins ii) Ifthe sum of two die rolls is less than 6, the player loses.Solution
#include <iostream>
#include <stdlib.h>
#include <time.h>
using namespace std;
int Function_A(); /*Function A for random number generation*/
int Function_B(); /*Function B for implementation of logic*/
int main()
{
srand (time(NULL));
int status;
/*initially we have define a integer status, if status equal to 1 then player won the match and
if status equal to 0 then player lost the match */
status=Function_B(); /*calling function B, we get the status*/
if(status==1)
{
cout << \"Player has WON the Match \" << endl;
}
else
{
cout << \"Player has LOST the Match \" << endl;
}
return 0;
}//end of main funcation
/*Function B definition */
int Function_B()
{
int sum=0;
int value_of_roll_1 = Function_A();/* calling function A for die roll 1*/
int value_of_roll_2 = Function_A();/* calling function A for die roll 2*/
sum=value_of_roll_1+ value_of_roll_2;
//cout<<\"sum of two die roll is :\"<<sum<<endl;
/* if sum of two die roll is greater than 8 then player wins match*/
if(sum>8)
{
return 1; /* player wins the match so return 1*/
}
/* if sum of two die roll is less than 6 then player loss match*/
if(sum<6)
{
return 0; /* player loss the match so return 0*/
}
/* if sum of two die roll equal to 6,7, or 8 then player get roll again*/
if(sum==6 || sum==7 || sum==8)
{
Function_B(); //calling function again
}
}
/*Function A definition */
int Function_A()
{
/* random number generation between 1 to 6*/
int rand_num=(1+(rand() % 6));
return rand_num;
}
---------------------------------------------------------------------------------------------
If you have any query, please feel free to ask
Thanks a lot

