Merging Arrays

This is a discussion on Merging Arrays within the C++ Programming forums, part of the General Programming Boards category; Hi, I am trying to merge two arrays into one while doing this I am trying to sort and remove ...

  1. #1
    Registered User
    Join Date
    Mar 2008
    Posts
    33

    Merging Arrays

    Hi,

    I am trying to merge two arrays into one while doing this I am trying to sort and remove duplicates..

    For example;

    Code:
    int a,a1,b,b1,c,c1,d,d1,e,e1;
    
    //the letters are int inputs from the user
    
    array1[size1]={a,b,c,d,e};
    array2[size2]={a1,b1,c1,d1,e1};
    
    size = size1+size2;
    
    array3[size]={/*here the int will be sorted and there will be no duplicates*/}
    so if the user enters;
    size1 = 5;
    size2 = 5;
    array1[5]=1,1,2,3,5
    array2[5]=4,5,6,7,8

    array3[10] should be array3[10]=1,2,3,4,5,6,7,8,-,-

    The point where I am stuck at is checking for the identical members and printing '-' for the empty parts in the array..

    Thanks in advance..

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Arrays can't have empty slots -- they will be filled with something. If you expect the users to not type negative numbers, you can use a negative value as a sort of "hey this number shouldn't be here" sentinel.

  3. #3
    Registered User
    Join Date
    Mar 2008
    Posts
    33
    negative value ? who said anything about negative numbers? that "-" isnt a minus sign..

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by yigster View Post
    negative value ? who said anything about negative numbers? that "-" isnt a minus sign..
    I said something about negative numbers, yes. But I'll say it again, in italics this time: you cannot have empty spaces in an array. There will be a number in that place. If you want to think of some of your array slots as "empty", then you will need to come up with a value that your users are unlikely to need, and use that as a placeholder.

  5. #5
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,806
    Maybe use some combination of std::sort/std::copy/std::set_intersection/std::merge/std::unique STL functions. All praise the all-mighty STL!
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  6. #6
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,308
    Here is something to get you started:
    Merging Arrays Interview Question
    Yes I know that's C#, and that was intentional.

  7. #7
    Registered User
    Join Date
    Mar 2008
    Posts
    33
    Quote Originally Posted by tabstop View Post
    I said something about negative numbers, yes. But I'll say it again, in italics this time: you cannot have empty spaces in an array. There will be a number in that place. If you want to think of some of your array slots as "empty", then you will need to come up with a value that your users are unlikely to need, and use that as a placeholder.
    yes I know there wont be any empty spaces thats why there will be '-' in the empty slots

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by yigster View Post
    yes I know there wont be any empty spaces thats why there will be '-' in the empty slots
    '-' is the number 45[*]. Are you willing to give up the number 45 and use it instead as a "this spot is empty" marker? If so, then you're in.
    [*]Assuming you're on an ASCII machine, naturally.

  9. #9
    Registered User
    Join Date
    Mar 2008
    Posts
    33
    So this is what I have so far.. but when I run it says array1.exe has stopped working =) the compiler doesnt give any errors..

    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
        int size,size1, size2,i,k,j;
        int array1[size1],array2[size2],array3[size];
    
        cout << "Enter size of array1" << endl;
        cin >> size1;
        cout << "\nEnter size of array2";
        cin >> size2;
    
        size = size1+size2;
        cout<<"\nThe size of array3 will be: "<<size;
    
        for(i=0;i<size1;i++)
        {
            cout<<"array1["<<i<<"]: ";
            cin >> array1[i];
        }
        for(i=0;i<size2;i++)
        {
            cout<<"array2["<<i<<"]: ";
            cin >> array2[i];
        }
        for(i=0;i<size;i++)
        {
            array3[i]=array1[i];
        }
        for(i=size1-1;i<size;i++)
        {
            array3[i]=array2[i];
        }
    
        for(i=0,j=0; i<size;i++)
        {
            cin>>array3[size];
            for(k=0;k<j;k++)
            {
                if(array3[k]=array3[j])
                {
                    j--;
                }
                j++;
            }
        }
        cout<<"Array3: "<<array3[size];
        return 0;
    }

  10. #10
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Code:
    int array1[size1],array2[size2],array3[size];
    Not even a little bit. VLA are illegal in C++, and even if you're using g++'s extensions, you have to figure out what size1, size2, and size are first before you can declare an array of those sizes.

    Also your indexes for copying from array2 to array3 is just not going to cut it. The elements are not going to line up in the same places (the first element of array2 is not going to land as the first element of array3) so don't try to force it.

  11. #11
    Registered User
    Join Date
    Mar 2008
    Posts
    33
    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
        int size1,size2,size,i,k,j;
        int *array1,*array2,*array3;
    
        //int array1[size1],array2[size2],array3[size];
    
        cout << "Enter size of array1" << endl;
        cin >> size1;
        array1 = new int[size1];
        cout << "\nEnter size of array2" << endl;
        cin >> size2;
        array2 = new int[size2];
    
    
        size = size1+size2;
        array3 = new int[size];
    
        cout << "The size of array3 will be: " << size << endl;;
    
        for(i=0;i<size1;i++)
        {
            cout<<"array1["<<i<<"]: ";
            cin >> array1[i];
        }
        for(i=0;i<size2;i++)
        {
            cout<<"array2["<<i<<"]: ";
            cin >> array2[i];
        }
        for(i=0;i<size;i++)
        {
            array3[i]=array1[i];
        }
        for(i=size1;i<size;i++)
        {
            array3[i]=array2[i];
        }
    
        for(i=0,j=0; i<size;i++)
        {
            
            cin>>array3[size];
            for(k=0;k<j;k++)
            {
                if(array3[k]=array3[j])
                {
                    j--;
                }
                j++;
            }
        }
    
        cout << endl << "The array elements:";
        for (i=0;i<j;i++)
        {
            cout<<"array3["<<i<<"]: ";
        }
        return 0;
    }
    cant get it to print array3??

  12. #12
    Registered User
    Join Date
    Mar 2010
    Posts
    109
    Quote Originally Posted by yigster View Post
    cant get it to print array3??
    I'm not sure what you are asking me.

    Code:
    cin>>array3[size];
    You are writing past the bounds of the array. Also,

    Code:
    if(array3[k]=array3[j])
    Are you looking to check if they are equal? If so, the operator is ==. A single = is the assignment operator.

  13. #13
    Registered User
    Join Date
    Mar 2008
    Posts
    33
    copy and run the program.. it should print soething like this;

    Code:
    array3[0]=1
    array3[0]=2
    .
    .
    .
    . and so on.. and '-' for the empty parts in array3..
    Last edited by yigster; 04-13-2010 at 11:21 AM.

  14. #14
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,308
    Don't tell us to copy and run your program. We can see at a glance what is wrong with it.
    It is you who needs to copy and run all of the advice given, into your head.

    It appears that you haven't made a real start on the merging yet. Don't try and do duplicate checking without it. The duplicate checking will be just a small amount of extra code on top of the merging code, not something separate.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  15. #15
    Registered User
    Join Date
    Mar 2008
    Posts
    33
    Code:
    #include <cstdlib>
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
        int size1,size2,size,i,k,j;
        string *array1,*array2,*array3;
        string temp;
    
        cout<<"Enter size of array1: ";
        cin>>size1;
        array1=new string[size1];
    
        cout<<"\nEnter size of array2: ";
        cin>>size2;
        array2=new string[size2];
    
        size=size1+size2;
        array3=new string[size];
    
        cout<<"The size of array3 will be: "<<size;
        cout<<"\n";
        cout<<"\n";
    
        for(i=0;i<size1;i++)
        {
            cout<<"array1["<<i<<"]: ";
            cin >> array1[i];
        }
        cout<<"\n";
        for(i=0;i<size2;i++)
        {
            cout<<"array2["<<i<<"]: ";
            cin >> array2[i];
        }
        for(i=0;i<size1;i++)
        {
            array3[i]=array1[i];
        }
        for(i=size1,j=0;j<size2,i<size;i++,j++)
        {
            array3[i]=array2[j];
        }
    
        for(i=0;i<size;i++)
         for(j=i+1;j<size;j++)
         {
           if(array3[i]<array3[j])
            {
              temp=array3[i];
              array3[i]=array3[j];
              array3[j]=temp;
            }
        }
        for(i=0;i<size;i++)
        {
            for(j=i+1;j<size;j++)
            {
                if(array3[i]==array3[j])
                    array3[j]="-";
            }
        }
        cout<<"Array3["<<size<<"]: ";
        for(i=0;i<size;i++)
        {
            cout<<array3[i]<<" ";
        }
    
    return 0;
    }
    solved it thank you for helping..

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

Similar Threads

  1. Merging arrays help
    By key4life in forum C Programming
    Replies: 12
    Last Post: 12-05-2009, 06:46 PM
  2. merging arrays
    By acohrockz21 in forum C Programming
    Replies: 28
    Last Post: 03-09-2008, 03:28 AM
  3. Merging two arrays.
    By Roaring_Tiger in forum C Programming
    Replies: 2
    Last Post: 08-21-2004, 08: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

Tags for this Thread


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