Question

In: Computer Science

Write a program in C language that uses a binary search algorithm to guess a number...

Write a program in C language that uses a binary search algorithm to guess a number from 1 to 100. The computer will keep guessing until they get the users number correct.

Solutions

Expert Solution

1.#include <stdio.h>
2.#include <strlib.h>
3.#include <simpio.h>
4.#define size 100
5.int binSearch (int num);
6.void getArray (int arr[]);
7.main()
8.{
9.      printf("Think of a number in the range of 1-100 and the program will guess it.\n");
10.      int arr[size];//array declaration
11.      getArray(arr);//function cal.control goes to line 15
12.      binSearch(arr);//function call.control goes to line 23
13.      getchar();//function call
14.}

15.void getArray (int numbers[])
16.{
17.     int number;

18.      for(number=1;number>=100;number++)//allocating numbers from 1 to 100 in array
19.      {
20.                 arr[number]=number;                                 
21.      }    
22.}

23.int binSearch(int num)
24.{
25.      int low, high, mid;//declare 3 varibles
26.      string strReply;//declare a string

27.     low=0;
28.     high=size-1;

29.      while(low<=high);
30.      {
31.                 mid=low+high/2;//mid value is found by taking average of low and high
32.                printf("\nIs the number %d ?\t", mid);
33.                strReply= GetLine();//get reply from user
34.     if(StringEqual(strReply, "no"))//if reply is no,the following questions will be asked.
35.                 {
36.                          printf("Is the number greater than %d ?\t", mid);
37.                          if(StringEqual(strReply, "no"))//if num is lesser than mid
38.                         { 
39.                                     high=mid-1;                         
40.                          }
41.                          else if(StringEqual(strReply, "yes"))//if num is greater than mid
42.                          {
43.                                     low=mid+1;     
44.                          }
45.                 }
46.                 else if(StringEqual(strReply, "yes"))//if the number is mid
47                 { 
48.                          return(mid);     
49.                 }
50.                 else
51.                 {
52.                          return(-1);    
53.                 } 
54.}

Related Solutions

Assume you need to write a Java program that uses a binary search algorithm to search...
Assume you need to write a Java program that uses a binary search algorithm to search a sorted array for a given value. 1. Write a Java pseudocode that uses recursion to accomplish the task. Here is a hint. When you are searching for a particular value in an array, there are two possible outcomes. 1) The value is found and the array index of that value is returned. 2) The value is not found and we return -1. (5...
Write a program in C++ to implement Binary Search Algorithm. Assume the data given in an...
Write a program in C++ to implement Binary Search Algorithm. Assume the data given in an array. Use number of data N at least more than 10. The function Binary Search should return the index of the search key V.
Make a Binary search program for C# and write algorithm and explain it in easy words...
Make a Binary search program for C# and write algorithm and explain it in easy words also show output and input
Create a List object that uses the binary search algorithm to search for the string "A"....
Create a List object that uses the binary search algorithm to search for the string "A". Display a message box indicating whether the value was found. Language: C#
Correct this Binary Search (C++) // This program demostrates linear search algorithm #include <iostream> using namespace...
Correct this Binary Search (C++) // This program demostrates linear search algorithm #include <iostream> using namespace std; // Binary search algorith // f is the first , l is the last , t is the target int binarySearch(int stgrade[], int f, int l, int t) { while (f <= l) { int m = f + (l - l) / 2; // Check if x is present at mid if (stgrade[m] == t) return m; // If x greater, ignore...
Write a version of the binary search algorithm that can be used to search a string...
Write a version of the binary search algorithm that can be used to search a string vector object. Also, write a program to test your algorithm. (Use the selection sort algorithm you developed in Programming Exercise 12 to sort the vector.) Your program should prompt the user to input a series of strings, ending the input stream with zzz. The program should then prompt the user to search for a specific string in the list. If the string is found...
Write a program of Binary Search in C++ by using function and arrays with the explanation.
Write a program of Binary Search in C++ by using function and arrays with the explanation.
Binary Search Algorithm a.) Create a Java application that utilizes the "Binary Search Algorithm" presented in...
Binary Search Algorithm a.) Create a Java application that utilizes the "Binary Search Algorithm" presented in chapter 19 (NOT Java's pre-built binarySearch() method from imported Java library) to search for an integer in a random array of size 30 in the range of 0 to 1000 inclusive. You should use Java's random number generator to randomize the numbers in your array. b.) The application's main() method should display unsorted array and sorted array, prompt user for a search key, allow...
In C++: Write a binary search tree and include the functions to find the number of...
In C++: Write a binary search tree and include the functions to find the number of nodes and the height of the tree. Test your code in main. Print the post-order, in-order and pre-order traversal.
In programming C language, write a program that creates a binary tree of words to be...
In programming C language, write a program that creates a binary tree of words to be used as a spell checking device for various text files. The list of words will come from a file “words.txt”. Your program is to read through the “words.txt” file and insert the word on that line into the tree in lexicographic order (also known as Dictionary order). Words in the file will be separated by spaces. Once this is done, your program should then...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT