-1

C/C++

I'm using 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.


    #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.


    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

I think the problem is in the 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;
    
    }

Shamsul Arefin
  • 661
  • 7
  • 15
Alay Shah
  • 11
  • 2
  • 5
    `int a[x];` and your other array declarations are using uninitialized variables for their size. You need to initialize or assign those variables a proper value first. You should also choose C or C++ because variable length arrays are not standard in C++. – Retired Ninja Apr 06 '21 at 18:45
  • VSCode is an editor - it cannot "run code", and the source file test.c is not executed, so it cannot "stop working". CX is a compiled language, you compile and link the source to produce an executable. It is the executable that is run (by the OS not VSCode). Use a debugger to debug your code. – Clifford Apr 06 '21 at 19:12
  • `int a[x];` -- This is not C++ if `x` is not a constant value. In C++, variable length arrays are done using `std::vector`. This `std::vector a(x);` is valid C++. This is also the reason why you should choose the appropriate language. There is no such language as **C/C++** (you specified this in your post). – PaulMcKenzie Apr 06 '21 at 19:17

1 Answers1

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 to 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]; without initializing the values of x & y. One possible solution is initializing the values of 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 a macro MAX, assuming that the values of x and y less than MAX. Caution: this process is mot memory efficient.

#define MAX 100000
int main(void) {
    int x, y, i, j;
    int a[MAX];
    int b[MAX];
    int c[MAX + MAX];
    .....
    .....
    .....
}
Shamsul Arefin
  • 661
  • 7
  • 15
  • Thank you for your answer, sir. As I've mentioned in the question "When I run this program file it stopped immediately." I didn't mention any errors, because I wasn't getting one. So my problem was the code wasn't running at all. I saw this code on Coursera. Professor IRA POHL was using this code to teach merge sort. That's why I tagged Merge-sort. But I was not able to run it. But your code helped me to run the code. I tried to initialize Arrays after initializing the x & y. – Alay Shah Apr 08 '21 at 18:20
  • @AlayShah The professor illustrated the way of merging two sorted arrays into a single array. Because you need to merge two arrays into a single array while implementing actual Merge sort. your merge() function that will be called from another function called mergesort(). Go ahead and finish the next video (merge code example 2). You will see. – Shamsul Arefin Apr 08 '21 at 20:21