We just started learning functions in my c programming class. We havent learned hardly anything about them yet, but we already have an assignment. It is a random number generator with a few things added in that our professor wanted. I have a working version, BUT, he wants us to do this:
Your program must decompose the problem into functions. Specifically, you must
declare and use a function that accepts two integer arguments as input and returns a value
as output. All of your printing must be done in the main function. The input arguments
represent a user’s guess and the correct answer. The returned value must reflect one of
the three possible outcomes of comparing the guess to the correct answer. The outcomes
are “Too Low”, “Too High”, or “Correct”. I recommend representing those outcomes
using integer values of -1, 1, and 0, respectively.
heres what i have...how do i do what he wants??
thxCode:#include <stdio.h> #include <stdlib.h> #include <time.h> int main(void) { /* begin function main */ int num; /* numbers generated */ int guess = 0; int game = 0; int counter = 0; srand(time(NULL)); /* generate the random seed generator */ num = rand() % 1000 +1 ; /* pick a number from 1 to 1000 */ printf("I am thinking of a random number between 1 and 1000\n"); /* prompt user for input */ printf("Try and guess the number.\n"); /* prompt user for input */ printf("Please enter your first guess.\n"); /* prompt user for input */ do { while (num != guess) { scanf("%d", &guess); /* input data from user */ ++counter; if (guess > 1000 || guess <= 0) { printf("Number out of range\n"); printf("Try again\n"); } else if (guess > num) { printf("Too High! try again!\n"); } else if (guess < num) { printf("Too Low! try again!\n"); } } /*end while loop */ printf("You guessed the number in %d tries!\n",counter); printf("Do you want to play again?\n"); printf("Enter n or N to quit\n"); while ( getchar() != '\n' ) ; game = getchar(); getchar(); } /*end of do while */ while (game != 'n' && game != 'N'); return 0; }/* end function main */