Hello Im working on a program in C right now that determines

Hello.

I\'m working on a program in C right now that determines how many calls to a random number generator it takes to match a number between 0 and 99 that is entered by a user, using a seeded random number function.

Then I am trying to embed the above program in a second loop that makes this computation 50 times and prints out the average number of times the above program took to match the number entered.

So far I\'ve been pretty stuck.

I\'ve been able to get a working random number generator to produce one number, but I am having difficulties understanding where to have the scanf() placed for the user input as well as determining how many calls to the number it would take.

Any help would be greatly appreciated.

Solution

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
int main()
{
int n;
int count[50];
time_t t;
printf(\"enter a number : \");
scanf(\"%d\",&n);
int c,turn;
for(int turn=0;turn<50;turn++)
{
srand((unsigned) time(&t));
c=0;
while(true)
{
c++;
int x=rand()%100;
if(x==n)break;
}
count[turn]=c;
c=0;
}
int sum=0;
for(int i=0;i<50;i++)
{
sum=sum+count[i];
}
printf(\" number found in average of %d turns \",(int)(sum/50));
return 0;
}

Hello. I\'m working on a program in C right now that determines how many calls to a random number generator it takes to match a number between 0 and 99 that is

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site