Solved

Array or Pointer Error

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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
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 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.

743 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now