/* * Add mac to the list. We should really have some kind of tree * structure here, but the loop below is reasonably fast. */ static void addmac(const char *mac) { int i, slot; if (ncmds >= MAXCMDS) { errx(1, "Only %d known commands allowed", MAXCMDS); } /* Don't try to add it if it's already in the table. */ if (binsrch(mac, &slot) >= 0) { #ifdef DEBUG printf("binsrch(%s) -> already in table\n", mac); #endif /* DEBUG */ return; } #ifdef DEBUG printf("binsrch(%s) -> %d\n", mac, slot); #endif for (i = ncmds - 1; i >= slot; i--) { strncpy(knowncmds[i + 1], knowncmds[i], 2); } strncpy(knowncmds[slot], mac, 2); ncmds++; #ifdef DEBUG printf("after: %s %s %s %s %s, %d cmds\n", knowncmds[slot-2], knowncmds[slot-1], knowncmds[slot], knowncmds[slot+1], knowncmds[slot+2], ncmds); #endif }
/* * Add mac to the list. We should really have some kind of tree * structure here but this is a quick-and-dirty job and I just don't * have time to mess with it. (I wonder if this will come back to haunt * me someday?) Anyway, I claim that .de is fairly rare in user * nroff programs, and the loop below is pretty fast. */ static void addmac(char *mac) { char **src, **dest, **loc; if (binsrch(mac) >= 0) { /* it's OK to redefine something */ #ifdef DEBUG printf("binsrch(%s) -> already in table\n", mac); #endif return; } /* binsrch sets slot as a side effect */ #ifdef DEBUG printf("binsrch(%s) -> %d\n", mac, slot); #endif if (ncmds >= MAXCMDS) { printf("Only %d known commands allowed\n", MAXCMDS); exit(1); } loc = &knowncmds[slot]; src = &knowncmds[ncmds-1]; dest = src+1; while (dest > loc) *dest-- = *src--; *loc = malloc(strlen(mac) + 1); strcpy(*loc, mac); ncmds++; #ifdef DEBUG printf("after: %s %s %s %s %s, %d cmds\n", knowncmds[slot-2], knowncmds[slot-1], knowncmds[slot], knowncmds[slot+1], knowncmds[slot+2], ncmds); #endif }
/* * Add mac to the list. We should really have some kind of tree * structure here but this is a quick-and-dirty job and I just don't * have time to mess with it. (I wonder if this will come back to haunt * me someday?) Anyway, I claim that .de is fairly rare in user * nroff programs, and the register loop below is pretty fast. */ void addmac(char *mac) { char **src, **dest, **loc; if (binsrch(mac) >= 0){ /* it's OK to redefine something */ #ifdef DEBUG printf("binsrch(%s) -> already in table\n", mac); #endif /* DEBUG */ return; } /* binsrch sets slot as a side effect */ #ifdef DEBUG printf("binsrch(%s) -> %d\n", mac, slot); #endif loc = &knowncmds[slot]; src = &knowncmds[ncmds-1]; dest = src+1; while (dest > loc) *dest-- = *src--; if ((*loc = strdup(mac)) == NULL) err(1, "strdup"); ncmds++; #ifdef DEBUG printf("after: %s %s %s %s %s, %d cmds\n", knowncmds[slot-2], knowncmds[slot-1], knowncmds[slot], knowncmds[slot+1], knowncmds[slot+2], ncmds); #endif }
void binsrch(int array[],int num, int low, int high) { int mid; if(low>high){ printf("invalid!"); } else{ mid=(low+high)/2; if(array[mid]==num){ printf("found at the location %d",mid); exit(0); } else if(array[mid]>num){ binsrch(array,num,low,mid-1); } else{ binsrch(array,num,mid+1,high); } } }
static void checkknown(const char *mac) { if (eq(mac, ".")) return; if (binsrch(mac, NULL) >= 0) return; if (mac[0] == '\\' && mac[1] == '"') /* comments */ return; pe(lineno); printf("Unknown command: .%s\n", mac); }
int main(){ int a[50]; int n,i,key; printf("Enter the no. of elements: "); scanf("%d",&n); printf("Enter the elements: "); for(i=0;i<n;i++) scanf("%d",&a[i]); printf("Enter the element to search for: "); scanf("%d",&key); binsrch(a,key,n); return (0); }
int main(){ int array[100],i,n,num,low,high; printf("enter array size"); scanf("%d",&n); printf("enter the values"); for(i=0;i<n;i++) { scanf("%d",&array[i]); } low=0; high=n-1; printf("enter to search"); scanf("%d",&num); binsrch(array,num,low,high); }
int main() { /* Example usage */ int x[] = { 1, 2, 2, 2, 2, 3, 4, 5, 6, 7, 8, 9, 22, 13, 54321, 22223 }; printf("%d %d %d\n", binsrch(x, 2, 16), binsrch(x, 20, 16), binsrch(x, 54321, 16)); return 0; }