Submission Requirements:
1: E-mail: Three digits after study number-topic number-name-midterm. Example: 098-1-Shenmio-Midterm
2: Without attachments to submit, direct mail, content including programming ideas (write a section of their own knowledge of the topic, ideas, technical details, etc.), source code, analysis of the results of the operation and
Topic:
1. Programming first by the computer "want" a number between 1 to 100 please guess, if the person guessed the right, then end the game, and on the screen output people guess how many times to guess the number, so as to reflect the guess the level of "guess", otherwise the computer gives a hint, tell people guess the number is too big or too small, up to guess 10 times, If you have guessed 10 times, you will end the game.
Programming Ideas :
1) The computer "wants" a number of 1-100, you need to run the program at random to generate a natural number of 1-100, you need to use the rand () and srand ((int) time (0)) function;
2) Guess the number of "guess" number: The user each time the number of input and randomly generated number to compare, use if. else. . to judge;
3) Guess the number of people only 10 times, you need to use for or while loop to control the number of times, the program chose to use for loop;
4) In addition, the procedure concludes and the procedure judgment,need to introduce return, break,couture and other cooperation;
Source Code :
#include <stdio.h> #include <stdlib.h> #include <time.h>/** generates a random number between 1-n * Note: Please cooperate with Srand ((int) time (0)); function is used */int randnum (n) {return rand ()%n + 1;} int main () {srand ((int) time (0));//set random number seed int num = Randnum (+), int_num,i,min=0,max=100; printf ("%d\n", num); for (i=1;i<=10;i++) {printf ("===============%d times =======================\n", i);p rintf ("Please enter a lucky number:"); scanf ("%d", &int_num); if (Int_num >100 | | Int_num < 1) {printf ("The number you entered is%d, not between 1-100. Please retry!\n", int_num); Continue;} else if (int_num = = num) {printf ("Congratulations on your lucky number, the lucky number you guessed:%d,%d guesses. \ n", int_num,i); return 0; }else{printf ("You guessed the lucky number is incorrect, please retry!\n"); if (Int_num > num) {max=int_num; printf ("Lucky number range reduced to%d-%d!\n", Min,max);} else{min=int_num;printf ("Lucky number range reduced to%d-%d!\n", Min,max);} Continue;} } printf ("You guess the number has reached 10 caps, I'm sorry you failed to guess the lucky number, program exit!\n"); return 0;}
Run result analysis :
The source program in the first line prompts the current number of input, prompting the user to enter a narrow range of lucky numbers, the end user correctly guessed the number and current number of users guessed, or user input reached 10 times, the program regrets to quit!
Output :
Several test programming topics on C language