its complicated... it has to do with coin flips. 3people flip a coin, person whose coin doesnt match gets one from each of the other 2 people. first one to zero loses, game resets, and it is played a total of 100,000 times. trying to count the average number of turns per game.
I commented out a==n; b==n; c==n; because I didn't understand what was meant by it. You are comparing them, but not in an if statement? Did you overload the '==' in your class?
and also, I changed rand()%2==x; rand()%2==y; rand()%2==x;
to x=rand()%2; y=rand()%2;
I at least got a display when I did it. Not sure what it's supposed to be. I got 54.
#include <math.h>
#include <stdio.h>
#include <cstdlib>
#include <iostream>
usingnamespace std;
int main () {
int a, b, c, x, y, z, n, g, q, u;
cout << "How many coins does each individual player start with? ";
cin >> n;
q=0;
for (g=0; g<100000; g++)
{
a=n; b=n; c=n;
q++;
while (a!=0 && b!=0 && c!=0)
{
x=rand()%2; y=rand()%2; z=rand()%2;
q++;
if (x+y+z!=0 && x+y+z!=3)
{
if (x==y)
{
c+2; a--; b--;
}
elseif (x==z) {
b+2; a--; c--;
}
elseif (y==z) {
a+2; b--; c--;
}
}
}
}
cout<< q/100000 << endl;
return 0;
}
I compiled it with all warnings displayed and got the following messages:
test.cpp: In function `int main()':
test.cpp:31: warning: statement has no effect
test.cpp:34: warning: statement has no effect
test.cpp:37: warning: statement has no effect
test.cpp:10: warning: unused variable 'u'
cout << "How many coins does each individual player start with?" << endl;
cin >> n;
cout << "How many games will be played?" << endl;
cin >> u;
q=0;
for (g=0; g<u; g++) { //sets number of games to whatever user entered;
a=n; b=n; c=n; //all players, a,b,c, begin with number of coins
//previously entered
q++;
while (a!=0 && b!=0 && c!=0) { //while all users have coins
x=rand()%2; y=rand()%2; z=rand()%2; //assign each person heads(0) or
//tails(1) randomly
q++;
if (x+y+z!=0 && x+y+z!=3) { //if coins are not all 0 or all 1:
if (x==y) { //if players a and b have same result, they
c+2; a--; b--; //award player c their coins from that turn
}
else if (x==z) { //if players a and c have same result, they
b+2; a--; c--; //award player b their coins from that turn
}
else if (y==z) { //if players b and c have the same result, they
a+2; b--; c--; //award player a their coins from that turn
}
}
}
}
cout<< q/u << endl; //tells user the total number of turns, q, from all cumulative games
return 0; //divided by the total number of games, providing the average number
} //of turns per game
Look at the warnings in @TheJJJunk's e-mail. The statements without effect are c+2;, b+2;, and a+2;. You are performing an arithmetic operation and then ignoring the results.
You should be using the += operator instead. That will store the result back into the variable in question.
Your bug will cause shorter execution runs because the coins are being discarded rather than being passed from player to player.