Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Why is the output printing 5 before the heads or tails?

#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("%2d", 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

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

You call flip and print the result of it. The flip function is printing ‘heads’ or ‘tails’, the printf is printing the 5

printf("%2d", 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("%2d", flip());

with

 flip();
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading