Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Turn this code from C to C++. It is a program which gueses a number using binary search 1.#include 2.#include 3.#include 4.#define size 100 5.int
Turn this code from C to C++. It is a program which gueses a number using binary search 1.#include2.#include 3.#include 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. "); 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(" Is 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.}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started