Hey again.
Continuing with my assignment and seem to have come a long way in understanding C. However part of my assignment was to create a guess the number game which randomly generates a number and you simply have to guess it, whist it either tells you if your guess was too high or too low. Now here is my code:
It works fine but I got a bit of help on generating random numbers from the Internet. My main trouble is that I don't think I can actually explain everything that's going on here.Code:#include <stdio.h> #include <stdlib.h> #include <time.h> int main(int argc, char *argv[]) { int secret_number; int guess=0; int play=1; const int LOW=-100; const int HIGH=100; time_t seconds; time(&seconds); srand((unsigned int) seconds); while (play==1) { printf("I have stored a random number between -100 and 100, try and see if you can guess the number:\n"); secret_number=rand()%(HIGH-LOW+1)+LOW; while(guess!=secret_number) { scanf("%d",&guess); if(guess==secret_number) { printf("\nYour Guess was correct! The number is %d\n", secret_number); } else if(guess>secret_number) { printf("\nGuess was too high, try again\n"); } else { printf("Guess was too low, try again\n"); } } printf ("\nWould you like to play again? Yes=1 or No=2\n"); scanf ("%d", &play); } system("pause"); return; }
If you were to explain each step in this program, what would you say?
Thank you.
-Ninestar