#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int flip(void); //function prototype
int main(void)// main function{//srand(time(NULL));//random generator
for(int i=1; i<=100; i)// for loop flip coin 100 times
{
printf("-", flip());
if(i % 10==0)
puts("");
}// end of for loop
}// end of main
// Coin flip function
int flip (void){
int toss = 0;
toss = rand()%2;
// generates 1 or 2 at random
if(toss==1)
printf("heads");
else
printf("tails");
}//end of flip function
Output:
heads 5heads 5tails 5tails 5heads 5tails 5tails 5tails 5tails 5tails 5
heads 5heads 5heads 5heads 5heads 5heads 5heads 5tails 5heads 5tails 5
heads 5tails 5tails 5heads 5tails 5tails 5heads 5tails 5tails 5heads 5
heads 5tails 5heads 5tails 5heads 5tails 5heads 5heads 5heads 5tails 5
heads 5heads 5tails 5heads 5heads 5tails 5heads 5heads 5heads 5tails 5
heads 5tails 5tails 5heads 5heads 5heads 5heads 5heads 5heads 5tails 5
tails 5heads 5tails 5tails 5tails 5tails 5tails 5tails 5tails 5tails 5
tails 5heads 5tails 5heads 5tails 5tails 5tails 5heads 5heads 5tails 5
heads 5heads 5tails 5tails 5tails 5tails 5tails 5tails 5heads 5tails 5
tails 5heads 5tails 5heads 5heads 5tails 5tails 5tails 5heads 5heads 5
CodePudding user response:
You call flip and print the result of it. The flip function is printing 'heads' or 'tails', the printf
is printing the 5
printf("-", flip());
note that
int flip (void){
int toss = 0;
toss = rand()%2;
// generates 1 or 2 at random
if(toss==1)
printf("heads");
else
printf("tails");
}//
says that flip returns an integer. (Not sure what the would be) but you do not return anything, what happens is that the compiler is returning to you the last return value of any function, which was printf
returning the count of characters it printed (Note that is not what should happen, the compiler should warn you that you made an error)
Since its not clear what value flip shoud return I suggest it is changed to 42 - a good number
int flip (void){
int toss = 0;
toss = rand()%2;
// generates 1 or 2 at random
if(toss==1)
printf("heads");
else
printf("tails");
return 42;
}//
your output will now be 'heds 42tails 42...'
Probably you just dont want to print the result so replace
printf("-", flip());
with
flip();
CodePudding user response:
inside your int flip (void) function , you haven;t return any integer , so some random integer will be considered.
for example:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int check (void );
int main(void) { // main function{//srand(time(NULL));//random generator
for(int i=1; i<=100; i)// for loop flip coin 100 times
{
printf("-", check());
if(i % 10==0)
puts("");
}// end of for loop
}// end of main
int check (void ) {
printf("hi");
}
will print out something like this
hi 2hi 2hi 2hi 2hi 2hi 2hi 2hi 2hi 2hi 2
hi 2hi 2hi 2hi 2hi 2hi 2hi 2hi 2hi 2hi 2
hi 2hi 2hi 2hi 2hi 2hi 2hi 2hi 2hi 2hi 2
hi 2hi 2hi 2hi 2hi 2hi 2hi 2hi 2hi 2hi 2
hi 2hi 2hi 2hi 2hi 2hi 2hi 2hi 2hi 2hi 2
hi 2hi 2hi 2hi 2hi 2hi 2hi 2hi 2hi 2hi 2
hi 2hi 2hi 2hi 2hi 2hi 2hi 2hi 2hi 2hi 2
hi 2hi 2hi 2hi 2hi 2hi 2hi 2hi 2hi 2hi 2
hi 2hi 2hi 2hi 2hi 2hi 2hi 2hi 2hi 2hi 2
hi 2hi 2hi 2hi 2hi 2hi 2hi 2hi 2hi 2hi 2
You can change the code something like this
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int flip(void); //function prototype
int main(void) { // main function{//srand(time(NULL));//random generator
for(int i=1; i<=100; i)// for loop flip coin 100 times
{
flip();
if(i % 10==0)
puts("");
}// end of for loop
}// end of main
// Coin flip function
int flip (void){
int toss = 0;
toss = rand()%2; // generates 1 or 2 at random
if(toss==1)
printf("heads\n");
else
printf("tails\n");
return 0;
}//end of flip function
CodePudding user response:
Flip returns the number of characters printed (5) as heads and tails are same length. And you print the result of flip inside the main loop. So it prints first heads/tails and then 5. Actually, you should return some meaningful value from flip. Otherwise, flip will return whatever happens to be in its return register.