简体   繁体   English

总线错误:C二进制和线性搜索程序中的10

[英]Bus error : 10 in C binary and linear search program

#include <stdio.h>
#include <stdlib.h>

void arrayCopy(int fromArray[], int toArray[], int size){
    int i = 0;

    while(i < size){
        toArray[i] = fromArray[i];
        i++;
    }
}


void sort(int arr[], int size){
    int i, j;
    int temp;

    //simple sorting method comparing values next to each other
    //bigger value is moved further down the array
    //loops through "size" times
    for(i = 0; i < size; i++){
       for(j = 0; j < size - 1; j++){
            if(arr[j] > arr[j+1])
            {
                temp = arr[j];
                arr[j] = arr[j+1];
                arr[j+1] = temp;
            }
        }
    }
}

int linSearch(int arr[], int size, int target, int* numComparisons){
    int i;
    *numComparisons = 0;

    //if target is found loop will exit prematurely returning location in array
    //if target is not found loop will exit and return -1
    for(i = 0; i < size; i++){
        *numComparisons = *numComparisons + 1;  //tracks number of comparisons made
        if(arr[i] == target){
            return i;
        }
    }
    return -1;
} 

int binSearch(int arr[], int size, int target, int* numComparisons){
    int i;
    int first, last, mid;

    first = 0;
    last = size - 1;
    mid = (first + last) / 2;
    *numComparisons = 0;

    while(first <= last){
        if(arr[mid] == target){
            *numComparisons = *numComparisons + 1;
            return mid;
        } else if(arr[mid] < target){
            *numComparisons = *numComparisons + 1;
            first = mid + 1;
        } else{
            *numComparisons = *numComparisons + 1;
            last = mid - 1;
        }
    }

    return -1;


}

int main(){
    int i = 0;
    int j, k, l;
    int size = 0;
    int dynamicSize = 100;
    int val;
    int *arr1, *arr2;
    int *temp;
    int *numComparisons1, *numComparisons2;


    arr1 = (int*)malloc(dynamicSize * sizeof(int));

    printf("Please input your values.\n Terminate input with the value -999.\n");

    while(val != -999){
        //reads values and assigns them to array
        scanf("%d", &val);
        if(size >= dynamicSize){
            temp = (int*)malloc(dynamicSize * 2 * sizeof(int));
            for ( i = 0 ; i < dynamicSize ; i++){
                temp[i] = arr1[i];
            }
            free(arr1);
            arr1 = temp;
            dynamicSize = dynamicSize * 2;
        }
        arr1[i] = val;
        i = i + 1;
        size = size + 1;

    }
    val = 0;

     //Second array for binary search
     arr2 = (int*)malloc(dynamicSize * sizeof(int));
     arrayCopy(arr1, arr2, size);
     sort(arr2, size);

    printf("Please enter the number you would like to find.\n Terminate search with value -999.\n");
    while(val != -999){
        scanf("%d", &val);
        k = linSearch(arr1, size, val, numComparisons1);
        j = binSearch(arr2, size, val, numComparisons2);
        if(k == -1){
             printf("The value was not found.\n");
        }else{
             printf("Unsorted Array:\n The value is located at %d\n The number of comparisons made was %d\n", k, *numComparisons1);
             printf("Sorted Array:\n The value is located at %d\n The number of comparisons made was %d\n", j, *numComparisons2);
    }
  }

    return 0;
}

The program is supposed to take in user input and store it in an array. 该程序应该接受用户输入并将其存储在一个数组中。 When the user enters the value -999 the program will then create a second array, which is a copy of the first. 当用户输入值-999时,程序将创建第二个数组,该数组是第一个数组的副本。 The second array will then be sorted through the sort function for use in the binary search function. 然后,第二个数组将通过sort函数进行排序,以便在二进制搜索函数中使用。 The program then asks for the user to enter a value and that value will be checked for by the linear search and the binary search. 然后,程序要求用户输入一个值,并通过线性搜索和二进制搜索检查该值。 If the value is found the functions will return the number of comparisons made and where the value is in the array. 如果找到该值,则函数将返回所进行的比较次数以及值在数组中的位置。 If it is not found the program will return that the value was not found. 如果未找到,程序将返回未找到该值的情况。 This repeats until the user terminates the program by inputing the value -999. 重复此过程直到用户通过输入值-999终止程序。 The problem seems to be with the binary search. 问题似乎在于二进制搜索。 When included the program produces bus error : 10, but when I comment out the call to the binSearch function the program works fine. 当包含程序产生总线错误:10,但当我注释掉对binSearch函数的调用时,程序运行正常。

You should check for available space before storing the value into the array: 将值存储到数组之前,应检查可用空间:

int *arr1 = malloc(sizeof(int));
size_t dynamicSize = 1;
size_t size = 0;

// reads values and assigns them to array
while (scanf("%d", &val) == 1 && val != -999) {
    if (size >= dynamicSize) {
        arr1 = realloc(arr1, dynamicSize * 2 * sizeof(*arr1));
        dynamicSize = dynamicSize * 2;
    }
    arr1[size++] = val;
}

Your binary lookup function is incorrect: 您的二进制查找功能不正确:

  • You must recompute mid for each iteration in the loop 您必须在循环mid的每次迭代中重新计算mid
  • You do not compute the number of comparisons correctly 您没有正确计算比较次数
  • You have the classic arithmetic overflow bug in your computation of mid 在计算mid你有经典的算术溢出错误

Here is corrected version: 这是更正版本:

int binSearch(int arr[], int size, int target, int *numComparisons) {
    int first = 0;
    int last = size;
    int comp = 0;

    while (first < last) {
        int mid = first + (last - first) / 2;

        comp++;
        if (arr[mid] == target) {
            *numComparisons = comp;
            return mid;
        }
        comp++;
        if (arr[mid] < target) {
            first = mid + 1;
        } else {
            last = mid;
        }
    }
    *numComparisons = comp;
    return -1;   
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM