17.9k views
3 votes
implement a linear search typing program. repeatedly ask the user what letter they are thinking until you get the right one. stop when the user types the exclamation point. submit the code in the file main.c. you may supply additional files if you wish to. you main program must be in main.c. assumption: you may assume that the user will only type: y, y, n, or n. the user will only enter exactly 1 character and will not enter any other characters. you do not have to provide any error checking on this input. remember we are simulating a person who can only give us yes/no answers. assumption: you may assume the user will never type more than 100 characters before entering '!'. example execution trace: are you thinking of the letter ' '? n are you thinking of the letter '!'? n are you thinking of the letter '.'? n are you thinking of the letter 'a'? n are you thinking of the letter 'b'? n are you thinking of the letter 'c'? y are you thinking of the letter ' '? n are you thinking of the letter '!'? n are you thinking of the letter '.'? n are you thinking of the letter 'a'? y are you thinking of the letter ' '? n are you thinking of the letter '!'? n are you thinking of the letter '.'? n are you thinking of the letter 'a'? n are you thinking of the letter 'b'? n are you thinking of the letter 'c'? n are you thinking of the letter 'd'? n are you thinking of the letter 'e'? n are you thinking of the letter 'f'? n are you thinking of the letter 'g'? n are you thinking of the letter 'h'? n are you thinking of the letter 'i'? n are you thinking of the letter 'j'? n are you thinking of the letter 'k'? n are you thinking of the letter 'l'? n are you thinking of the letter 'm'? n are you thinking of the letter 'n'? n are you thinking of the letter 'o'? n are you thinking of the letter 'p'? n ar

User Margareth
by
7.0k points

1 Answer

5 votes

Final answer:

To implement a linear search typing program that asks a user for a series of yes or no answers until the correct character is guessed or an exclamation point is entered, a C program loops through a predefined character array, querying the user for each character.

Step-by-step explanation:

To implement a linear search typing program in C, you can use a simple character array to simulate the characters the user may be thinking of and a loop to iterate over this array while asking the user to respond with 'y' for yes or 'n' for no. The loop should exit when the user types the exclamation point '!' which indicates that the correct letter was guessed, or if the limit of 100 characters has been reached.

Here's an example code snippet in C that follows these instructions:

#include

int main() {
char letters[] = " !.abcdefghijklmnopqrstuvwxyz"; // Array of possible characters
char response;
int index = 0;

// Linear search until the correct letter is found or '!' is entered
while(index < sizeof(letters) - 1) {
printf("Are you thinking of the letter '%c'? ", letters[index]);
scanf(" %c", &response);
if(response == 'y') {
printf("Guessed the correct letter '%c'!\\", letters[index]);
break;
}
if(response == '!') {
printf("Search stopped by user.\\");
break;
}
index++;
}
return 0;
}

This program will repeatedly ask the user if they are thinking of the current letter in the array and wait for a 'y' or 'n' response. It keeps track of the index within the letters array and moves on to the next character if the user responds with 'n'.

User Trunal Bhanse
by
7.7k points