Click here to Skip to main content
13,736,491 members
Rate this:
 
Please Sign up or sign in to vote.
See more:
When I debug the code here, it shows the error mentioned above.
int search(int A[],int N) {
    int item, i, beg = 0, mid, end;
    std::cout << "Enter the element";
    std::cin >> item;
    for (i = 0; i < N; i++) {
        end = N - 1;
        mid = (beg + end) / 2;
        if (A[mid] == item) {
            std::cout << "Search found";
            return item;
        } else if (A[mid] > item) {
            beg = mid + 1;
        } else end = mid - 1;
    }
    return -1;
}

int main() {
    int A[90],item,i,N,R;
    std::cout<<"Enter element to be searched";
    std::cin>>item;

    std::cout<<"Enter size of the array";
    std::cin>>N;

    std::cout<<"enter array";
    for(i=0;i<N;i++)
        std::cin>>A[i];

    R=search(int A[],int item);

    if(R!=1)
        std::cout<<"Success";
    else std::cout<<"Try again";
    return 0;
}

i totally have no idea regarding this.. This is a code for binary search,,

What I have tried:

declaring the array without the parameters
Posted 13-Oct-18 8:20am
Updated 13-Oct-18 12:18pm
v2
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 3

Quote:
When I debug the code here, it shows the error mentioned above.

This error message don't comes while debugging. It comes from compiler to tell you that there is a syntax error. You are hunting syntax errors.
Debugging comes after that when there is no more syntax error.
The debugger is meant to show you what your code is really doing, and you have to compare with what you expect it to do.
int search(int A[],int N) {
    int item, i, beg = 0, mid, end;
    std::cout << "Enter the element";
    std::cin >> item;
    for (i = 0; i < N; i++) { // wrong kind of loop
        end = N - 1;  // this should not be inside the loop
        mid = (beg + end) / 2;
        if (A[mid] == item) {
            std::cout << "Search found";
            return item;
        } else if (A[mid] > item) {
            beg = mid + 1;
        } else end = mid - 1;
    }
    return -1;
}

Once syntax errors are corrected, use the debuger and watch you code behave, you will be undoubtedly interested.
  Permalink  
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

Replace:
R=search(int A[],int item);
with:
R=search( A , item);
?

And replace;
if(R!=1)
with:
if(R!=-1)
?

And replace:
return item;
with:
return mid;

Also, does your teacher expect you to use recursion?
  Permalink  
v2
Comments
Radon & Radium 5 days ago
   
no,she doesnt
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

Please note, an ordered array is a precondition of the binary search.
To fix your code you might write:
#include <iostream>
#include <algorithm>

bool search(int a[], size_t size, int item)
{
  int beg = 0;
  int end = size-1;

  while (true)
  {
    if ( (beg + 1) >=  end)  
      return (item == a[end] || item ==a[beg]);
    int mid = (beg+end)/2;
    if ( item == a[mid])
      return true; 
    else if ( item < a[mid] )
      end = mid;
    else  
      beg = mid;
  }
}
enum { max_size = 90 };
int main()
{

  int a[max_size];

  int item;
  std::cout << "Enter the item to search for ";
  std::cin >> item;

  size_t size;
  std::cout << "Enter the array size ";
  std::cin >> size;

  if ( size > max_size)
  {
    std::cerr << "wrong input\n";
    return -1;
  }

  std::cout << "Enter the array elements\n";

  for (size_t n = 0; n< size; ++n)
    std::cin >> a[n];

  std::sort( &a[0], &a[size]);

  bool found  = search( a, size, item);
  std::cout << "The item is " << (found ? "found\n" : "not found\n");
}



However, in all programs but exercises, I would use the firepower of the standard C++ library:
#include <iostream>
#include <algorithm>
#include <vector>

int main()
{
  int item;
  std::cout << "Enter the item to search for ";
  std::cin >> item;

  size_t size;
  std::cout << "Enter the array size ";
  std::cin >> size;

  std::cout << "Enter the array elements\n";
  std::vector<int> v;
  v.resize(size);

  for (auto & x : v)
    std::cin >> x;

  sort(v.begin(), v.end());

  auto found =  binary_search(v.begin(), v.end(), item);
  std::cout << "The item is " << (found ? "found\n" : "not found\n");

}
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Cookies | Terms of Service
Web06-2016 | 2.8.180920.1 | Last Updated 13 Oct 2018
Copyright © CodeProject, 1999-2018
All Rights Reserved.
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100