Guessing Game # 1.0

Source: Internet
Author: User

The program generates a random number of 1-100, and the user tries to guess the number as few times as possible.

#include <stdio.h>#include<stdlib.h>#include<time.h>#defineMax_number 100//external VariableintSecret_number;//prototypesvoidInitialize_number_generator (void);voidChoose_new_secret_number (void);voidRead_guesses (void);intMainvoid){    Charcommand; printf ("Guess The secret number between 1 and%d.\n\n", Max_number);    Initialize_number_generator ();  Do{choose_new_secret_number (); printf ("anew number has been chosen.\n");        Read_guesses (); printf ("Play again? (y/s)"); scanf ("%c", &command); printf ("\ n"); }  while(Command = ='y'|| Command = ='Y'); return 0;}//initialize_number_generator:initialize the random number generator using the time of day.voidInitialize_number_generator (void{srand (unsigned) time (NULL));}//Choose_new_secret_number:randomly selects a number between 1 and max_number and stores it in Secret_number.voidChoose_new_secret_number (void) {Secret_number= rand ()% max_number +1;}//read_guesses:repeatedly reads user guesses and tells the user whether each guess was too low, too high, or correct. When the guess was correct, prints the total number of guesses and returns.voidRead_guesses (void){    intGuess, num_guesses =0;  for (;;) {num_guesses++; printf ("Enter guess:"); scanf ("%d", &guess); if(Guess = =Secret_number) {printf ("You won in%d guesses!\n\n", num_guesses); return; }        Else if(Guess <secret_number) printf ("Too Low, try again.\n"); Elseprintf ("Too High, try again.\n"); }}

Guessing Game # 1.0

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.