jeudi 26 octobre 2017

how to count how many iterations it takes to get a random number equal to user input and calculate that average in C?

Using nested loops, We have to create a program that takes a number in range of 0-99 from the user and use a seeded random number generator to try and guess the number. We have to keep track of how many numbers the RNG generates before it generates the user's number. We have to also do this process 50 times to then calculate the average number of tries it takes the RNG to guess our number. This is all i have so far and i'm stumped:

#include<stdio.h>
#include<stdlib.h>
#include<time.h>

int main()
{
    srand(time(NULL));

    int userInput;
    int randoms = rand() % 100;
    int numCalls;
    int i;
    float average = 0.0;

    printf("Please enter a number in between 0 and 99: ");
    scanf(" %d", &userInput);

    for( i = 0; i < 50; i++)
    {

        while (userInput != randoms);
        {

           numCalls = numCalls + 1;

           if (userInput = randoms)
           {
               float average = numCalls/50.0;

           }
        }
        printf("Number of iterations it took for a matching number: %d\n", numCalls);
        printf("Average number of iterations to find a match: %.2f\n", average);

    }

return;
}




Aucun commentaire:

Enregistrer un commentaire