Solved

Array or Pointer Error

Posted on 2011-02-22
5
458 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 500 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:DeepuAbrahamK
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Prime numbers are natural numbers greater than 1 that have only two divisors (the number itself and 1). By “divisible” we mean dividend % divisor = 0 (% indicates MODULAR. It gives the reminder of a division operation). We’ll follow multiple approac…
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 goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

821 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