O
osmium
Ben said:My initial problem was that I had read in a maths book that the average
no. of 6 heads in a row in a sequence of 200 tosses was at least one...
my program kept providing 0.77
I have since realised that they must have meant "at least 6"...
AFAICT they really meant exactly. I get about 1.5 "hits" under the problem
as I understand it.
I only started learning programming a few months ago- so I'm not sure
what the problem is with global variables? (And what's an alternative?)
Move the variables above main so they are included in main, or to whoever
they belong to. The idea is to pass information by passing paramters.
The #defines seem to do what I want - enums might be more appropriate,
but I haven't yet learnt what they are
That's a prefectly good reason.
The sentinel seemed easy enough to use - what potential problems are
there with using them? Why are they only a last resort?
Not many problems, they just make you look like a student. Instructors
often teach the use of sentinels to avoid teaching how to handle end of
file. The thought is that sentinels are easier to understand, and I agree.
But real programmers use end of file.
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 ?
It does compute 'exactly x in a row' ... which is what I intended...
(it looks at the character that breaks the sequence when 'i' is
incremented - which breaks the while loop)
while(A==H)
{
i++;
row_h++;
}
if(row_h==in_a_row)
count++;
row_h=0;
I goofed and missed that. It is really hard to study code with no
indentation. And no comments. And no paycheck for all the effort. That (the
general area) was my leading candidate for the trouble you were having. So
you are back at square zero WRT my help.
My uni textbook says "If you need your program to be portable, and
require the values to be uncorelated even when compiled with the
original rand and srand functions, you should extract the desired value
from high-order bits rather than low-order ones..."
But they are talking really small numbers, like six (sides on a die). Not
half the numbers.
I'm a newbie ... by habbit I tend to stick in as many extra parens as
possible
Better too many than too few. Much better.
If you want to indent and repost, I will take a fresh look at what you have.
I was bothered mostly by not knowing what it was that it was supposed to
compute.
Here's another cut at it. It is wordier than most C programs, in the hopes
of retaining some clarity. The array, as I suspect you already knew, was
just a crutch. There might be some ideas you can use. Or then it could be
all wrong. Probability and statistics are real bears. I tried to derive
the answer you quote from your book and couldn't get it.
------
#include <stdio.h>
#include <stdlib.h> // abort
#include <time.h> // time
// count the number of times that there are exactly w heads in a row
int count(int w)
{
if(w<2)
{
printf("Function count is not intended to work\nfor sequences "
"of lengths less than 2.\n\n"
"Aborting\n");
getchar();
abort();
}
static int mid_pt = RAND_MAX / 2.0;
int on = 0; // state. on means "in count mode"
int ct = 0; // number of occurances of heads
int bit = 0; // the coin. 1 is a head
int last = 0; // results for preceding flip
int k = 0; // number of heads in *this* sequence
int i;
for(i=1; i<128; i++)
{
bit = rand();
bit = (bit<mid_pt) ? 0 : 1;
if(bit && !last) // transition to heads
{
k = 1;
on = 1;
}
else if(!bit && last) // transition to tails
{
k++; // count this flip
if(k == w)
ct++; // count the winners
k = 0;
on = 0; // back to passive mode
}
else if(on && bit) // another head
k++;
else
/* do nothing */;
last = bit;
}
return ct;
} // end function
//====================
int main()
{
srand(time(NULL));
int sum = 0;
int n = 6; /* count the number of times there are *exactly* n heads in a
row.*/
int i;
for(i=1; i<1000; i++)
sum += count(n);
printf("Mean number of times there were exactly %d heads: %g",
n, (sum/1000.0) );
getchar();
}