Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

D http://ucblackboardec × rtro In Lab Midters 1 CS 107 Fall 2817 Professor Theys

ID: 3870162 • Letter: D

Question

D http://ucblackboardec × rtro In Lab Midters 1 CS 107 Fall 2817 Professor Theys September 26, 2817 For this assignment you are going to write a guessing game. The computer will randomly guess a letter between a and z and the user will type in characters guessing which letter the computer picked. Example Game Play: welcome to Midterm Lab 1 -Guess the Letter Game ·Written by M. Theys The computer has chosen a letter Please guess which letter you think the computer has chosen: 1 Your guess of 1 is not the letter the computer chose, and the letter the " computer chose is closer to z. " Please guess which letter you think the computer has chosen: Your guess of w is not the letter the computer chose, and the letter the " computer chose is closer to a. * Please guess which letter you think the computer has chosen: t " Congratulations!!! You have guessed the letter the computer has chosen. " Thanks for playing, would you like to play again (Y/N)?N " GoodBye . If the user chose Y at the end the gane would restart and pick a new letter * for the user to guess. . GRADING · 10 points header incode with student information (name, lab, ..) 10 points coenents in code detailing what you did 18 points test cases showing your code is working * 15 points prompts printing correctly * 15 points getting input from user correctly " 28 points randon character creation done correctly (different letter each run, different letter if play again, letter between a-z " 28 points able to determine whether letter computer has is closer to z or from users guess ·SUBMISSION ·Submit your code to either the link in blackboard or the tylab setup for * inlabidters include int sain(0 ( return 9

Explanation / Answer

#include <stdio.h>
#include <stdlib.h>

int main()
{
   char a,b;
   int n;
   char f;
   f='Y';
   printf("Welcome to Midterm Lab 1 - Guess the Letter Game ");
   printf("Written by M.Theys ");
   while(1)
   {
       if(f=='Y')
       {
           printf("The Computer has chosen a letter. ");
           n=rand()%26;
           a=(char)(97+n);
           f='N';
       }
       printf("Please guess which letter you think the computer has chosen: ");
       scanf("%c",&b);
       fflush(stdin);  
       if(a!=b)
       {
           printf("Your guess of %c is not the letter the computer chose, and the letter the computer chose is close to ",b);
          
           printf("%c",((b-'a')<=('z'-b))?'a':'z');
       }
       else
       {
           printf("Congratulations!!! You have guessed the letter the computer has chosen.");
           printf("Thanks for playing, would you like to play again(Y/N)?");
           char ch;
           scanf("%c",&ch);
           if(ch=='Y')
           {
               f='Y';
           }
           else
           {
               printf("GoodBye ");
               break;
           }
       }

   }
   return 0;
}