Thread: merging arrays

  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をこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    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をこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    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
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    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
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  8. #8
    Ex scientia vera
    Join Date
    Sep 2007
    Posts
    477
    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
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    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)
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  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
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  12. #12
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    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
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    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をこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 01-01-2008, 04:07 PM
  2. Need Help With 3 Parallel Arrays Selction Sort
    By slickwilly440 in forum C++ Programming
    Replies: 4
    Last Post: 11-19-2005, 10: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, 09:24 PM
  5. merging arrays using pointers help!!!
    By edshaft in forum C++ Programming
    Replies: 3
    Last Post: 12-19-2001, 07:19 AM