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 November 9th, 2004, 04:05 PM
kerrigan88 kerrigan88 is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Nov 2004
Posts: 1 kerrigan88 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
binary search

Alright, mine's a long one. The error I keep on getting is bin_search local funtion defs are illegal.
Think there is something worng with my binary search. Anyone see what's worng with it?


#include
<iostream>
#include <ctime>


usingnamespace std;

int bin_search(int a[], int n, int& target);



// sel sort

void selsort(int a[], int size)

{

int i, j, maxpos, temp;

for (i=0; i < size; i++)

{

maxpos = i;

for (j=maxpos+1; j < size; j++)

if (a[j] > a[maxpos])maxpos = j;

temp = a[i];

a[i] = a[maxpos];

a[maxpos] = temp;

}



}

int main()

{



int A[150];

int target;

int num_searches = 0;

int suc_search = 0;

int tests = 0;

srand((
unsigned)time(NULL));

int random_integer;



// random nums

{

for (int i = 0; i < 150; i++)

A[i] = (rand()%200)+1;

// sorts

selsort(A,150);

for (int i = 0; i < 200; i++)



{

target = (rand()%200)+1;

num_searches++;

if (bin_search(A, 150, target)!=-1)

suc_search++;
// sucessfull searches

}

int bin_search (int a[], int n, int& target)

{

// Precondition: array a is sorted in ascending order

int first = 0;

int last = n - 1;

int mid = (first + last) / 2;

while ((a[mid] != target) && (first <= last))

{

tests++;

if (target < a[mid])

last = mid - 1;

else

first = mid + 1;

mid = (first + last) / 2;

}

if (a[mid] == target) return mid;

return -1; // target not found

}

int report (double num_searches, double suc_search, double tests);

{

cout <<"there are ["<< num_searches<<"] num of searches" << endl;

cout <<"there are ["<< suc_search<<"] num of sucessfull searches" << endl;

cout <<"there are ["<< suc_search/num_searches<<"] num of average searches" << endl;

cout <<"there are ["<< tests/200<<"] percent of sucessfull searches" << endl;

system("pause");

cout << "Press Enter to exit." << endl;



}

}

}


Reply With Quote
  #2  
Old November 10th, 2004, 07:30 AM
kode_monkey kode_monkey is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jul 2003
Posts: 367 kode_monkey User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 7 m 21 sec
Reputation Power: 6
If you are going to post code like this please use the code tags provided to maintain your formatting.

I wrote a binary search for someone a while back. Might be worth looking back through the C/C++ forum for it and comparing it with yours.

-KM-

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingC/C++ Help > 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 2 hosted by Hostway