Longer array out of two shorter arrays

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!
 
I

Ian Collins

Mik0b0 said:
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 main(void)

The output is:

[Mike@localhost drills]$ ./153
1 2 3 5 7 8 10 11 12 23 45 56 8
With which compiler? I see

1 2 3 5 7 8 10 11 12 23 45 56 100
 
R

Richard Heathfield

Mik0b0 said:
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)


You don't check to ensure that k < N or that i < M. Once k == N, you need to
stop loading from 'small' and copy the rest of 'big', unless i == M. Once i
== M, you need to stop loading from 'big', and copy the rest of 'small',
unless k == N.

Fix that, and I reckon your problem will vanish.
 
I

Ian Collins

Richard said:
Mik0b0 said:

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)



You don't check to ensure that k < N or that i < M.


Good catch.
 
W

woodstok

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()

main returns an int. Always.
{
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)


what happens in the above line if k > N or i > M?
{
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");


return 0;
}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!
 
R

Richard Heathfield

woodstok said:
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()

main returns an int. Always.

Yes, and:

main()

defines main as returning int. Always. (Except in C99thud[1].)
{
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)


what happens in the above line if k > N or i > M?


ITYM >= in each case.


[1] Q: What goes 99 thud?
A: A centipede with a wooden leg.

Q: What else goes 99 thud?
A: The latest C Standard going down like a lead balloon.
 
M

Mik0b0

Ian said:
Mik0b0 said:
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 main(void)

The output is:

[Mike@localhost drills]$ ./153
1 2 3 5 7 8 10 11 12 23 45 56 8
With which compiler? I see

1 2 3 5 7 8 10 11 12 23 45 56 100

I use gcc.
 
W

woodstok

C

CBFalconer

Mik0b0 said:
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:

You are allowed to use spaces. There are no prizes for obfuscation
by eliding them, and they are no longer on allocation. You are
also allowed to use reasonable indentation.
#include<stdio.h>
#define M 8
#define N 5
main()

int main(void)
{
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))
while ((i < N) && (k < M))
{
if(small[k]<big)
{
new[l]=small[k];
k++;
l++;
}
else
{
new[l]=big;
i++;
l++;
}
}

while (i < M) new[l++] = big[i++];
while (k < N) new[l++] = small[k++];
for(l=0;l<(M+N);l++)
printf("%d ",new[l]);
printf("\n");

return 0;
}

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?

Think about it. Your choice of names is shocking.
 
C

CBFalconer

CBFalconer said:
Mik0b0 said:
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:

You are allowed to use spaces. There are no prizes for obfuscation
by eliding them, and they are no longer on allocation. You are
also allowed to use reasonable indentation.
#include<stdio.h>
#define M 8
#define N 5
main()

int main(void)
{
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;
.... snip code ...

As an example, here is how I would have coded it. Note that now
things are defined in one place, and the heart can be freely
modified. The key is the use of sizeof to extract what you defined
as M and N, and the slaving of the *ix names to the names of the
variables they index. The #define for sz is a dog-standard way of
extracting the size of an array. It is a compile time constant, so
it does not complicate the emitted code. size_t is an unsigned
type, which can always measure the size of an array in bytes, and
is the type returned by sizeof and thus by sz. Think about why at
most only one of the final while loops will be executed.

You might want to look up the use of loop-invariants to deduce the
validity of code.

#include <stdio.h>
int main(void)
{
int big[] = {1, 2, 5, 8, 10, 23, 45, 56};
int small[] = {3, 7, 11, 12, 100};

#define sz(a) (sizeof a / sizeof a[0])

int new[sz(big) + sz(small)];
size_t bigix, smallix, newix;

bigix = smallix = newix = 0;
while ((bigix < sz(big)) && (smallix < sz(small)))
if (small[smallix] < big[bigix])
new[newix++] = small[smallix++];
else
new[newix++] = big[bigix++];

while (bigix < sz(big))
new[newix++] = big[bigix++];
while (smallix < sz(small))
new[newix++] = small[smallix++];

for (newix = 0; newix < (sz(big) + sz(small)); newix++)
printf("%d ", new[newix]);
putchar('\n');

return 0;
} /* main */

/* note that the index names are now tied to the arrays
that they index, and that the various sizes are tied
to the arrays that they describe. You should now be
able to modify the array initializations and have the
remainder of the code automatically adjust. */
 
D

deepak

How about this program?
We need to take seperate care for the last case.
In your code,in the last interation of the while i=8 and k=4. So they
are taking some
value which is not part of the array. ie big[8] which is not
initialized in array big.

#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++;
}
}
if(small[N-1] < big[M-1])
new[M+N-1] = big[M-1];
else
new[M+N-1] = small[N-1];

for(l=0;l<(M+N);l++)
printf("%d ",new[l]);
printf("\n");
}


