My assignment asks to write a program to read a list of integer data items into an array, determine the position of a number in the list using binary search. The program should print the left and right endpoints for each searching step. The program should print the location of the number if he number is found and print 0 if the number is not found.
the given inputs are 3 7 8 9 11 13 17 25 27 28 30 31 33 35 36 40 44 47 50 51 53 55 67 68 72 77 78 80 82 91 99
IM suppose find the location of number 30 and 96
when I enter the inputs the given inputs the program doesn't not run is there an error with sstream inside the function stringToArray() it may need to allow it to read that many values
please help
I will provide the code below
#include <iostream>
#include <string>
#include <sstream> // allows use of "stringstream" which will read in a message
using namespace std;
int SIZE = 200;
int bTree[200]= {0};
void stringToArray(string input);
void insertNum(int x);
void searchTree(int x);
int main()
{
string userInput;
int key= 0;
char choice;
cout<< "Enter values sepereated by spaces. "<< endl;
cout<< "Example input: 19 24 7 14 9 1 0" << endl;
cout<< "Input: ";
getline(cin, userInput);
stringToArray(userInput);
cout<< endl << endl;
do{
cout<< "\n\nEnter value to search: ";
cin>> key;
searchTree(key);
cout<< "\n Would you like to search another number?"<< endl;
cout<< "Enter y for yes or n for no" << endl;
cout<< "Choice: ";
cin>> choice;
toupper(choice);
}while(choice == 'Y');
return 0;
}
void stringToArray(string input)
{
int count= 0;
int temp= 0;
stringstream ss(input);
cout<< "\n TEST \n";
while(ss.good())
{
ss>>temp;
insertNum(temp);
count++;
}
cout<< "\n TEST \n";
}
void insertNum(int x)
{
int i= 0;
for(;;)
{
if(bTree[i] == 0)
{
bTree[i] = x;
break;
}
else
{
if(bTree[i] < x)
{
i= 2*i + 2;
}
else
{
i= 2*i + 1;
}
}
}
}
void searchTree(int x)
{
int i= 0;
for(;;)
{
if(bTree[i] == 0)
{
cout << "Array is empty 0" << endl;
break;
}
else if (bTree[i] == x)
{
cout << "Value: " << x << " found at index " << i << endl;
break;
}
else if (bTree[i] < x)
{
i= 2*i + 2;
cout << "Right child: " << bTree[i] << endl;
}
else if (bTree[i] > x)
{
i= 2*i + 1;
cout<< "Left child: " << bTree[i] << endl;
}
}
}