473,385 Members | 1,983 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Longer array out of two shorter arrays

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[i])
{
new[l]=small[k];
k++;
l++;
}
else
{
new[l]=big[i];
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!

Jan 13 '07 #1
15 1688
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[i])
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.
--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Jan 13 '07 #2
Mik0b0 wrote:
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)

<snip code>
>
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

--
Ian Collins.
Jan 13 '07 #3
Richard Heathfield wrote:
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[i])


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

--
Ian Collins.
Jan 13 '07 #4


On Jan 12, 5:14 pm, "Mik0b0" <new...@gmail.comwrote:
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[i])
what happens in the above line if k N or i M?
{
new[l]=small[k];
k++;
l++;
}
else
{
new[l]=big[i];
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!
Jan 13 '07 #5
woodstok said:
>

On Jan 12, 5:14 pm, "Mik0b0" <new...@gmail.comwrote:
>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[i])

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.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Jan 13 '07 #6

Ian Collins wrote:
Mik0b0 wrote:
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)

<snip code>

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

--
Ian Collins.
I use gcc.

Jan 13 '07 #7


On Jan 12, 5:38 pm, Richard Heathfield <r...@see.sig.invalidwrote:
woodstok said:
On Jan 12, 5:14 pm, "Mik0b0" <new...@gmail.comwrote:
main()
main returns an int. Always.
Yes, and:

main()

defines main as returning int. Always. (Except in C99thud[1].)
So.. not always then. :)
>
{
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[i])
what happens in the above line if k N or i M?
ITYM >= in each case.
Yes I did. Thanks for catching that.

Jan 13 '07 #8
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;

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

--
"I was born lazy. I am no lazier now than I was forty years
ago, but that is because I reached the limit forty years ago.
You can't go beyond possibility." -- Mark Twain

Jan 13 '07 #9
CBFalconer wrote:
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. */

--
"I was born lazy. I am no lazier now than I was forty years
ago, but that is because I reached the limit forty years ago.
You can't go beyond possibility." -- Mark Twain

Jan 13 '07 #10
Thanks a lot to everyone who replied! Now I have things to think about

Jan 13 '07 #11

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[i])
{
new[l]=small[k];
k++;
l++;
}
else
{
new[l]=big[i];
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");
}
On Jan 13, 2:06 pm, CBFalconer <cbfalco...@yahoo.comwrote:
CBFalconer wrote:
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. */

--
"I was born lazy. I am no lazier now than I was forty years
ago, but that is because I reached the limit forty years ago.
You can't go beyond possibility." -- Mark Twain- Hide quoted text -- Show quoted text -
Jan 13 '07 #12
On 13 Jan 2007 06:20:36 -0800, "deepak" <de*********@gmail.comwrote:
>
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[i])
{
new[l]=small[k];
k++;
l++;
}
else
{
new[l]=big[i];
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");
}
On Jan 13, 2:06 pm, CBFalconer <cbfalco...@yahoo.comwrote:
>CBFalconer wrote:
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. */

--
"I was born lazy. I am no lazier now than I was forty years
ago, but that is because I reached the limit forty years ago.
You can't go beyond possibility." -- Mark Twain- Hide quoted text -- Show quoted text -

Remove del for email
Jan 13 '07 #13
Ark
woodstok wrote:
>
<snip>
>main()

main returns an int. Always.
<snip>
Always indeed or only in hosted environments?
- Ark
Jan 13 '07 #14
Ark wrote:
woodstok wrote:
<snip>
main()
main returns an int. Always.
<snip>
Always indeed or only in hosted environments?
As far as I know, only for hosted programs and only under C99.

Jan 13 '07 #15
"santosh" <sa*********@gmail.comwrites:
Ark wrote:
>woodstok wrote:
>
<snip>
>main()

main returns an int. Always.
<snip>
Always indeed or only in hosted environments?

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.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Jan 13 '07 #16

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

19
by: deko | last post by:
I'm kind of lost on this one - I need to modify 2 files based on user input: $data_array = file($data_file); $counter_array = file($counter_file); // There is a line-for-line relationship...
4
by: Michael Hill | last post by:
If I have this array like: var XXXX = new Array( "('2003','45.24','.2',true,true)", "('2004','45.75','.25')", "('2005','46.01','.27')", ); and have identified the array as "selectedArray"...
18
by: Dan | last post by:
hello, I would to know if it is possible to delete an instance in an array, The following does not allow me to do a delete. I am trying to find and delete the duplicate in an array, thanks ...
4
by: Jim O'D | last post by:
Hi all I have an array a=array(). I want to extract an array with all the elements of a that are less than 0. Method 1. new = array() Method 2.
9
by: buda | last post by:
Hi, I've been wondering for a while now (and always forgot to ask :) what is the exact quote from the Standard that forbids the use of (&array) (when x >= number_of_columns) as stated in the FAQ...
21
by: yeti349 | last post by:
Hi, I'm using the following code to retrieve data from an xml file and populate a javascript array. The data is then displayed in html table form. I would like to then be able to sort by each...
4
by: entitledX | last post by:
Hi, I'm trying to use the HDF library to read a few HDF files that I need to process. The data in each file varies in rows, but the columns remain constant. Because of that, I had dynamically...
12
by: rajash | last post by:
Thanks to everyone on this forum for their helpful comments, now here is my solution to Exercise 1.17, pretty short and neat! // print lines longer than 80 chars char x; // allocate buffer ...
14
by: Dan Rumney | last post by:
I've been taking a look at Douglas Crockford's JSLint. One of the conventions that it expects is that arrays be created using literal notation var arr1 = ; as opposed to using a constructor...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.