CBFalconer said:
Mik0b0 wrote:
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:
You are allowed to use spaces. There are no prizes for obfuscation
by eliding them, and they are no longer on allocation. You are
also allowed to use reasonable indentation.
int main(void)
{
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;... snip code ...

As an example, here is how I would have coded it. Note that now
things are defined in one place, and the heart can be freely
modified. The key is the use of sizeof to extract what you defined
as M and N, and the slaving of the *ix names to the names of the
variables they index. The #define for sz is a dog-standard way of
extracting the size of an array. It is a compile time constant, so
it does not complicate the emitted code. size_t is an unsigned
type, which can always measure the size of an array in bytes, and
is the type returned by sizeof and thus by sz. Think about why at
most only one of the final while loops will be executed.

You might want to look up the use of loop-invariants to deduce the
validity of code.

#include <stdio.h>
int main(void)
{
int big[] = {1, 2, 5, 8, 10, 23, 45, 56};
int small[] = {3, 7, 11, 12, 100};

#define sz(a) (sizeof a / sizeof a[0])

int new[sz(big) + sz(small)];
size_t bigix, smallix, newix;

bigix = smallix = newix = 0;
while ((bigix < sz(big)) && (smallix < sz(small)))
if (small[smallix] < big[bigix])
new[newix++] = small[smallix++];
else
new[newix++] = big[bigix++];

while (bigix < sz(big))
new[newix++] = big[bigix++];
while (smallix < sz(small))
new[newix++] = small[smallix++];

for (newix = 0; newix < (sz(big) + sz(small)); newix++)
printf("%d ", new[newix]);
putchar('\n');

return 0;

} /* main *//* note that the index names are now tied to the arrays
that they index, and that the various sizes are tied
to the arrays that they describe. You should now be
able to modify the array initializations and have the
remainder of the code automatically adjust. */
 
B

Barry Schwarz

How about this program?

It invokes undefined behavior if the last two values of big are larger
than the last value of small. It will attempt to evaluate small[N]
which does not exist.
We need to take seperate care for the last case.
In your code,in the last interation of the while i=8 and k=4. So they
are taking some
value which is not part of the array. ie big[8] which is not
initialized in array big.

#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++;
}
}
if(small[N-1] < big[M-1])
new[M+N-1] = big[M-1];
else
new[M+N-1] = small[N-1];

for(l=0;l<(M+N);l++)
printf("%d ",new[l]);
printf("\n");
}


CBFalconer said:
Mik0b0 wrote:
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:
You are allowed to use spaces. There are no prizes for obfuscation
by eliding them, and they are no longer on allocation. You are
also allowed to use reasonable indentation.
#include<stdio.h>
#define M 8
#define N 5
main()
int main(void)
{
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;... snip code ...

As an example, here is how I would have coded it. Note that now
things are defined in one place, and the heart can be freely
modified. The key is the use of sizeof to extract what you defined
as M and N, and the slaving of the *ix names to the names of the
variables they index. The #define for sz is a dog-standard way of
extracting the size of an array. It is a compile time constant, so
it does not complicate the emitted code. size_t is an unsigned
type, which can always measure the size of an array in bytes, and
is the type returned by sizeof and thus by sz. Think about why at
most only one of the final while loops will be executed.

You might want to look up the use of loop-invariants to deduce the
validity of code.

#include <stdio.h>
int main(void)
{
int big[] = {1, 2, 5, 8, 10, 23, 45, 56};
int small[] = {3, 7, 11, 12, 100};

#define sz(a) (sizeof a / sizeof a[0])

int new[sz(big) + sz(small)];
size_t bigix, smallix, newix;

bigix = smallix = newix = 0;
while ((bigix < sz(big)) && (smallix < sz(small)))
if (small[smallix] < big[bigix])
new[newix++] = small[smallix++];
else
new[newix++] = big[bigix++];

while (bigix < sz(big))
new[newix++] = big[bigix++];
while (smallix < sz(small))
new[newix++] = small[smallix++];

for (newix = 0; newix < (sz(big) + sz(small)); newix++)
printf("%d ", new[newix]);
putchar('\n');

return 0;

} /* main *//* note that the index names are now tied to the arrays
that they index, and that the various sizes are tied
to the arrays that they describe. You should now be
able to modify the array initializations and have the
remainder of the code automatically adjust. */



Remove del for email
 
K

Keith Thompson

santosh said:
As far as I know, only for hosted programs and only under C99.

All C99 did in this area was (a) drop implicit int (in C90, "main()"
is a valid declaration specifying that main returns int; in C99, it's
illegal) and (b) explicitly state that other *implementation-defined*
forms are allowed (though C90 already allowed extensions, so IMHO this
additional permission is redundant).

And yes, the declaration of the program's entry point (its name, its
return type, and its parameters) is implementation-defined for
freestanding implementations.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top