problem with my srand programm
Question.Write a program that simulates coin tossing.For each toss of the coin the program should print heads or tails.Let the program toss the coin 100 times, and count the number of time each side of the coin appears.Print the result.The program should call a seperate function cointoss that takes no argument and returns 0 for tails and return 1 for head.
Well my code it aint workin' it keeps on printing heads or talles hundredc time.that when it is executed each time.
Code:
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
int cointoss ( void ); //fucntion prototype
int main()
{
int sum, cnt;
srand (time(0));
sum = cointoss ();
for ( cnt =1; cnt <= 100; cnt ++){
if ( sum == 0 )
printf ("Heads\t");
else
if ( sum == 1 )
printf ("Tails\t"); }
getch();
return 0;
}
int cointoss ( void )
{
int toss;
toss = 1+rand() % 2;
if ( toss == 1 )
return (0);
else
if ( toss == 2 )
return (1);
}
Re: problem with my srand programm
Quote:
Originally posted by datainjector
Well my code it aint workin' it keeps on printing heads or talles hundredc time.that when it is executed each time.
This is your main loop. Tell me, how many times are you calling the cointoss() function within this loop?
Code:
for (cnt = 1; cnt <= 100; cnt++)
{
if (sum == 0)
printf("Heads\t");
else if (sum == 1)
printf("Tails\t");
}
Just in case you don't get the answer, I'll tell you. It's none, you're not calling cointoss(), you're printing the value from the first call of cointoss() 100 times over.