473,696 Members | 2,103 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

coin toss problem

I've written this coin toss program, and can't figure out why it isn't
giving accurate results...
cheers,
Ben

#include <stdlib.h>
#include <stdio.h>
#define H 1
#define T 0
#define SENTINEL -1

int count=0, seed=3645, limit=200, in_a_row=6, iterations=100;

void one_run(int seed_passed)
{
int i, x, row_h;
row_h = 0;

seed=(1+seed_pa ssed);
srand(seed);

char A[limit+1];

for(i=0;i<limit ;i++)
{
x=(int)(2.0*ran d()/(1.0+RAND_MAX)) ;

if(x==H)
A[i]=H;

else
A[i]=T;
}

A[i]=SENTINEL;

i=0;

while(A[i]!=SENTINEL)
{
while(A[i]==H)
{
i++;
row_h++;
}

if(row_h==in_a_ row)
count++;

row_h=0;

if(A[i]!=SENTINEL)
i++;
}
}

int main(int argc, char **argv)
{
int i;
double av_occur;

if (argc==2)
seed=atoi(argv[1]);

printf("\nEnter number of coin tosses: ");
scanf("%d", &limit);

printf("\nEnter number of heads to be found in a row: ");
scanf("%d", &in_a_row);

if(limit<=1000)
iterations=1000 00;

for(i=1;i<=iter ations;i++)
{
one_run(seed);
}

(av_occur=1.0*c ount/iterations);

printf("\nIf a coin is flipped %d times:\n", limit);
printf("Average no. of occurances of %d heads in a row is %.5f"
, in_a_row, av_occur);
printf("\n(with %d tests)", iterations);

/* printf("\ncount is %d", count);
*/
return 0;
}

Jun 17 '06 #1
52 5382
ce**********@ho tmail.com wrote:

I've written this coin toss program, and can't figure out why it isn't
giving accurate results...
cheers,
Ben

#include <stdlib.h>
#include <stdio.h>
#define H 1
#define T 0
#define SENTINEL -1

int count=0, seed=3645, limit=200, in_a_row=6, iterations=100;

