vendredi 3 mars 2017

Nested Loops with Random Values (C)

Trying to figure this one out. Basically code needs the use of nested loops and random values.

I need to get have user pick a number between say 0-100, and then run a loop that gets a random number, and keeps going until that random number equals the value inputed by the user. The final value is total amount of times that it took for the rand() to get a number that's equal to the user input value.

Then that number gets used by another equation which runs the first loop say 80 times, then gets the average number of times that it took for rand() to get the user inputed variable.

Basically its a while-do function nested with for loop

I have been beating my brain, this is all I could muster so far:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main()
{
int a=0;
int ranvalue=0;
srand(time(NULL));
printf("Pick a Number between 0 and 99\n");
scanf("%d",&a);
   if (a>99)
        printf("error\n");
    do{
           ranvalue = rand() % (99 + 1);
    } while(ranvalue=a);


  }




Aucun commentaire:

Enregistrer un commentaire