Hello all. I'm having trouble merging arrays here. So first I allow the user to enter the size of the array, followed by the elements. Then I use a bubble sort to get them in ascending order. And I do this for two sets of values. But I can't seem to get them to merge. Please look at my code and help if you can ...
I keep getting hexidecimal digits for the output of the final 3 numbers. I think its because the values for each half to be merged are less than the counter for the loop, and so when it loops they have no value in that array element.Code:#include<stdio.h>
#include<conio.h>
#define SIZE 4
int bubble_sort(int array[],int w);
int merge(int array1[], int w1, int array2[], int w2);
int main(void)
{
/*Accept values for Values1*/
int array1[10],val_1,i,x;
printf("Please enter the array size of Values1.\n");
scanf("%d",&i);
printf("Please enter the elements of Values1.\n");
for(x = 0; x < i; x++)
{
scanf("%d",&val_1);
array1[x] = val_1;
}
bubble_sort(array1,i);
/*Accept values for Values2*/
int array2[10],val_2,y,z;
printf("Please enter the array size of Values2.\n");
scanf("%d",&y);
printf("Please enter the elements of Values2.\n");
for(z = 0; z < y; z++)
{
scanf("%d",&val_2);
array2[z] = val_2;
}
bubble_sort(array2,y);
printf("The sorted elements of Values1 are:");
for(x = 0; x < i; x++)
{
printf("%d",array1[x]);
}
printf("\n");
printf("The sorted elements of Values2 are:");
for(z = 0; z < y; z++)
{
printf("%d",array2[z]);
}
merge(array1,i,array2,y);
getch();
}
int bubble_sort(int array[],int w)
{
int c1,c2,hold;
for (c1 = 1; c1 < w; c1++)
{
for (c2 = 0; c2 < w-1; c2 ++)
{
if (array[c2] > array[c2+1])
{
hold = array[c2];
array[c2] = array[c2+1];
array[c2+1] = hold;
}
}
}
}
int merge(int array1[], int w1, int array2[], int w2)
{
int f,c, array3[20];
for(f = 0; f < (w1+w2) ; f++)
{
if(array1[f] < array2[f])
{
array3[f] = array1[f];
array3[f+1] = array2[f];
}
else
{
array3[f] = array2[f];
array3[f+1] = array1[f];
}
}
printf("The final values are:\n");
for(f = 0; f < (w1+w2); f++)
{
printf("%d ",array3[f]);
}
}