在程序中得到错误的输出

时间:2018-09-06 07:25:39

标签: c arrays output

我最近写了一个程序,该程序输出错误,我丝毫不知道为什么。

此程序检查以下内容:给定一些“ k”(值)以及两个数组A和B,检查是否有一些“ x ”属于数组 A 和属于 B 的' y ',因此 xk = y

这是我的代码:

        #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; 
            }

对于大小为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 x-y=k

而预期的输出是  there are some x which belongs to A and y which belongs to B such that x-y=k,因为-例如,有14个属于A,11个属于B,所以14-11 = 3。

1 个答案:

答案 0 :(得分:1)

<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>CKEditor 5 – Classic editor</title> <script src="https://cdn.ckeditor.com/ckeditor5/11.0.1/classic/ckeditor.js"></script> </head> <body> <h1>Classic editor</h1> <textarea name="content" id="editor"> &lt;p&gt;This is some sample content.&lt;/p&gt; </textarea> <script> ClassicEditor .create( document.querySelector( '#editor' ) ) .catch( error => { console.error( error ); } ); </script> </body> </html>函数中,定义这些变量时,只需设置一次findValuesForDifference()low的值。

您需要在主循环的每次迭代中重置其值,否则您的二进制搜索将只运行一次:

high