Solved

Array or Pointer Error

Posted on 2011-02-22
5
456 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

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Focus not getting shifted out of  editbox 2 63
2008 AD Password hashing algorithm 3 137
designing in object programming 12 78
draw a Christmas tree by using a nested loop? 26 62
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
Iteration: Iteration is repetition of a process. A student who goes to school repeats the process of going to school everyday until graduation. We go to grocery store at least once or twice a month to buy products. We repeat this process every mont…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

778 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