C/C++ Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingC/C++ Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old April 12th, 2006, 10:03 AM
tosh89 tosh89 is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2006
Posts: 5 tosh89 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 9 m 36 sec
Reputation Power: 0
Outputting Binary search

I have a problem now..

i am doing binary search on a few inputs

but i can't seem to make it output whether the number is found ot not. below is my code for it.

#include <iostream>
using namespace std;

int binarysearch(int sortedarray[100], int first, int last, int key)
{
while (first <= last )
{
int mid = ( first + last ) / 2;
if (key> sortedarray[mid] )
first = mid + 1;
else if (key < sortedarray[mid])
last = mid - 1;
else return mid;
}
return - (first+1);
}

void main()
{
int nums, a[100], finds;
cout<<"Please enter no. of inputs.";
cin>> nums;
for (int i=0; i<nums; i++)
{
cout<<"Please enter your inputs.";
cin>>a[i];
}
cout<<"enter the number you want to find.";
cin>> finds;
binarysearch(a,a[0],a[nums],finds);
}


if anyone could help. really thanks alot.

Reply With Quote
  #2  
Old April 12th, 2006, 10:06 AM
tosh89 tosh89 is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2006
Posts: 5 tosh89 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 9 m 36 sec
Reputation Power: 0
i need to put my cout statement somewhere but i can't seem to figure out where

Reply With Quote
  #3  
Old April 12th, 2006, 10:43 AM
SunnyPalSingh SunnyPalSingh is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Mar 2006
Posts: 6 SunnyPalSingh User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 21 m 36 sec
Reputation Power: 0
See the changes
Code:
#include <iostream>
using namespace std;

int binarysearch(int sortedarray[100], int first, int last, int key)
{
while (first <= last )
{
int mid = ( first + last ) / 2;
if (key> sortedarray[mid] )
first = mid + 1;
else if (key < sortedarray[mid])
last = mid - 1;
else return mid;
}
return -1;
}

int main()
{
int nums, a[100], finds;
cout<<"Please enter no. of inputs.";
cin>> nums;
for (int i=0; i<nums; i++)
{
cout<<"Please enter your inputs.";
cin>>a[i];
}
cout<<"enter the number you want to find.";
cin>> finds;
int found=binarysearch(a,0,nums,finds);
if(found!=-1)
cout<<"Value found at "<<found<<"pos";
else
cout<<"Value not found ";

return 0;}

Reply With Quote
  #4  
Old April 12th, 2006, 11:01 AM
tosh89 tosh89 is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2006
Posts: 5 tosh89 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 9 m 36 sec
Reputation Power: 0
oh my. really thanks alot. grateful to u man.

now im gonna try to understand the code.

thanks again! =D

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingC/C++ Help > Outputting Binary search


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway
Stay green...Green IT