0

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;
        }

      }

  }

1 Answers1

0

The logic in insertNum is not clear to me but the value of the i increases to a point where you are accessing the array using an out of bounds index.

Add the following line

  cout << "i: " << i << endl;

as the first statement in the for loop. When I did that, I got the following output:

 TEST 
i: 0
i: 0
i: 2
i: 0
i: 2
i: 6
i: 0
i: 2
i: 6
i: 14
i: 0
i: 2
i: 6
i: 14
i: 30
i: 0
i: 2
i: 6
i: 14
i: 30
i: 62
i: 0
i: 2
i: 6
i: 14
i: 30
i: 62
i: 126
i: 0
i: 2
i: 6
i: 14
i: 30
i: 62
i: 126
i: 254
i: 0
i: 2
i: 6
i: 14
i: 30
i: 62
i: 126
i: 254
i: 510
i: 0
i: 2
i: 6
i: 14
i: 30
i: 62
i: 126
i: 254
i: 510
i: 1022
i: 0
i: 2
i: 6
i: 14
i: 30
i: 62
i: 126
i: 254
i: 510
i: 1022
i: 2046
i: 0
i: 2
i: 6
i: 14
i: 30
i: 62
i: 126
i: 254
i: 510
i: 1022
i: 2046
i: 4094

It ended there with core dump in my setup.

I don't have a suggestion on how to fix it since it is not clear to me what you your goals are for the function.

Of course, you can add more lines of code to print diagnostic messages. For example, you can add

cout << "Inserting : " << x << endl;

before the for loop to figure out where things to start go wrong.


The while loop in stringToArray can be improved to:

while ( ss >> temp )
{
   insertNum(temp);
   count++;
}

See Why is iostream::eof inside a loop condition considered wrong? for further details.

R Sahu
  • 204,454
  • 14
  • 159
  • 270