Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

a) Write a program in C that determines how many calls to a random number genera

ID: 3798872 • Letter: A

Question

a) Write a program in C 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. Use a seeded random number generation function; otherwise all iterations of the outer loop (part b below) will be the same. b) Embed the above program in a second loop that makes this computation 50 times and prints out the average number of times that the program took to find a match for the number entered. Musts: 1) You must use at least two nested (embedded) loops. 2) You must use a seeded random number generator. 3) The output average of times the inner loop ran must be a floating point number. 4) Lastly (and obviously!), your program must be written in C and must compile and execute correctly from Code::Blocks

Explanation / Answer

#include <stdio.h>
#include <stdlib.h> /* srand, rand */
#include <time.h>
int main()
{
int n, randNum;
int i=0,j =0;
srand (time(NULL));
printf("Enter the number between 0 and 99: ");
scanf("%d", &n);
for(i=0; i<50; i++){
for(; ; j++){
randNum = rand() % 100;
if(randNum ==n){
break;
}
}
}
printf("Average of times the inner loop; %lf ", j/(double)50);
  
  

return 0;
}

Output:

sh-4.2$ gcc -o main *.c                                                                                                                                                                                                                                               

sh-4.2$ main                                                                                                                                                                                                                                                           

Enter the number between 0 and 99: 15                                                                                                                                                                                                                                  

Average of times the inner loop; 96.120000                                                                                                                                                                                                                             

sh-4.2$ main                                                                                                                                                                                                                                                          

Enter the number between 0 and 99: 45                                                                                                                                                                                                                                  

Average of times the inner loop; 102.100000

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote