Can someone please help me create a C program that manages the value 0 or 1 once and store it in a variable

Asked

Viewed 28 times

-3

Someone can help create a C program that generates the value 0 or 1, once and store it in a variable.

I tried to do it but it didn’t turn out the way I’d hoped:

#include <stdlib.h>
#include <stdio.h>
#include <time.h>
float chuva()
{
    int i;
    float chuva;
    for(i=0 ; i <= 1 ; i++)
    {
    chuva = ( 0 + rand()%1); 
        printf("chuva= %d: %d\n",i, rand());
if(chuva=1)
printf("Choveu\n");
else
printf("Nao choveu\n");
}
}
  • 1

    You have already asked this: https://answall.com/q/525221/5878

  • put 2 in place of %1

  • See the function documentation rand() and also the table of arithmetic operators in C

  • rand() % 1 takes the random value returned by rand and returns the rest of the division by 1. But every whole number is divisible by 1, so the rest of the division by 1 is always zero. And then you add zero to that result, which changes nothing, that is, the result of 0 + rand()%1 is always zero

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.