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