HI, everyone. I am having trouble getting the index of the location of the search number to print out. This is my program. Any help would be greatly appreciated!

#include<iostream.h>
#include<cmath>
void sortit(int x[], int n)
{
int temp,i,j;
for(i=0;i<n;i++)
for(j=i;j<n;j++)
if(x[i]>x[j])
{
temp=x[i];
x[i]=x[j];
x[j]=temp;
}
}
void bsearch(int x[], int max, int searchnum)
{
int mid;
int high=0;
int size;
int low = size-1;
int location = -1;
int found;
while(high <=low && found==false)
{
mid=(high + low)/2;
if(x[mid]==searchnum)
location=mid;
found=true;
else if(searchnum>>x[mid])
high=(mid + 1);
else
low= (mid - 1);
}
}
main()
{
int num[11];
int max=10;
int i,mid,x,location;
int searchnum;
int found;
for(i=0;i<11;i++)
{
cin>>num[i]; //
}
sortit(num,max);
cout<<"The list sorted is: "<<endl;
for(int i=0;i<max;i++)
cout<<num[i]<<"\n";
while(cin>>searchnum)
{
bsearch(num,max,searchnum);
}
cout<<"The number is " <<searchnum<<endl;
if(found=true)
cout<<"The number was found at location" <<location<<endl;
else cout<<"Number not found" <<"\n";
}

PLEASE FEEL FREE TO MAKE ANY CORRECTIONS IF NEEDED

Recommended Answers

All 6 Replies

Member Avatar for iamthwee

Do you mean like a binary search tree? If so you need to work on the datastructure.

hi, try with thi code. i suggest to look carefully at changes made and try to sortout reason behind changes.

#include<iostream>
using namespace std;
void sortit(int x[], int n)
{
int temp,i,j;
for(i=0;i<n;i++)
for(j=i;j<n;j++)
if(x[i]>x[j])
{
temp=x[i];
x[i]=x[j];
x[j]=temp;
}
}

bool bsearch(int x[], int max, int searchnum, int &location)
{
int mid;
int high=0;
int size = max;
int low = size-1;
//int location = -1;
bool  found=0;
while(high <=low && found==false)
{
mid=(high + low)/2;
if(x[mid]==searchnum){
	location=mid;
	found=true;
	break;
}
else if(searchnum >x[mid])
high=(mid + 1);
else
low= (mid - 1);
}
return found;
}


main()
{
int num[11];
int max=10;
int i,mid,x,location=-1;
int searchnum;
bool  found;
for(i=0;i<11;i++)
{
cin>>num[i]; //
}
sortit(num,max);
cout<<"The list sorted is: "<<endl;
for(int i=0;i<max;i++)
cout<<num[i]<<"\n";
while(cin>>searchnum)
{
	found = bsearch(num,max,searchnum,location);
	break;
}
cout<<"The number is " <<searchnum<<endl;
if(found=true)
cout<<"The number was found at location :: " <<location+1<<endl;
else cout<<"Number not found" <<"\n";
}

Thanks very much! I still have a problem though, it is only printing out one of the search numbers instead of 4. Any help?

hi, try with thi code. i suggest to look carefully at changes made and try to sortout reason behind changes.

#include<iostream>
using namespace std;
void sortit(int x[], int n)
{
int temp,i,j;
for(i=0;i<n;i++)
for(j=i;j<n;j++)
if(x[i]>x[j])
{
temp=x[i];
x[i]=x[j];
x[j]=temp;
}
}
 
bool bsearch(int x[], int max, int searchnum, int &location)
{
int mid;
int high=0;
int size = max;
int low = size-1;
//int location = -1;
bool  found=0;
while(high <=low && found==false)
{
mid=(high + low)/2;
if(x[mid]==searchnum){
    location=mid;
    found=true;
    break;
}
else if(searchnum >x[mid])
high=(mid + 1);
else
low= (mid - 1);
}
return found;
}
 
 
main()
{
int num[11];
int max=10;
int i,mid,x,location=-1;
int searchnum;
bool  found;
for(i=0;i<11;i++)
{
cin>>num[i]; //
}
sortit(num,max);
cout<<"The list sorted is: "<<endl;
for(int i=0;i<max;i++)
cout<<num[i]<<"\n";
while(cin>>searchnum)
{
    found = bsearch(num,max,searchnum,location);
    break;
}
cout<<"The number is " <<searchnum<<endl;
if(found=true)
cout<<"The number was found at location :: " <<location+1<<endl;
else cout<<"Number not found" <<"\n";
}

may be this code will help u more.

// binary search.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"

#include<iostream>
using namespace std;
void sortit(int x[], int n)
{
int temp,i,j;
for(i=0;i<n;i++)
for(j=i;j<n;j++)
if(x[i]>x[j])
{
temp=x[i];
x[i]=x[j];
x[j]=temp;
}
}

bool bsearch(int x[], int max, int searchnum, int &location)
{
int mid;
int high=0;
int size = max;
int low = size-1;
//int location = -1;
bool  found=0;
while(high <=low && found==false)
{
mid=(high + low)/2;
if(x[mid]==searchnum){
	location=mid;
	found=true;
	break;
}
else if(searchnum >x[mid])
high=(mid + 1);
else
low= (mid - 1);
}
return found;
}


main()
{
int num[11];
int max=10;
int i,mid,x,location=-1;
int searchnum;
bool  found;
for(i=0;i<11;i++)
{
cin>>num[i]; //
}
sortit(num,max);
cout<<"The list sorted is: "<<endl;
for(int i=0;i<max;i++)
	cout<<num[i]<<"\n";
cout<<"Enter a number to search	:: ";
while(cin>>searchnum)
{
	found = bsearch(num,max,searchnum,location);
	cout<<"The number is " <<searchnum<<endl;
	if(found==true)
		cout<<"The number was found at location :: " <<location+1<<endl;
	else cout<<"Number not found" <<"\n";
	cout<<"Enter 0 to exit, or anything to continue searching	:: ";
	cin>>searchnum ;
	if(searchnum == 0) {
		break;
	}
	cout<<"Enter a number to search	:: ";
	//break;
}
}

/*BINARY SEARCH PROGRAM BY SUMAIR IRSHAD*/
#include<stdio.h>
#include<conio.h>
void main(void)
{
clrscr();
int arr[]={1,3,5,7,9,11,13,15,17,19};
int i,target,start=0,end=10,mid;
printf("enter target");
scanf("%d",&target);
while(target!=arr[mid])
{
mid=(start+end)/2;
if(target==arr[mid])
{
break;
}
else if(target<arr[mid])
{
end=mid-1;
}

else if(target>arr[mid])
{
start=mid+1;
}
}
printf("target found at %dth location",mid+1);
getch();
}

/*BINARY SEARCH PROGRAM BY SUMAIR IRSHAD*/
#include<stdio.h>
#include<conio.h>
void main(void)
{
clrscr();
int arr[]={1,3,5,7,9,11,13,15,17,19};
int i,target,start=0,end=10,mid;
printf("enter target");
scanf("%d",&target);
while(target!=arr[mid])
{
mid=(start+end)/2;
if(target==arr[mid])
{
break;
}
else if(target<arr[mid])
{
end=mid-1;
}

else if(target>arr[mid])
{
start=mid+1;
}
}
printf("target found at %dth location",mid+1);
getch();
}

Where to start.

1) Use code-tags
2) use int main() not void main()!
3) This is a c++ forum not c

Chris

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.