Link to home
Start Free TrialLog in
Avatar of Member_2_4213139
Member_2_4213139Flag for United States of America

asked on

Array or Pointer Error

This code is giving errors:

#include <iostream>

using namespace std;

int binarySearch(int);

int main()
{
      int myList[10] = {1,7,13,17,24,38,45,50,100,1000};

      int num;
      int loc;

      cout << "The list you entered is: " << endl;
      cout << myList;

      cout << "Enter search item: ";
      cin >> num;
      cout << endl;

      binarySearch(num);
      loc = binarySearch(num);

      if (loc != -1)
            cout << "Item found at position " << loc << endl;
      else
            cout << "Item not in the list" << endl;

      system("pause");
      return 0;
}

int binarySearch(int item)
{
      int list;
      int length = 10;
      
      int first = 0;
      int last = length - 1;
      int mid;

      bool found = false;

      while (first <= last && !found)
      {
            mid = (first + last) / 2;

            if (list[mid] == item)
               found = true;
            else if(list[mid] > item)
                last = mid - 1;
            else
                  first = mid + 1;
      }

      if (found)
            return mid;
      else
            return -1;
}

The errors are, specifically, at: the "list[mid]" section of the "binarySearch" function.  There is an "array or pointer error" at those two spots.  Could anyone tell me what is wrong and how to fix it?
ASKER CERTIFIED SOLUTION
Avatar of AriMc
AriMc
Flag of Finland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Maybe something like this:


#include <iostream>

using namespace std;

int binarySearch(int, int *, int);

int main()
{
      int myList[10] = {1,7,13,17,24,38,45,50,100,1000};

      int num;
      int loc, i;

      cout << "The list you entered is: " << endl;
      for (i=0; i<sizeof(myList)/sizeof(int); i++)
         cout << myList[i] << " ";
      cout << endl;

      cout << "Enter search item: ";
      cin >> num;
      cout << endl;

      binarySearch(num, myList, sizeof(myList)/sizeof(int));
      loc = binarySearch(num, myList, sizeof(myList)/sizeof(int));

      if (loc != -1)
            cout << "Item found at position " << loc << endl;
      else
            cout << "Item not in the list" << endl;

      system("pause");
      return 0;
}

int binarySearch(int item, int *list, int length)
{
      int first = 0;
      int last = length - 1;
      int mid;

      bool found = false;

      while (first <= last && !found)
      {
            mid = (first + last) / 2;

            if (list[mid] == item)
               found = true;
            else if(list[mid] > item)
                last = mid - 1;
            else
                  first = mid + 1;
      }

      if (found)
            return mid;
      else
            return -1;
}

Open in new window

Avatar of TommySzalapski
You may have noticed that cout << myList; isn't working right. You would need to put it in a loop and print them one at a time.

The error is because list is an integer in your function. You need to pass it in as an array. Something like
int binarySearch(int item, int list[])
AriMc, simple C++ programs are usually homework related. As per EE policy, for these we should try to give guidance not full solutions.
Congrats on hitting Master by the way.
Few errors in your code

I would first make this global though it is not a great idea always.


int myList[10] = {1,7,13,17,24,38,45,50,100,1000};

int main()
{
     

      int num;
      int loc;

      cout << "The list you entered is: " << endl;
	  for(int i=0;i<10;i++) //<---------introduce a loop here to show all the items in the array
      cout << myList[i] <<" ";
...
...
}

int binarySearch(int item)
{
//      int list; //<----------commented out
      int length = 10;
      
      int first = 0;
      int last = length - 1;
      int mid;

      bool found = false;

      while (first <= last && !found)
      {
            mid = (first + last) / 2;

            if (myList[mid] == item)<--------added this myList
               found = true;
            else if(myList[mid] > item)<--------added this myList
                last = mid - 1;
            else
                  first = mid + 1;
      }

      if (found)
            return mid;
      else
            return -1;
}

Open in new window