简体   繁体   English

当我在 VScode 上运行此代码时,它显示“test.c 已停止工作”。 我试图练习合并排序

[英]When I run this code on VScode, it shows “test.c has stopped working”. I was trying to Practice merge sort

C/C++ C/C++

I'm using VScode.我正在使用 VScode。 When I run this program file stopped immediately.当我运行这个程序文件时立即停止。

I have encountered this kind of problem before.我以前遇到过这种问题。 but it happened because I scanned an array without '&' addressing it.但它发生了,因为我扫描了一个没有“&”寻址的数组。

but this time I couldn't find the problem.但这次我找不到问题。

this function will print an array.这个 function 将打印一个数组。


    #include <stdio.h>
    void print_array ( int size, int data[], char *str) // this function will print array.
    {
        int i;
        printf("%s", str);
    
        for (i = 0; i < size; i++)
        {
            printf("%d\t", data[i]);
        }
    }

This function will take two sorted arrays, merge-sort them and it will give a sorted array.这个 function 将采用两个已排序的 arrays,对它们进行合并排序,它将给出一个排序数组。


    void merge(int a[], int b[], int c[], int x, int y) //this function will merge-sort two sorted arrays.
    {
        int i = 0, j = 0, k = 0;
        
        while ( i < x && j < y)
        {
            if (a[i] < b[j])
            {
                c[k++] = a[i++];
            }
            else
                c[k++] = b[j++];
        }
        while (i < x)
        {
            c[k++] = a[i++];
        }   
        while(j < y)
        {
            c[k++] = b[j++];
        }
    }

This main function will take the array's elements, and perform all task defined in the above functions这个主要的 function 将获取数组的元素,并执行上述函数中定义的所有任务

I think the problem is in the int main() function.我认为问题出在int main() function 中。 Because the problem doesn't start to run at all.因为问题根本没有开始运行。


    int main (void)
    {
        int x, y, i, j;
        int a[x];
        int b[y];
        int c[x+y];
        printf("Enter size of first array: ");
        scanf("%d", &x);
        printf("\nEnter sorted elements of first array:\n");
        for(i = 0; i < x; i++)
        {
            scanf("%d", &a[i]);  //it will take elements of array. 
        }
        print_array( x, a, "\nFirst array\n"); //print the first array.
        printf("\n\n");
    
        printf("Enter size of second array: ");
        scanf("%d", &y);
        printf("\nEnter sorted elements of Second array:\n");
        for(j = 0; j < y; j++)
        {
            scanf("%d", &b[i]);
        }
        print_array( y, b, "\nSecond array\n"); //print second array.
        printf("\n\n");
    
        merge(a, b, c, x, y); /merge sort
    
        print_array( x+y, c, "\nSorted Data\n"); //print sorted data
        printf("\n\n");
    
        return 0;
    
    }

What are you actually trying to implement?您实际上要实施什么? If you are trying to implement Merge sort algorithm, this is not the way to implement Merge sort.如果您尝试实现合并排序算法,这不是实现合并排序的方法。

Your question is vague.你的问题很模糊。 You did not specify whether you got a runtime error or compile time error.您没有指定是否遇到运行时错误或编译时错误。

If you Copied your code correctly, I think you are getting a compilation error because a comment is missing / .如果您正确复制了代码,我认为您会收到编译错误,因为缺少注释/ Fix the comment.修正评论。 Edit the line merge(a, b, c, x, y); /merge sort编辑行merge(a, b, c, x, y); /merge sort merge(a, b, c, x, y); /merge sort to merge(a, b, c, x, y); //merge sort merge(a, b, c, x, y); /merge sort merge(a, b, c, x, y); //merge sort merge(a, b, c, x, y); //merge sort . merge(a, b, c, x, y); //merge sort

You may also get runtime error or undefined behavior because you declared three arraies int a[x] , int b[y] & int c[x+y];您还可能会遇到运行时错误或未定义的行为,因为您声明了三个数组int a[x] , int b[y] & int c[x+y]; without initializing the values of x & y .没有初始化x & y的值。 One possible solution is initializing the values of x & y .一种可能的解决方案是初始化x & y的值。

int main(void) {
    int x, y, i, j;
    printf("Enter size of first array: ");
    scanf("%d", &x);
    //declare array a[x] here 
    int a[x];
    printf("\nEnter sorted elements of first array:\n");
    for (i = 0; i < x; i++) {
        scanf("%d", &a[i]);  //it will take elements of array.
    }
    print_array(x, a, "\nFirst array\n"); //print the first array.
    printf("\n\n");

    printf("Enter size of second array: ");
    scanf("%d", &y);
    //declare array b[y] here 
    int b[y];
    printf("\nEnter sorted elements of Second array:\n");
    for (j = 0; j < y; j++) {
        scanf("%d", &b[i]);
    }
    print_array(y, b, "\nSecond array\n"); //print second array.
    printf("\n\n");

    //declare array c[x+y] here 
    int c[x + y];
    merge(a, b, c, x, y); //merge sort

    print_array(x + y, c, "\nSorted Data\n"); //print sorted data
    printf("\n\n");

    return 0;
}

Another solution of the last problem is to declare the array using amacro MAX , assuming that the values of x and y less than MAX .最后一个问题的另一个解决方案是使用MAX声明数组,假设xy的值小于MAX Caution: this process is mot memory efficient.注意:这个过程是 mot memory 高效的。

#define MAX 100000
int main(void) {
    int x, y, i, j;
    int a[MAX];
    int b[MAX];
    int c[MAX + MAX];
    .....
    .....
    .....
}

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

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