Accumulator to average coin tosses

Hello guys?
I am a noob at C++, here is my problem I have to solve:

Using a loop and rand(), simulate a coin toss 10000 times
Calculate the difference between heads and tails.
Wrap the above two lines in another loop, which loops 1000 times.
Use an accumulator to sum up the differences
Calculate and display the average difference between the number of heads and tails.

The code is not working as expected in terms of the accumulator, the loop seems to work fine.

Thank you for the help
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include <iostream>
using namespace std;
int main() 
{


	int heads = 0, tails = 0, num, total = 0;

	srand(time(NULL));

	for (int h = 0; h < 1000; h++) // Loop Coin Toss
	{
		for (int i = 0; i < 10000; i++) // COIN TOSS
		{
	
				int random = rand() % 2;

			if (random == 0)
			{
			heads++;
			}
			else 
			{
			tails++;
			}

		}

		cout << abs((heads++ - tails++));
		cin >> num;
		total =+ num;

	}
	cout << "The average distance between is " << total / 1000 << endl;

	cin.get();
	return 0;
}
Why are you obtaining user input num, and then adding this number to total?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <iostream>
#include <ctime>
#include <cstdlib>
#include <cmath>
using namespace std;

int main()
{
	int total {};

	srand(static_cast<unsigned int>(time(NULL)));

	for (int h = 0; h < 1000; h++) // Loop Coin Toss
	{
		int heads {}, tails {};

		for (int i = 0; i < 10000; ++i) // COIN TOSS
			if (rand() % 2 == 0)
				++heads;
			else
				++tails;

		total += abs(heads - tails);
	}

	cout << "The average distance between is " << total / 1000.0 << '\n';
}



THANK YOU! yeah im not super understanding with this yet lol only been doing for a few weeks lol
Hello nickmcp11,

I was working on your code when seeplus posted, So some of what I did you may fine useful either now or in the future.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include <iostream>
#include <cstdlib>  // <--- Added. For "rand" and "srand".
#include <ctime>    // <--- Added.
#include <cmath>    // <--- Added.

using namespace std;

int main()
{
    constexpr int MAXLOOPS{ 20 };  // <--- Shortened for testing.
    constexpr int MAXTOSSES{ 10000 };

    int total{};  // <--- Best to initialize all variables. In the end "num" has no use. "heads" and "tails" moved.

    //srand(time(NULL));
    srand(static_cast<unsigned int>(time(nullptr)));

    for (int h = 0; h < MAXLOOPS; h++) // Loop Coin Toss
    {
        int heads{}, tails{};

        for (int i = 0; i < MAXTOSSES; i++) // COIN TOSS
        {
            //int random = rand() % 2;

            if (!(rand() % 2))
            {
                heads++;
            }
            else
            {
                tails++;
            }
        }

        cout << abs((heads++ - tails++)) << '\n';  // <--- Is there a reason for adding 1?

        //cin >> num;  // <--- Does this have a purpose?

        total += abs((heads - tails));
    }

    cout << "The average distance between is " << static_cast<double>(total) / MAXLOOPS << '\n';

    cin.get();  // <--- Needs a prompt.

    return 0;
}

Line 4: For my IDE, MSVS2017, "cmath" is included through "iostream". DO NOT count on this for every compiler and set of header files. If you think you need it include it.

When you look through the code the constant variables make it much easier to use. They also avoid magic numbers that may not all get changed if you need to revise the program.

Andy
Topic archived. No new replies allowed.