Coin flipping

"Write a program that has a function prototype before main and an implementation of the
function after main. The function to be implemented is a coin toss simulation using the random number
generator. The function should return 1 or true 50% of the time and 0 or false 50% of the time. Use
srand and the system time to make the program run differently each time. (srand(time(NULL)); ).
Keep track of the number of head and tails for 10, 100, 1000, 10,000, 100,000 and 1,000,000 trials.
Output the number of heads and tails and number of each as a percentage of the total. Notice that the
more trials the more accurate your simulation becomes."

This is the assignment im trying to do. Im getting some errors.
C4244 'argument': conversion from 'time_t' to unsigned int' possible lossof data
Line 16

LNK1120 1 unresilved externals Line 1

LNK2019 unresolved external symbol "int_cdecl flip (void)" Line 1

Any ideas


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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
 
#include <iostream>
#include <stdio.h>
#pragma warning(disable: 4996)
#include<string>
#include <stdlib.h>
#include <time.h>
#include <math.h>
#include <iomanip>

using namespace std;

int flip();                    /*Function prototype*/

int main()
{
	srand(time(NULL));

	int tossTen = 10;           /* toss coin ten times */
	int tossHundred = 100;      /* toss coin one hundred times */
	int tossThousand = 1000;    /* toss coin one thousand times */
	int tossTenThou = 10000;    /* toss coin ten thousand times */
	int tossHunThou = 100000;   /* toss coin one hundred thousand times */
	int tossMillion = 1000000;  /* toss coin one million times */
	int headsTen = 0;
	int headsHun = 0;
	int headsThou = 0;
	int headsTenThou = 0;
	int headsHunThou = 0;
	int	headsMillion = 0;
	int tailsTen = 0;
	int tailsHun = 0; 
	int tailsThou = 0;
	int tailsTenThou = 0;
	int tailsHunThou = 0;
	int tailsMillion = 0;

	for (int i = 1; i <= tossTen; i++) /* start ten toss */
	{
		if (flip() == 0) {
			headsTen++;
		}
		else {
			tailsTen++;
		}
	}             /* break ten toss*/
	for (int i = 1; i <= tossHundred; i++) /* start hundred toss */
	{
		if (flip() == 0) {
			headsHun++;
		}
		else {
			tailsHun++;
		}
	}             /* break hundred toss*/
	for (int i = 1; i <= tossThousand; i++) /* start ten toss */
	{
		if (flip() == 0) {
			headsThou++;
		}
		else {
			tailsThou++;
		}
	}             /* break thousand toss*/
	for (int i = 1; i <= tossTenThou; i++) /* start ten toss */
	{
		if (flip() == 0) {
			headsTenThou++;
		}
		else {
			tailsTenThou++;
		}
	}             /* break ten thousand toss*/
	for (int i = 1; i <= tossHunThou; i++) /* start ten toss */
	{
		if (flip() == 0) {
			headsHunThou++;
		}
		else {
			tailsHunThou++;
		}
	}             /* break Hundred Thousand toss*/
	for (int i = 1; i <= tossMillion; i++) /* start ten toss */
	{
		if (flip() == 0) {
			headsMillion++;
		}
		else {
			tailsMillion++;
		}
	}             /* break million toss*/

	cout << setw(10) << "Heads" << setw(10) << "Tails" << endl;
	cout << setw(10) << headsTen << setw(10) << tailsTen << endl;
	cout << setw(10) << headsHun << setw(10) << tailsHun << endl;
	cout << setw(10) << headsThou << setw(10) << tailsThou << endl;
	cout << setw(10) << headsTenThou << setw(10) << tailsTenThou << endl;
	cout << setw(10) << headsHunThou << setw(10) << tailsHunThou << endl;
	cout << setw(10) << headsMillion << setw(10) << tailsMillion << endl;

	system("pause");
	return 0;
}
Write a program that has a function prototype before main and an implementation of the function after main.
C4244 'argument': conversion from 'time_t' to unsigned int' possible loss of data

That's just a warning and can be safely ignored. If you want, you can do a static_cast to change the type to eliminate the warning.

unresolved external symbol "int_cdecl flip (void)" Line 1

You have a function prototype for flip() at line 13. Where is your implementation?

Lines 19-24 should be const

You have a lot of repeated code in your program. Consider consolidating your for loops into a single function.

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
#include <iostream>
#include <string>
#include <ctime>
#include <iomanip>
using namespace std;

int flip();                    /*Function prototype*/

void do_flips (int count)
{   int heads = 0, tails = 0;

    for (int i = 0; i < count; i++)
	{   if (flip() == 0) 
			heads++;
		else 
			tails++;		
	}             
	cout << setw(10) << heads << setw(10) << tails << endl;
}
	
int main()
{   srand(static_cast<unsigned int>(time(NULL)));
    cout << setw(10) << "Heads" << setw(10) << "Tails" << endl;
    do_flips (10);
    do_flips (100);
    do_flips (1000);
    do_flips (10000);    
    do_flips (100000);
    do_flips (1000000);
    system("pause");
    return 0;
}
     Heads     Tails
         6         4
        53        47
       490       510
      4932      5068
     50121     49879
    499891    500109
Press any key to continue . . .
Last edited on
When I ran yoiur code it gave me LNK2019 Error. unresolved external symbol int_cdecl flip (void)" (/flip@@YAHXZ) referenced in function "void_cdecl do_flips (int)'
I think my flip is undefined. Do I have to define flip? It says functio definition for "flip" not found on line 7.
Last edited on
Moschops How do i do that. And what should the program entail?
When I ran yoiur code it gave me LNK2019 Error. unresolved external symbol int_cdecl flip (void)" (/flip@@YAHXZ) referenced in function "void_cdecl do_flips (int)'
I think my flip is undefined. Do I have to define flip?

Correct. I left that for you to do.
Topic archived. No new replies allowed.