Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Array or Pointer Error

Posted on 2011-02-22
5
Medium Priority
?
503 Views
Last Modified: 2012-05-11
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?
0
Comment
Question by:Member_2_4213139
  • 2
  • 2
5 Comments
 
LVL 9

Accepted Solution

by:
AriMc earned 2000 total points
ID: 34955271
Your list variable in binarySearch function is not an array. You would probably want to pass your myList as a parameter from main instead of declaring a scalar in binarySearch.

0
 
LVL 9

Expert Comment

by:AriMc
ID: 34955336
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

0
 
LVL 37

Expert Comment

by:TommySzalapski
ID: 34955370
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[])
0
 
LVL 37

Expert Comment

by:TommySzalapski
ID: 34955392
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.
0
 
LVL 11

Expert Comment

by:Deepu Abraham
ID: 34955402
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

0

Featured Post

Ask an Anonymous Question!

Don't feel intimidated by what you don't know. Ask your question anonymously. It's easy! Learn more and upgrade.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Okay. So what exactly is the problem here? How often have we come across situations where we need to know if two strings are 'similar' but not necessarily the same? I have, plenty of times. Until recently, I thought any functionality like that wo…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

963 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question