merging arrays

This is a discussion on merging arrays within the C Programming forums, part of the General Programming Boards category; hi.. just asking of what code am i going to use to merge arrays? for example. Code: intA[5]={1,2,3,4,5}; intB[5]={2,4,6,8,10}; the ...

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    18

    merging arrays

    hi.. just asking of what code am i going to use to merge
    arrays?
    for example.

    Code:
    intA[5]={1,2,3,4,5};
    intB[5]={2,4,6,8,10};
    the output should be..

    1,2,3,4,5,6,8,10

    please help
    advance thanks ^_^

  2. #2
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,668
    Make a new array, or reallocate an array. But since these arrays are static, fixed-size, you can't reallocate them, so a new might be necessary. If you have fixed-width arrays, you can define a new fixed-size array which is the length of the two combined. Otherwise you need to allocate a new array dynamically via malloc/free.

    But if you only want to print out the data, you don't really need to merge the arrays at all.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User
    Join Date
    Feb 2008
    Posts
    18
    nono..thats just for example what i mean is like this

    Enter values for the first array

    [code]scanf("%d",&a[5]);[\code]

    Enter values for the second array

    [code]scanf("%d",&b[5]);[\code]

    for example the user supplied numbers of 5, 9 , 11, 13 , 15
    and 1, 5, 9 11, 14

    the output should be like this..

    1, 5, 9, 11, 13, 14, 15

  4. #4
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    So basically, you don't have to store the merged arrays?

    And you want to ignore repeats... why not add the element from A providing if it's not in B to C, and the same for B (providing it's not in A) add it to C. Then order C... And output it.

    The max size of C will be, sizeof(A) + sizeof(B).

  5. #5
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,668
    You messed up those code tags.
    They should not be [\code], they should be [/code].
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Registered User
    Join Date
    Feb 2008
    Posts
    18
    Quote Originally Posted by zacs7 View Post
    So basically, you don't have to store the merged arrays?

    And you want to ignore repeats... why not add the element from A providing if it's not in B to C, and the same for B (providing it's not in A) add it to C. Then order C... And output it.

    The max size of C will be, sizeof(A) + sizeof(B).
    i dont get it >_<
    sorry im just a novice when it comes on C

  7. #7
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    1. Input A (not with your broken code - but with a loop)
    2. Input B
    3. Allocate C big enough to store members of 2 arays
    4. Copy A to C
    5. for each member of B that is not found in A - copy to C
    6. sort C

    for example
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  8. #8
    Ex scientia vera
    Join Date
    Sep 2007
    Posts
    478
    1. Input both arrays, A and B,
    2. Allocate a new one that's size sizeof(A)+sizeof(B)
    3. Put numbers in new array - use a loop.
    4. Delete the repeats.
    5. Sort it - bubblesort is easy to implement.


    Now code.

  9. #9
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    Quote Originally Posted by IceDane View Post
    1. Input both arrays, A and B,
    2. Allocate a new one that's size sizeof(A)+sizeof(B)
    3. Put numbers in new array - use a loop.
    4. Delete the repeats.
    5. Sort it - bubblesort is easy to implement.


    Now code.
    better sort first - then output ignoring duplicates (only works if A and B do not have duplicates from the beginning)
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  10. #10
    Registered User
    Join Date
    Feb 2008
    Posts
    18
    dude how can i copy and allocate the members of the array to C?

    heres my code.. i dont know what to do next..

    Code:
    #include<stdio.h>
    
    void main()
    { int a[5];
      int b[5];
      int c[10][10];
      int d;
    
      clrscr();
      printf("\n\nEnter values of first array:\n");
      for(d=0;d<5;d++)
      {scanf("&#37;d",&a[5]);}
      printf("\n\nEnter values of second array:\n");
      for(d=0;d<5;d++)
      {scanf("%d",&b[5]);}

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,794
    better sort first - then output ignoring duplicates (only works if A and B do not have duplicates from the beginning)
    Heheh, acohrockz21, have you heard of merge sort?

    By the way, void main() should be int() main(), clrscr() is non-standard and not needed here, and you should indent your code a little better.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  12. #12
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,668
    Quote Originally Posted by acohrockz21 View Post
    dude how can i copy and allocate the members of the array to C?

    heres my code.. i dont know what to do next..

    Code:
    #include<stdio.h>
    
    int main()
    { 
      int a[5];
      int b[5];
      int c[10][10];
      int d;
    
      /*clrscr();*/
      printf("\n\nEnter values of first array:\n");
      for(d=0;d<5;d++)
      {
        scanf("&#37;d",&a[5]);
      }
      printf("\n\nEnter values of second array:\n");
      for(d=0;d<5;d++)
      {
        scanf("%d",&b[5]);
      }
    Indent properly and you'll see you're missing an ending }.
    And don't use void main. Use int main. And avoid clrscr. It's non-standard.
    It also seems to me that you need to go back to the books because this is code that will get you nowhere. It's not even functional right now.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  13. #13
    Registered User
    Join Date
    Feb 2008
    Posts
    18
    Quote Originally Posted by laserlight View Post
    Heheh, acohrockz21, have you heard of merge sort?

    By the way, void main() should be int() main(), clrscr() is non-standard and not needed here, and you should indent your code a little better.

    dude sorry i dont >_<

    t also seems to me that you need to go back to the books because this is code that will get you nowhere. It's not even functional right now.
    sorry T_T likewise i sad im still a newbie in C im still studying

  14. #14
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,714
    If the output doesn't have to be sorted I would just make a union of the two arrays, a and b.

  15. #15
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,668
    Quote Originally Posted by acohrockz21 View Post
    sorry T_T likewise i sad im still a newbie in C im still studying
    But that's why I told you to consult the books a little more. To try to grasp the more basics concepts before moving on.
    Don't worry, everyone starts off as a beginner. You just need practice.
    I can highlight the problems in the code if it makes you feel better.

    int c[10][10];
    The array is a 2D array of 10 x 10 elements, which is too much. You don't need all that space.

    Code:
      for(d=0;d<5;d++)
      {
        scanf("%d",&a[5]);
      }
    This code loops 5 time and writes the input to the 5th (or rather 6th) element of the array, which btw, does not exist. When you create an array of 5 elements (such as int a[5]), then you get elements 0 to 4 (number of elements - 1, since indexing starts from 0, not 1).
    Writing over the same data over and over is not very productive and writing beyond the end of the array is a security risk.

    In itself, a, b, c and d are not very good variable names. They tell nothing of what they're used for, so it's better to use productive names that tells what they are used for.
    And finally, you need to indent better so you can match all the { and } correctly. If you need help indenting, use a code beautifier or an IDE that indents for you, such as Visual Studio (Express).

    Welcome to C.
    Good luck.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 01-01-2008, 03:07 PM
  2. Need Help With 3 Parallel Arrays Selction Sort
    By slickwilly440 in forum C++ Programming
    Replies: 4
    Last Post: 11-19-2005, 09:47 PM
  3. Merging two arrays.
    By Roaring_Tiger in forum C Programming
    Replies: 2
    Last Post: 08-21-2004, 07:00 AM
  4. Crazy memory problem with arrays
    By fusikon in forum C++ Programming
    Replies: 9
    Last Post: 01-15-2003, 08:24 PM
  5. merging arrays using pointers help!!!
    By edshaft in forum C++ Programming
    Replies: 3
    Last Post: 12-19-2001, 06:19 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21