#‎include‬ <stdio.h>
#include <conio.h>
#include <malloc.h>

‪#‎define‬ MAX1 5
#define MAX2 7

int* arr;
int* create (int size){
int *arr,i;
arr = (int *)malloc (sizeof(int)* size);/*note here size of (int )*size means size of int size no. of times,
size is the no of integers in the array*/
for (i=0;i<size;i++){
printf("Enter the element no. %d:",i+1);
scanf("%d",&arr[i]);
}
printf("\n");
return;
}

void sort (int *arr, int size){

int i,j,temp;
for (i=0;i<size;i++)
for(j=i+1;j<size;j++){
if(arr[i]>arr[j]){

temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
}

}

}

void display(int *arr, int size){
int i;
for(i=0;i<size;i++)
printf("%d\t",arr[i]);
printf("\n");

}

int* merge (int *a,int *b){
int *arr;
int i,k,j;
int size= MAX1+MAX2;
arr=(int *)malloc(sizeof(int)*size);

for(i=0,j=0,k=0; k<size; k++){
if(a[i]<b[j]){
arr[k]=a[i];
i++;
if (i==MAX1){
for (;j<MAX2;j++,k++)
arr[k]=b[j];
}

}
else{
arr[k]=b[j];
j++;
if (j==MAX2){
for(;i<MAX1;i++,k++){
arr[k]=a[i];

}

}

}

}

return arr;

}

int main(){
int *a,*b,*c;

printf("Enter elements for first array:\n");
a =create(MAX1);

printf("Enter elements for second array:\n");
b =create(MAX2);

sort(a,MAX1);
sort(b,MAX2);

printf("First array:\n");
display(a, MAX1);

printf("Second array:\n");
display(b, MAX2);

printf("After merging:\n");

c= merge (a,b);

display (c, MAX1 +MAX2);

return 0;

}

This program runs for a while and then stops executing, can anyone tell me the problem? Codeblocks

What are the errors codeblocks gives you when the program gets stuck or is it trapped in an infinite loop?
Try inserting print statements to find out where its getting stuck if your not getting any errors.

I had returned void in fn create where i should have returned arr.

There is one more logical error though.

#‎include‬ <stdio.h>
#include <conio.h>
#include <malloc.h>
‪#‎define‬ MAX1 5
#define MAX2 7

int* create (int size){
int *arr,i;
arr = (int *)malloc (sizeof(int)* size);/*note here size of (int )*size means size of int size no. of times,
size is the no of integers in the array*/
for (i=0;i<size;i++){
printf("Enter the element no. %d:",i+1);
scanf("%d",&arr[i]);
}
printf("\n");
return arr;
}
void sort (int *arr, int size){
int i,j,temp;
for (i=0;i<size;i++)
for(j=i+1;j<size;j++){
if(arr[i]>arr[j]){
temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
}
}
}
void display(int *arr, int size){
int i;
for(i=0;i<size;i++)
printf("%d\t",arr[i]);
printf("\n");
}
int* merge (int *a,int *b){
int *arr;
int i,k,j;
int size= MAX1+MAX2;
arr=(int *)malloc(sizeof(int)*size);
for(i=0,j=0,k=0; k<size; k++){
if(a[i]<b[j]){
arr[k]=a[i];
i++;
if (i==MAX1){
for (k++;j<MAX2;j++,k++)
arr[k]=b[j];
}
}
else{
arr[k]=b[j];
j++;
if (j==MAX2){
for(k++;i<MAX1;i++,k++){
arr[k]=a[i];
}
}
}
}
return arr;
}
int main(){
int *a,*b,*c;
printf("Enter elements for first array:\n");
a =create(MAX1);
printf("Enter elements for second array:\n");
b =create(MAX2);
sort(a,MAX1);
sort(b,MAX2);
printf("First array:\n");
display(a, MAX1);
printf("Second array:\n");
display(b, MAX2);
printf("After merging:\n");
c= merge (a,b);
display (c, MAX1 +MAX2);
return 0;
}

This code is working fine!

Please indent your code, otherwise it is VERY difficult to read! Example:

int* merge (int *a,int *b)
{
    int *arr;
    int i,k,j;
    int size= MAX1+MAX2;
    arr=(int *)malloc(sizeof(int)*size);
    for(i=0,j=0,k=0; k<size; k++)
    {
        if(a[i]<b[j])
        {
            arr[k]=a[i];
            i++;
            if (i==MAX1)
            {
                for (k++;j<MAX2;j++,k++)
                arr[k]=b[j];
            }
        }
        else
        {
            arr[k]=b[j];
            j++;
            if (j==MAX2)
            {
                for(k++;i<MAX1;i++,k++)
                {
                    arr[k]=a[i];
                }
            }
        }
    }
    return arr;
}

That makes errors a LOT easier to find! :-)

Edited 3 Years Ago by rubberman

Also note that I align the start/end brackets. Some don't like to do that, but I find it a lot easier to scope blocks of code. Style IS important. Readable code generally has a lot fewer errors, in my experience! After all, I only have 30+ years of it, and I still mentor engineers who have "senior" status in my organization... :-)

This article has been dead for over six months. Start a new discussion instead.