I am trying to figure out how to create a program consisting of a player tossing two dices, then the value of the dices are added up, and then I must determine if the number is considered a prime number. The sequence must be done 500 times.
I must test each integer I receive from the dice between 1 and sqrt(n) using modulus. If the number is prime, I return 1, otherwise, return 0. I must count every time a number is prime until the dice has rolled its 500th time. If the number is prime, the player wins and it gets counted in the program.
(sqrt(n) is the value of the dice rolls)
This is what I have so far:
Code:
#include <iostream>
#include <stdlib.h>
#include <math.h>
using namespace std;
//This program is to determine how many times a player wins by rolling two dice adding up to a prime number 500 times
main()
{
int n = 0, die1 = 0, die2 = 0, roll_count = 0, playerwin = 0, i;
while (roll_count <= 500)//Number of times dice will roll
{
srand(time(NULL));
die1 = rand()%6+1; //randomizing the first die
srand(time(NULL));
die2 = rand()%6+1; //randomizing the second die
n = die1 + die2; //Adding the randomized values together
for (i = 2;i <= sqrt(n); i++) //i is the integer being tested on n
{
if (n%i==0) //if they get even, playerwin gets added 1 and return 1
{
playerwin++;
return 1;
}
if (i == 11) //If integer exceeds 11 which eliminates any possible
even divisible between 2-11, it is considered not prime
and returns 0
{
return 0;
}
}
roll_count++; //After the sequence is done, roll_count gets counted
the and loop starts again
}
cout << "Number of times player has won is: " << playerwin << endl; // Counted playerwin will be displayed
return 0;
}
I'm fairly new to C++ and for this program, the output is blank. I am in need of help. Thank you.