I'm still getting compile errors. Part of my problem with this program the search code were given to us. If I wrote the code myself I would be easier. This is what I have:
Code:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int BIN_NUM, SEQ_NUM;
int arraycpy(int *a, int *b);
int compare (void *a, void *b); /* function prototype */
int bin_search (int *data, int n, int key);
int seq_search(int *data, int n, int key);
int arraycpy(int *a, int *b) {
int x;
x=0;
while((a[x]=b[x])!='\0') {
x++;
}
}
int main() {
int x, y, z, a, b, ia[1000], ib[1000], key;
srand(time(NULL)); /* seed random # generator */
for(x=0; x<1000; x++) { /* select 1000 numbers */
y=rand()%1000+1; /* generate random numbers from 1-1000 */
ia[x]=y;
}
printf("\nEnter an integer to search the array or -1 to quit:\n");
scanf("%d", &key);
while(key!=-1) {
a=bin_search(ib, ia, key);
b=seq_search(ia, ib, key);
arraycpy(ia, ib);
qsort(ia, 1000, sizeof(int), compare);
printf("%d", z);
printf("\nEnter an integer to search the array or -1 to quit:\n");
scanf("%d", &key);
}
return 0;
}
int compare(void *a, void *b) { /* function to sort array */
return *(int*)a-*(int*)b;
}
int bin_search (int *data, int n, int key) {
int found, midpoint, first, last, BIN_NUM;
found = 0;
first = 0;
BIN_NUM = 0;
last = n - 1;
while ( ( first <= last ) && ! found ) {
midpoint = (first + last) / 2;
if ( data[midpoint] == key )
found = 1;
else if ( data[midpoint] > key )
last = midpoint - 1;
else
first = midpoint + 1;
BIN_NUM++;
}
if ( found )
return (midpoint);
else
return (-1);
}
int seq_search(int *data, int n, int key) {
int found, i, SEQ_NUM;
i = 0;
found = 0;
SEQ_NUM = 0;
while ( ! found && ( i < n ) ) {
if ( data[i] == key )
found = 1;
else
i++;
SEQ_NUM++;
}
if ( found )
return (i);
else
return (-1);
}