Segmentation Fault in C program that is doing a binary search of an array -
in c program, doing binary search of array read in data.txt
first trying scan in data.txt array sec using insertion sort algorithm sort 3rd doing binary search of array. new c , have no thought code gone wrong, give thanks help in letting me know doing incorrect.
#include <stdio.h> #include <stdint.h> #include <stdlib.h> int main () { int v, t, low, high, mid, search; int n = 20, array[20]; int p = 0; file *infile; infile = fopen(“data.txt”,”r”); while(!feof(infile)) { fscanf(infile,”%d”, &array[p]); p++; } (p = 0; p < n; p++) { scanf("%d", &array[p]); } (p = 1 ; p <= n - 1; p++) { v = p; while ( v > 0 && array[v] < array[v-1]) { t = array[v]; array[v] = array[v-1]; array[v-1] = t; v--; } } (p = 0; p <= n - 1; p++) { printf("%d\n", array[p]); printf("please come in value (-1 = done)>\n"); scanf("%d",&search); low = 0; high = n - 1; mid = (low+high)/2; while( low <= high ) { if ( array[mid] < search ) low = mid + 1; else if ( array[mid] == search ) { printf("%d located @ %d in array.\n", search, mid+1); break; } else high = mid - 1; mid = (low + high)/2; } if ( low > high ) printf("-1\n"); homecoming 0; }
1). initialize n value 2). after insertion sort (i guess) there printf print array. add together braces after printf. if u dont programme keeps running(unless come in char breaks prog) may work cause returning before can loop still major problem..
c arrays sorting binary segmentation-fault
No comments:
Post a Comment