简体   繁体   English

在程序中得到错误的输出

[英]getting the wrong output in program

I recently wrote a program, which gives to a wrong output and I dont have the slightest idea why. 我最近写了一个程序,输出错误,我也不知道为什么。

This program checks the following: given some 'k' (value), and two arrays A and B, checking if there are some ' x ' which belongs to array A and ' y ' which belongs to B , so that xk=y . 该程序检查以下内容:给定一些“ k”(值)以及两个数组A和B,检查是否存在属于数组A的一些“ x ”和属于B的y ”,从而xk = y

Here is my code: 这是我的代码:

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

        int* buildArray(int size);
        int partition(int arr[], int low, int high);
        void quickSort(int *arr, int low, int high);
        int findValuesForDifference(int* A, int n, int* B, int m, int k);

        void main()
        {
            int n, m, k;
            int *A, *B;
            printf("please enter a number for the size of array A : ");
            scanf("%d", &n);
            printf("\nenter %d numbers for array A: ", n);
            A = buildArray(n);
            printf("please enter a number for the size of array B : ");
            scanf("%d", &m);
            printf("\nenter %d numbers for array A: ", m);
            B = buildArray(m);
            printf("\nplease enter a number for k: ");
            scanf("%d", &k);
            if (findValuesForDifference(A, n, B, m, k))
                printf("\nthere are some x which belongs to A and y which belongs to B such that x-y=k\n");
            else
                printf("\nthere are not any x which belongs to A and y which belongs to B such that x-y=k\n");

            free(B);
            free(A);
        }

        int findValuesForDifference(int* A, int n, int* B, int m, int k)
        {
            int low = 0, high = n - 1, middle, i;

            quickSort(A, low, high);

    /*using binary search sorted Array A, for each element of array B*/
            for (i = 0; i < m; i++)
            {
                while (low <= high)
                {
                    middle = (low + high) / 2;
                    if (k + B[i] == A[middle])
                        return 1;
                    else if (k + B[i] < A[middle])
                        high = middle - 1;
                    else
                        low = middle + 1;
                }
            }
            return 0;
        }

            int partition(int arr[], int low, int high)
            {
                int pivot = arr[high], i = (low - 1), j;

                for (j = low; j <= high - 1; j++)
                {

                    if (arr[j] <= pivot)
                    {
                        i++;    
                        swap(&arr[i], &arr[j]);
                    }
                }
                swap(&arr[i + 1], &arr[high]);
                return (i + 1);
            }

            void quickSort(int* arr, int low, int high)
            {
                int pivot;

                if (low < high)
                {
                    pivot = partition(arr, low, high);

                    quickSort(arr, low, pivot - 1);
                    quickSort(arr, pivot + 1, high);
                }
            }

           int* buildArray(int size)

           { int i; 
             int* arr = (int*)malloc(size * sizeof(int));       
             if (!arr) 
                 { printf("ERROR! Not enough memory!\n");  
                   exit(1); 
                 } 
             for (i = 0; i < size; i++) 
                  scanf("%d", &arr[i]); return arr; 
            }

For array A with size n=4 and the elements : 14 2 12 2 ,and array B with the size m=6 and the elements : 25 11 2 25 17 8 , and k=3 , I get the following wrong output 对于大小为n=4且元素为: 14 2 12 2数组A和大小为m=6且元素为: 25 11 2 25 17 8k=3数组B,我得到以下错误输出
there are not any x which belongs to A and y which belongs to B such that xy=k , there are not any x which belongs to A and y which belongs to B such that xy=k

while the expected output is there are some x which belongs to A and y which belongs to B such that xy=k , because - for instance, there are 14 which belongs to A and 11 which belongs to B so that 14-11=3. 而预期输出是there are some x which belongs to A and y which belongs to B such that xy=k ,因为-例如,有14个属于A的11和11属于B的,所以14-11 = 3 。

In your findValuesForDifference() function, you only set the values of low and high once, when defining these variables. 在您的findValuesForDifference()函数中,定义这些变量时,只需将lowhigh值设置一次。

You need to reset their values at every iteration of the main loop, otherwise your binary search will only work once: 您需要在主循环的每次迭代中重置它们的值,否则二进制搜索将只运行一次:

int findValuesForDifference(int *A, int n, int *B, int m, int k)
{
    int low, high, middle, i;

    quickSort(A, low, high);

    /* using binary search sorted Array A, for each element of array B */
    for (i = 0; i < m; i++) {
        low = 0;
        high = n - 1
        while (low <= high) {
            middle = (low + high) / 2;
            if (k + B[i] == A[middle])
                return 1;
            else if (k + B[i] < A[middle])
                high = middle - 1;
            else
                low = middle + 1;
        }
    }
    return 0;
}

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

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