M
Mik0b0
Hallo everybody,
my problem is: there are two single-dimension arrays, longer[M] and
shorter[N], every array is organized in ascending order. We need to
build a new array out of two. This is what I wrote:
#include<stdio.h>
#define M 8
#define N 5
main()
{
int big[M]={1,2,5,8,10,23,45,56};
int small[N]={3,7,11,12,100};
int new[M+N];
int i=0,k=0,l=0;
while(l<(N+M))
{
if(small[k]<big)
{
new[l]=small[k];
k++;
l++;
}
else
{
new[l]=big;
i++;
l++;
}
}
for(l=0;l<(M+N);l++)
printf("%d ",new[l]);
printf("\n");
}
The output is:
[Mike@localhost drills]$ ./153
1 2 3 5 7 8 10 11 12 23 45 56 8
Why is the last number wrong?
Thanks for your attention!
my problem is: there are two single-dimension arrays, longer[M] and
shorter[N], every array is organized in ascending order. We need to
build a new array out of two. This is what I wrote:
#include<stdio.h>
#define M 8
#define N 5
main()
{
int big[M]={1,2,5,8,10,23,45,56};
int small[N]={3,7,11,12,100};
int new[M+N];
int i=0,k=0,l=0;
while(l<(N+M))
{
if(small[k]<big)
{
new[l]=small[k];
k++;
l++;
}
else
{
new[l]=big;
i++;
l++;
}
}
for(l=0;l<(M+N);l++)
printf("%d ",new[l]);
printf("\n");
}
The output is:
[Mike@localhost drills]$ ./153
1 2 3 5 7 8 10 11 12 23 45 56 8
Why is the last number wrong?
Thanks for your attention!