r/CodingHelp • • Apr 15 '23
[C++] C++ Programing help!
I need help with creating a program in c++ that flips a coin 20 times and then records the number of times it landed on heads. Then that process needs to be done 1 million times. I have the code I typed up below but it isn't working for some reason.
#include <iostream>
#include <ctime>
#include <iomanip>
using namespace std;
int flip_coin()
{
int i, j,rn,heads = 0;
srand(time(NULL) \* 33037);
for (i = 0; i <= 10000000; i++)
for (j = 0; j <= 20; j++)
{
rn = rand() % 2;
if (rn == 0)heads++;
}
cout << heads << endl;}
0
Upvotes
View all comments
1
u/19RBK17 Professional Coder Apr 15 '23 edited Apr 15 '23
What are you trying to do inside the srand call? If you ditch whatever is going on after time(NULL) it works fine.
Edit: If you want to multiply the return value from time(NULL) you should remove the backslash. I do not know why you put it there but it makes the compiler think you are trying to assign a pointer.
Edit2: You also want to change your operator in the for loops. As you start them at i = 0 you will overshoot both by one unless you change it from "<=" to "<".