|
|
|
|
Posted 2004-12-01, 02:07 PM
|
|
|
|
Right now I am completely stuck on this the program works fine but I do not know how to input data that should be on a text file. And also test the function using the sorted arrays like quicksort, both random array of integers and the posted data.
#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);
}
}
|
|
|
|
|
|
|
|
|
|
|