Vous êtes sur la page 1sur 6

Name: WORNU CHIZOM Student number: 200789991

ANSWER TO QUESTION1: PART1: #include <stdio.h> int random(int *, int); int gameplay(int *, int ); int store( char w[20], int, int *, int , int); int record(); int b,d,s,t; void main() { char w[20]; int k[20]; int b=0,d; printf("\n THIS IS A GUESSING GAME\n"); printf(" What is your name\n"); scanf("%s",&w); printf("What other do u want to guess(HINT: Enternumbers between 1 and 20)\n"); scanf("%d",&b); d = random(k,b);/* call function to know the secret number*/ s = gameplay(&k[0],d);/* call function to play the game*/ store( w, b, k, d , s);/* call function to update record file*/ record();/* call function to store data */ } /*create a file containing numbers and store it in an array*/ int random(int *k ,int b) { FILE *fp; fp = fopen("Numbers.txt","w+"); /* open for writing and reading*/ for(b=1; b<=20; b++) { k[b] = 74 + 5*(b+5); fprintf(fp,"%d\n",k[b]); } fclose(fp); /* close the file before ending program */ return k[b]; } int gameplay( int *k, int d)
Page 1 of 6

{ int t = 1,c; printf("I have a number between 100 and 200, what is the number?\n"); while (scanf("%d",&c)) { if(c>k[d])/* condition to check if the guess is greater than the secret number*/ { printf("Too high, guess again\n"); t++; } else if(c<k[d])/* condition to check if the guess is less than the secret number*/ { printf("Too low, guess again\n"); t++; } else if(c==k[d])/* condition to check if the guess is the same as the secret number*/ { if(t==1) { printf("you are lucky!!\n"); break; } if(t==2 || t==3) { printf("Excellent!!\n"); break; } if(t==4 || t==5) { printf("Good!!\n"); break; } if(t>5) { printf("OK!!\n"); } break; } } return t; } /* update the file to read and write data into the file*/ int store( char w[20], int b, int *k , int d, int s ) { FILE *fp;

Page 2 of 6

fp = fopen ("RECORD.txt","a+");/* open for update(writing and reading)*/ fprintf(fp,"================================================== ========================\n"); fprintf(fp,"NAME: --------------------%s\n",w); fprintf(fp,"ORDER: -------------------%d\n",b); fprintf(fp,"SECRET NUMBER: -----------%d\n",k[d]); fprintf(fp,"NUMBER OF GUESSES:------- %d\n",s); fprintf(fp,"================================================== ========================\n"); fclose(fp);/* close the file before ending program */ return 0; } /* create a file to read in data*/ int record() { FILE *fp;/* points to file*/ int u; fp = fopen("RECORD.txt","r");/* open for reading*/ u = getc(fp) ; while (u!= EOF) { putchar(u); u = getc(fp); } fclose(fp);/* close the file before ending program */ return 0; }

Page 3 of 6

PART2: PROBLEMS: Write a program that implements a simple game which involves a user guessing a number. The file "numbers.txt" stores 20 integer numbers between 100 and 200. The game starts by asking the name of the user and the order of the secret number in the file "numbers.txt". The user has to guess this secret number in a series of guesses. A function(s) will be writing to take a number input from the player via the keyboard. If this number as guessed by the player is the secret number then the player wins! If not the computer should explain if the guess is lower or higher than the secret number (but do not reveal what this number is!). The computer should keep asking the player to guess until the answer is right. Also the program needs have a history of games played. ANALYSIS: Problem inputs: Names and numbers Problem output: Name, Order, Secret number and number of guesses Formulas: DESIGN: 1. 2. 3. 4. 5. 6. 7. Declaring functions and variables Asking the player to input his or her name Ask the player which order he or she wants to guess Function to create a text file containing numbers Function to play the game. Function to store, name, order, secret number and number of guesses Function to create a file history to store the name, order, secret number and number of guesses k[b] = 74 + 5*(b+5)

IMPLEMENTATION: 1. int random(int *k ,int b) 1.1. create a text file for writing 1.2. using for loop to create a set of array numbers 1.3. using the equation k[b] = 74 + 5*(b+5) to create numbers between 100 and 200 1.4. and print them on the file 2. int gameplay( int *k, int d) 2.1. Asking the player(s) to input there guess. 2.2. Set condition to know if the guess is higher or lower than the secret number. 2.3. Also check for number of tries.
Page 4 of 6

2.4. Set condition for various for number of tries, to know what to print. 3. int storage( char w[20], int *k , int d, int s ) 3.1. 4. int record() 4.1. Create a file called record to update and store characters and numbers, of games played. TESTING: Test1: Playing the game and checking if it works correctly. To check if it creates the history text file to store previous games.

Page 5 of 6

Test2: To check if the history file updates with recent games

Page 6 of 6

Vous aimerez peut-être aussi