void one_run(int seed_passed)
{
int i, x, row_h;
row_h = 0;

seed=(1+seed_pa ssed);
srand(seed);

char A[limit+1];


This code should not compile unless you have a C99 system, which I
doubt. limit+1 is not a constant, so the compiler has no idea what
to do. In addition, you are defining storage after executable
code. Turn up your warning levels. Also you are allowed to use
blanks in expressions, the blank embargo was lifted some years ago.

--
"A man who is right every time is not likely to do very much."
-- Francis Crick, co-discover of DNA
"There is nothing more amazing than stupidity in action."
-- Thomas Matthews

Jun 17 '06 #2
CBFalconer wrote:
ce**********@ho tmail.com wrote:
I've written this coin toss program, and can't figure out why it isn't
giving accurate results...
cheers,
Ben

#include <stdlib.h>
#include <stdio.h>
#define H 1
#define T 0
#define SENTINEL -1

int count=0, seed=3645, limit=200, in_a_row=6, iterations=100;

void one_run(int seed_passed)
{
int i, x, row_h;
row_h = 0;

seed=(1+seed_pa ssed);
srand(seed);

char A[limit+1];

This code should not compile unless you have a C99 system, which I
doubt.


Why? Compiles fine with either compiler on mine.

--
Ian Collins.
Jun 17 '06 #3
<ce**********@h otmail.com> wrote:
I've written this coin toss program, and can't figure out why it isn't
giving accurate results...
I don't know what your problem is but I will make some observations anyway.
First of all, get rid of the global variables. There is no need to resort
to global variables in a program this small and simple.

Is there any reason not to use enum instead of the #defines? They are more
appropriate and would probably do what you want.

Don't use the sentinel. Sentinels are something you resort to when there is
a good reason. There is no good reason here.

Reduce the program to its essence. You seem to be able to handle command
line, a default set of data, or user supplied data. This is just
obfuscating things right now. Choose one. I would use built in data for
initial testing.

Pay attention to the definition of in_a_row. Does it mean *exactly* n in a
row? The code says no. To determine that your code would have to look at
the next character - the one that breaks the sequence- , and it doesn't do
that. To me, exactly is the most sensible thing to compute. For example,
if in_a_row is 3 what does the following sequence of heads get counted as:
001111111100 ?

I tried the corner cases, in a row = 1 and in a row = 0 and got strange
results.

You might refuse to accept such questions as n = 1 in the final version of
your code.

The crucial number is count. If you still have problems, add code to print
the raw value of count.

There are also a few embedded comments.
#include <stdlib.h>
#include <stdio.h>
#define H 1
#define T 0
#define SENTINEL -1

int count=0, seed=3645, limit=200, in_a_row=6, iterations=100;

void one_run(int seed_passed)
{
int i, x, row_h;
row_h = 0;
Don't separate the definition from the initial value.

IOW
int row_h = 0;
seed=(1+seed_pa ssed);
srand(seed);

char A[limit+1];

for(i=0;i<limit ;i++)
{
x=(int)(2.0*ran d()/(1.0+RAND_MAX)) ;
Too complicated.

Note that if the number drawn is less than 1/2 RAND_MAX, you have divided
the distribution into two almost equal parts.
if(x==H)
A[i]=H;

else
A[i]=T;
}

A[i]=SENTINEL;

i=0;

while(A[i]!=SENTINEL)
{
while(A[i]==H)
{
i++;
row_h++;
}

if(row_h==in_a_ row)
count++;

row_h=0;

if(A[i]!=SENTINEL)
i++;
}
}

int main(int argc, char **argv)
{
int i;
double av_occur;

if (argc==2)
seed=atoi(argv[1]);

printf("\nEnter number of coin tosses: ");
scanf("%d", &limit);

printf("\nEnter number of heads to be found in a row: ");
scanf("%d", &in_a_row);

if(limit<=1000)
iterations=1000 00;

for(i=1;i<=iter ations;i++)
{
one_run(seed);
}

(av_occur=1.0*c ount/iterations);
What are those parens for? Just a kind of idle question.
printf("\nIf a coin is flipped %d times:\n", limit);
printf("Average no. of occurances of %d heads in a row is %.5f"
, in_a_row, av_occur);
printf("\n(with %d tests)", iterations);

/* printf("\ncount is %d", count);
*/
return 0;
}

Jun 17 '06 #4
Ian Collins said:
CBFalconer wrote:
ce**********@ho tmail.com wrote:
<snip>

int count=0, seed=3645, limit=200, in_a_row=6, iterations=100;
<snip>
char A[limit+1];

This code should not compile unless you have a C99 system, which I
doubt.


Why? Compiles fine with either compiler on mine.


If you're using a C99 compiler, well, fair enough. If you're using a
compiler adhering to an older standard, like 99.90813774% of us, then your
C compiler is required to issue a diagnostic message for the above code,
which violates this constraint: "The expression that specifies the size of
an array shall be an integral constant expression that has a value greater
than zero." Since limit+1 is not an integral constant expression, a
diagnostic message is required.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Jun 17 '06 #5

osmium wrote:
int i, x, row_h;
row_h = 0;


Don't separate the definition from the initial value.

IOW
int row_h = 0;


Is there a reason to use an initialization instead of
an assignment after the declaration? I prefer declaring
and then assigning, but the only reason is that I find
it annoying when I'm debugging and the debugger stops
in the declaration block. Is this more than just a stylistic
issue, or do some compilers generate different code for
the 2 cases?

Jun 17 '06 #6
"Bill Pursell" writes:
osmium wrote:
> int i, x, row_h;
> row_h = 0;


Don't separate the definition from the initial value.

IOW
int row_h = 0;


Is there a reason to use an initialization instead of
an assignment after the declaration? I prefer declaring
and then assigning, but the only reason is that I find
it annoying when I'm debugging and the debugger stops
in the declaration block. Is this more than just a stylistic
issue, or do some compilers generate different code for
the 2 cases?


My objection was based on documentation. I found a place in his code where
the value seemed to be an un-initialized datum. In fact it wasn't, but the
initialization was hidden, at least from me. For a while.

I doubt if there are any non-stylistic issues.
Jun 17 '06 #7

<ce**********@h otmail.com> wrote in message
news:11******** **************@ r2g2000cwb.goog legroups.com...
cheers,
Ben
#include <stdlib.h>
#include <stdio.h>
#define H 1
#define T 0
#define SENTINEL -1

I might be able to provide comment on your data, but I crap out right here.
Unlike Keith Thompson, I am not 100% certain what this ultimate statement
means. BTW, I'm not even sure that 'statement" is the right descriptor for
something that doesn't necessarily end in a ';'. Anyways,
#define SENTINEL -1
probably instructs the preprocessor to replace every instance of SENTINEL
with negative one. When I looked at it, I thought you were decrementing. I
think better form would be
#define SENTINEL '-1'
or
#define SENTINEL (-1)
, I'm not sure which, but since '-' is a token, it needs something. Post
your "howdy and thanks, ben" below, so that they can be easily snipped.
furunculus

Jun 17 '06 #8
Bill Pursell wrote:

osmium wrote:
int i, x, row_h;
row_h = 0;
Don't separate the definition from the initial value.

IOW
int row_h = 0;


Is there a reason to use an initialization instead of
an assignment after the declaration? I prefer declaring
and then assigning,


So do I.
but the only reason is that I find
it annoying when I'm debugging and the debugger stops
in the declaration block.


I prefer to initialise just prior to usage because
my compiler issues a warning for unused variables,
which is a warning that I like.
My compiler considers an initialised variable,
to be a used one.

--
pete
Jun 17 '06 #9
Joe Smith wrote:

<ce**********@h otmail.com> wrote in message
news:11******** **************@ r2g2000cwb.goog legroups.com...
#define SENTINEL -1

I think better form would be
#define SENTINEL '-1'
or
#define SENTINEL (-1)
, I'm not sure which, but since '-' is a token, it needs something.


I prefer
#define SENTINEL (-1)
because I don't know what '-1' means.

--
pete
Jun 17 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
1668
by: Jon Monteleone | last post by:
Greetings, I posted a few days back and didnt get much of a response, so I figured I would post again with more detail. I am running gnome under fedora core 4. I want a kid to be able to drop a quarter into a coin slot and get 15 minutes of time on an account that has "restricted" Internet access via the firefox web browser. We are experimenting with different ways to allow kids to safely surf the web during school hours while...
12
2846
by: DannyB | last post by:
I'm just learning Python. I've created a simple coin flipper program - here is the code: #Coin flipper import random heads = 0 tails = 0 counter = 0
3
16927
by: viewsonic | last post by:
Help, im a student that has to write a program for counting coins. Half of the program works but the other half doesn.t should have the following parameters. output is: Name Date total number of coins number of quarters number of dimes
0
1543
by: coolindienc | last post by:
Honestly, I am totally lost about this program. I don't even know where to start. At first I thought I can ask a user to flip a coin. But then again one will get bored flipping coin fot 100 times. How do I get a computer to flip coin and get result? Any ideas??? Any help will be greatly appreciated greatly!!!!!!!! Andy
5
3841
by: sallyk57 | last post by:
I have to make a program that would ask a user for their guess (heads or tails) and tells them if its correct or not then it has to ask the user if they want to play again. I figured out how to do this but the part that i am stuck on is how to print a summary at the end of this program tracking how many wins and how many losses. import cs1.Keyboard; public class CoinToss6 { public static void main(String args) { ...
6
6069
blackstormdragon
by: blackstormdragon | last post by:
I just started a C++ class and now we're doing loops and have to make a coin flipping program. Well here's mine: #include<iostream> #include<cstdlib> using namespace std; int flip(); void main () { int coin, counter, tails = 0, heads = 0; for (counter = 1; counter <= 100; counter++)
8
24916
by: PAK11 | last post by:
This is what i have so far.......I need to add a function named coin to simulate a coin toss where heads is represented by a 1 and tails a 2. The outcome of the toss should be printed and the result should be return to the main program. I'm having trouble figuring it out, can anyone please help? #include <iostream> using namespace std; # include <ctime> int coin();
0
1168
Chuncatty
by: Chuncatty | last post by:
How much does a Draped Bust coin fabricate cost? (Sorry for my Eng) I ask a beau to see if it's genuine he said it is, down repay a collage professor i met said so. It's in virtue word, well-grounded the top formerly larboard piece is kinda scratched. How much do you meditate on it's worth? pic of coin is here: http://coindad.com/coin/Cent_1803_F.jpg" http://coindad.com/coin/Cent_1803_F.jpg
18
3303
by: Chinde | last post by:
Hi I'm using AS2 to produce a simple platform game. So far so good I would like the character to collect coins or whatever. so what I have done is create a coin movie clip, I've run a hittest on the coin and my character and when this = true set the coin to _visible = false, move it way off screen and add 10 point to the _global.score. all is working fine but I haven't a clue about having more than one instance of the coin, or more precisely I...
0
8595
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9142
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8875
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8849
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5855
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4353
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4608
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3030
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
1989
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.