Zelaron Gaming Forum  
Stats Arcade Portal Forum FAQ Community Calendar Today's Posts Search
Go Back   Zelaron Gaming Forum > The Zelaron Nexus > Science and Art > Tech Help

 
 
Thread Tools Display Modes

 
Binary Search
Reply
Posted 2004-12-04, 01:14 PM
I am writing a binary search that the user can input data that should be on a text file. Then I need to test the function using the sorted arrays by testing on these following numbers
12 30 33 81 99 85 28 24 25
24 26 27 9 41 32 35 23 39
43 41 46 56 62 8 20 22 18
or
generate a rand() at least 25

Code:
#include <stdio.h>
#define DESIRED_VAL 44
#define ARR_SIZE 50
int binsrch (int *arr, int lb, int up);
main()
{
 int arr [ARR_SIZE], i,j, lower, upper, index;
 for (i=0;i<ARR_SIZE; i=i+1)
  arr[i]= 2*i;
 printf ("Desired Value = %d\n", DESIRED_VAL);
 lower = 0;
 upper = ARR_SIZE-1;
 index = binsrch (arr, lower, upper);
 if (index > 0)
  printf ("the number %d was found at index %d\n", DESIRED_VAL, index);
 else
  printf ("the number %d was not found\n", DESIRED_VAL);
}

int binsrch (int *arr, int lb, int up)
{
 int mid;
 int index;
 mid = (up+lb)/2;
 if (lb>up)
  return (-1);
 printf ("looking in position %d\n", mid);
 if (arr[mid]==DESIRED_VAL)
  return (mid);
 if (arr[mid]<DESIRED_VAL)
 {
  return (binsrch (arr, mid+1, up));
 }
 else
 {
  index = binsrch(arr, lb, mid-1);
  return (index);
 }
}
Old
Profile PM WWW Search
deadlock75 is neither ape nor machine; has so far settled for the in-betweendeadlock75 is neither ape nor machine; has so far settled for the in-between
 
deadlock75
 
 

Bookmarks

« Previous Thread | Next Thread »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 

Posting Rules [Forum Rules]
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump


All times are GMT -6. The time now is 08:08 PM.
'Synthesis 2' vBulletin 3.x styles and 'x79' derivative
by WetWired the Unbound and Chruser
Copyright ©2002-2008 zelaron.com
Powered by vBulletin® Version 3.8.2
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
This site is best seen with your eyes open.