binary search

please do the necessary corrections in the program below as when i put a different no than in the array the program hangs
program:
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
main()
{
clrscr();
int x=0,y=0,l=0,m;
int ar[5];
for(x=0;x<=4;x++)
{
cin>>ar[x];
}
for(x=0;x<=4;x++)
{
for(y=0;y<=4;y++)
if(ar[x]<=ar[y])
{
m=ar[x];
ar[x]=ar[y];
ar[y]=m;
}
}
cout<<"ans";
for(x=0;x<=4;x++)
{
cout<<ar[x];
}
int n;
cout<<"enter";
cin>>n;
int a=5,g,k,mid;
l=5;k=0;
do
{

if(l%2==0)
mid=(l/2);
else
mid=(l/2+0.5);


if(n<=ar[mid-1])
{
if(n<ar[mid-1])
a=a-1;
if(n==ar[mid-1])
{
cout<<"no is at pos"<<mid;k=1;
break;
}
}
else
if(n>=ar[mid-1]);
{
if(n>ar[mid-1])
a=a+1;
if(n==ar[mid-1])
{
cout<<"no is at pos"<<mid;k=1;
break;
}
}
l=a;
}while(mid-1!=0||mid-1!=4);
if(k==0)
cout<<"no number";
getch();
}

thanks
Last edited on
Not a homework service, sorry.

If you have questions, we'll help answer them.
Working code :

#include<iostream>
using namespace std;
main()
{
int x=0,y=0,l=0,m;
int ar[5];

cout << "Enter " << endl;
for(x=0;x<=4;x++) {
cin>>ar[x];
}
for(x=0;x<=4;x++) {
for(y=0;y<=4;y++)
if(ar[x]<=ar[y])
{
m=ar[x];
ar[x]=ar[y];
ar[y]=m;
}
}
cout<< endl << "ans ";
for(x=0;x<=4;x++) {
cout<< " " << ar[x];
}
int n;
cout << endl << "Enter ";
cin>>n;
int a=5,g,k,mid;
l=5;k=0;
int low,high;
low=0,high=4;
do
{
mid=(high-low)%2;
if (mid)
mid=(high-low)/2;
else
mid=1+(high-low)/2;
cout << "mid:" << mid << " low:" << low << " high:" << high << endl;

if(n==ar[mid]) {
cout<<"no is at pos"<<mid;k=1;
break;
}
else if(n<ar[mid]) {
a=mid-1;
high=mid-1;
} else {
low=mid+1;
}

}while( low < high);

if(k==0)
cout<<"no number";
}
jysonia wrote:
Working code :


jysonia, how does this help the OP?

for 1 you didn't include code tags,

secondly consider this:

I enroll into Uni, I decide that instead of actually going to lectures I'm going to go into town to watch movies, hang out with friends, and smoke bongs in the park....

When I get any homework or assignment I come here and post asking for the solution.
I travel my whole way through uni, doing this... ignoring classes as simply getting other to do my homework for me.

Then I graduate, same time as you. Turns out we are both going for the same job. However while you know what your doing I don't have a clue. I get the job, you don't ...
Why? Because I spent all my uni days being social and more personable, while you sat on forums doing other peoples assignments for them.

sound stupid ? fact is it's not that far from the truth... Doing other peoples work for them does not turn them into programmers, it doesn't promote an analytical mind to try and decipher problems for themselves.

This is why we are more than willing to help people on the forum with specific questions and problems, but we don't do the work for them. For all you know the OP just grabbed the first example he saw off the web, realized it didn't work and came here and posted.
hey guys.. put your code in code tags. [ code ] [ /code ]
note no spaces.. for syntax highlighting..
well to really illustrate why it's stupid to accept help from people who don't listen to their peers, and to be a bit of a hypocrite. THIS IS A BUBBLESORT!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
void bubbleSort(int arr[], int n)
{
   bool swapped = true;
   int j = 0;
   int tmp;

   while (swapped)
   {
      swapped = false;
      j++;
      for (int i = 0; i < n - j; i++)
      {
         if (arr[i] > arr[i + 1])
         {
            tmp = arr[i];
            arr[i] = arr[i + 1];
            arr[i + 1] = tmp;
            swapped = true;
         }
      }
   }
}


notice mine makes sense, namely because it's in code tags, and secondly because it's cleaner.
Thirdly, because ISO C++ forbids declarations of main with no return type.

If there question from the OP were asked, I wrote this but for some reason it's not working... can someone please help me to figure out what I've done wrong....?

we would have then posted all the various typical responses pointing out what is wrong in the code, then you would come to the conclusion how to fix it. Same result, less swearing involved :D

you may want to read this: http://cplusplus.com/articles/how_to_ask/
Last edited on
argh!.. this always happen, someone's go and register in this forum just to ask for their homework and never goes back to help someone else in return,
That's ok, because newbies giving help to newbies is like the blind leading the blind.
Bubble Sort? Binary Search?
Topic archived. No new replies allowed.