0

**

i am tring to merge two array but every time its display first array proparly but in second array

its only display last element....thx....#
**

#include<stdio.h>
#include<conio.h>
void main()
{
int a[10],b[10],i,j,c,d,e;
clrscr();
printf("how many number in first array=");
scanf("%d",&c);
printf("how many number in second array=");
scanf("%d",&d);
printf("\nenter %d number for fist array\n",c);
for(i=1;i<=c;i++)
    {
    scanf("%d",&a[i]);
    }
printf("\nenter %d number for second array\n",d);
for(j=1;j<=d;j++)
    {
    scanf("%d",&b[j]);
    }
e=c+d;
for(i=c+1;i<=e;i++)
    {
    for(j=1;j<=d;j++)
        {
        a[i]=b[j];
        }
    }
printf("\nafter merge two array");
for(i=1;i<=e;i++)
    {
    printf("\t%d",a[i]);
    }
getch();
}
2
Contributors
2
Replies
19
Views
3 Years
Discussion Span
Last Post by parth2911
1

try

for(i=c+1;i<=e;i++)
{
 a[i]=b[i-c];
}

instead of

for(i=c+1;i<=e;i++)
{
  for(j=1;j<=d;j++)
  {
       a[i]=b[j];  // this sets a[i]=b[d] because all settings a[i]=b[j] with j<d are overwritten by next j
  }
}

Please be aware that in C arrays are zero based i.e. array a begins with a[0] until a[9] with your declaration.
You should check that c, d and e are small enough to fit the array sizes.
Use better (i.e. self-explaining) names for your variables

Edited by Rolf_2

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.