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

This lab was designed to reinforce programming concepts using control structures

ID: 3840110 • Letter: T

Question

This lab was designed to reinforce programming concepts using control structures. In this lab, you will practice: Using standard C library functions: time(), rand(), srand(). Loops Write a program that allows a user to guess a random number between 1 and 1000. The function rand() from the header file can be used for this purpose. Before using the function, the random number generator must be provided a "seed" value to set it up. If the same seed value is supplied each time the program is run, the results of the random number generator will come out in the same order each time. A commonly used seed value is derived from the system clock, because the seconds count changes frequently enough to provide a different seed value each time we run the program. Include this in the beginning of your main program to seed the RNG: srand(time (NULL)); To use the time function, must be included. The rand() function returns a random integer between 0 and 32767. You will need to adjust the result to be between 1 and 1000 (think about how we might do this). Use a while loop to keep looping whenever the user guesses incorrectly. The loop should terminate when the user guesses the number correctly. Your program should count and print the number of guesses by the user. Each time the user guesses a number, you should print one of the following messages: Good guess. Too low, Too high,

Explanation / Answer

// Including required library files

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

// Main method goes here

int main(void) {
   // numGuess variable for the user input of Guessing numebr
int numGuess;

// Count variable to track number of guesses by user
int count=1;

// variable to store Random numeber generated by Sys
int SysNum;

// Variable for time
time_t t;

// First time srand
srand(time(NULL));

// Variable for while loop running

int run=0;

// Loop
   while(run!=1)
   {

// Printing number of guesses made by user every time
   printf("Guess Count:%d",count);

// Prompting user to guess a numebr
   printf("Enter a number-Guess in range 1 - 1000");

// Reading number from user
   scanf("%d",&numGuess);
// incrementing counter
   count++;

// srand using seed as time
   srand((unsigned) time(&t));

//The line generates number between 1 and 1000
   SysNum=rand() % 1000;

//Conditions to print the out put on console
   if(SysNum>numGuess)
   {
   printf("Too low, Try again");
   }
   else if(SysNum<numGuess)
   {
   printf("Too high, Try again");
   }
   else
   {
   printf("Good Guess");

// run =1 to stop while loop from executing
   run=1;
   }
   }
  
   return 0;
}

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