Longer array out of two shorter arrays

Discussion in 'C Programming' started by Mik0b0, Jan 13, 2007.

  1. Mik0b0

    Mik0b0 Guest

    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!
    Mik0b0, Jan 13, 2007
    #1
    1. Advertising

  2. Mik0b0

    Ian Collins Guest

    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.
    Ian Collins, Jan 13, 2007
    #2
    1. Advertising

  3. 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.


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

    Ian Collins Guest

    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)

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


    Good catch.

    --
    Ian Collins.
    Ian Collins, Jan 13, 2007
    #4
  5. Mik0b0

    woodstok Guest

    On Jan 12, 5:14 pm, "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()


    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!
    woodstok, Jan 13, 2007
    #5
  6. woodstok said:

    >
    >
    > On Jan 12, 5:14 pm, "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()

    >
    > 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.

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

    Mik0b0 Guest

    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.
    Mik0b0, Jan 13, 2007
    #7
  8. Mik0b0

    woodstok Guest

    On Jan 12, 5:38 pm, Richard Heathfield <> wrote:
    > woodstok said:
    >
    >
    >
    > > On Jan 12, 5:14 pm, "Mik0b0" <> wrote:
    > >> 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)

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


    > ITYM >= in each case.
    >


    Yes I did. Thanks for catching that.
    woodstok, Jan 13, 2007
    #8
  9. Mik0b0

    CBFalconer Guest

    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)
    > {
    > 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.

    --
    "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
    CBFalconer, Jan 13, 2007
    #9
  10. Mik0b0

    CBFalconer Guest

    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
    CBFalconer, Jan 13, 2007
    #10
  11. Mik0b0

    Mik0b0 Guest

    Thanks a lot to everyone who replied! Now I have things to think about
    Mik0b0, Jan 13, 2007
    #11
  12. Mik0b0

    deepak Guest

    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");
    }


    On Jan 13, 2:06 pm, CBFalconer <> wrote:
    > 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 -
    deepak, Jan 13, 2007
    #12
  13. On 13 Jan 2007 06:20:36 -0800, "deepak" <> wrote:

    >
    >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");
    >}
    >
    >
    >On Jan 13, 2:06 pm, CBFalconer <> wrote:
    >> 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
    Barry Schwarz, Jan 13, 2007
    #13
  14. Mik0b0

    Ark Guest

    woodstok wrote:
    >

    <snip>

    >> main()

    >
    > main returns an int. Always.
    >

    <snip>
    Always indeed or only in hosted environments?
    - Ark
    Ark, Jan 13, 2007
    #14
  15. Mik0b0

    santosh Guest

    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.
    santosh, Jan 13, 2007
    #15
  16. "santosh" <> writes:
    > 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) <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.
    Keith Thompson, Jan 13, 2007
    #16
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    2
    Views:
    276
    CBFalconer
    Jan 12, 2007
  2. Philipp
    Replies:
    21
    Views:
    1,101
    Philipp
    Jan 20, 2009
  3. Kev Jackson
    Replies:
    2
    Views:
    99
  4. Allen Walker

    Merging two arrays -> array of arrays

    Allen Walker, May 21, 2010, in forum: Ruby
    Replies:
    6
    Views:
    151
    Jesús Gabriel y Galán
    May 21, 2010
  5. Steventangle
    Replies:
    4
    Views:
    91
    StevenTangle
    Mar 23, 2005
Loading...

Share This Page