May 9, 2013 at 5:28am May 9, 2013 at 5:28am UTC
you are assigning values to y.(by doing y=2 and y=1).you need to check like this: y==2
May 9, 2013 at 5:30am May 9, 2013 at 5:30am UTC
I just made few changes:
you also need g and z to be of float types or the percentage would be calculated as zero(cause they are int)
#include<ctime>
#include<iostream>
#include<cstdlib>
#include<conio.h>
using namespace std;
int main()
{
float z=0, g=0;
int y;
srand(static_cast<unsigned int>(time(0)));
for (int x = 0; x < 1000000; x++)
{
y = (rand()%2)+1;
if(y==2)
{
z++;
}
if(y==1)
{
g++;
}
}
double perch = (g/1000000)*100;
double perct = (z/1000000)*100;
cout << "Number of heads = " << g << ", percent = " << perch << "%" << endl;
cout << "Number of tails = " << z << ", percent = " << perct << "%" << endl;
getch();
}
Last edited on May 9, 2013 at 5:31am May 9, 2013 at 5:31am UTC
May 9, 2013 at 5:32am May 9, 2013 at 5:32am UTC
You have infinite loop on lines 10 and 16. Change loop statement to conditional statement.