Thread: to merge two arrays

  1. #1
    Registered User
    Join Date
    Oct 2009
    Posts
    27

    to merge two arrays

    compiled on turbocpp 4.5.....error is that the final merged array is much bigger with garbage values...please help to find the mistake in this program...
    Code:
    //cpp program to concatenate 2 arrays
    #include<iostream.h>
    #include<conio.h>
    //the class
    class array
    {
    		private:
    		int a[40];
    		int n;
    		public:
    		void getdata();
    		void concatenate(array &a1,array &a2);
    		void showdata(array &a);
    };
    void array::getdata()
    {
    		cout<<"\n How many?";
    		cin>>n;
    		cout<<"\n Enter the elements:";
    		for(int i=0;i<n;i++)
    		{
    			  cin>>a[i];
    		}
    }
    void array::showdata(array &a)
    {
    		for(int i=0;i<a.n;i++)
    		{
    			  cout<<"\n"<<a.a[i];
    		}
    }
    //concatenation or merge function for the two arrays
    void array::concatenate(array &a1,array &a2)
    {
    		array a3;
    		for(int i=0,k=0;i<a1.n;i++,k++)
    		{
    			  a3.a[k]=a1.a[i];
    		}
    		for(int j=0;j<a2.n;j++,k++)
    		{
    			  a3.a[k]=a2.a[j];
    		}
    }
    void main()
    {
    		array a1,a2,a3;
    		a1.getdata();
    		a1.showdata(a1);
    		a2.getdata();
    		a2.showdata(a2);
    		a3.concatenate(a1,a2);
    		a3.showdata(a3);
    		getch();
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Your concatenate stores all the results in a local var a3, not the 'this' instance of a.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Oct 2009
    Posts
    27

    little bit of more help

    Quote Originally Posted by Salem View Post
    Your concatenate stores all the results in a local var a3, not the 'this' instance of a.
    Code:
    void array::concatenate(array &a1,array &a2)
    {
    		for(int i=0,k=0;i<a1.n;i++,k++)
    		{
    			  this.a[k]=a1.a[i];
    		}
    		for(int j=0;j<a2.n;j++,k++)
    		{
    			  this.a[k]=a2.a[j];
    		}
    }
    should not the above modification solve the problem? Haven't done OOP for a long time. sorry for the inconvenience.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > should not the above modification solve the problem?
    It might, if you also update the other private member variable with the appropriate value.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Jul 2013
    Location
    Germany
    Posts
    499
    Code:
    //cpp program to concatenate 2 arrays
    #include<iostream>
    
    using namespace std;
    
    //the class
    class my_array
    {
    private:
        int a[40];
        int n;
    public:
        my_array();
        void getdata();
        void concatenate(my_array &a1,my_array &a2);
        void showdata(my_array &a);
    };
    void my_array::getdata()
    {
        cout<<"\n How many?";
        cin>>n;
        cout<<"\n Enter the elements:";
        for(int i=0;i<n;i++)
        {
            cin>>a[i];
        }
    }
    my_array::my_array()
    {
        for (int i=0; i<40; ++i)
        {
            a[i]=0;
        }
        n=0;
    }
    void my_array::showdata(my_array &a)
    {
        for(int i=0;i<a.n;i++)
        {
            cout<<"\n"<<a.a[i];
        }
    }
    //concatenation or merge function for the two arrays
    void my_array::concatenate(my_array &a1,my_array &a2)
    {
        my_array a3;
        for(int i=0,k=0;i<a1.n;i++,k++)
        {
            a3.a[k]=a1.a[i];
        
        for(int j=0;j<a2.n;j++,k++)
        
            a3.a[k]=a2.a[j];
        }
    }
    int main()
    {
        my_array a1,a2,a3;
        a1.getdata();
        a1.showdata(a1);
        a2.getdata();
        a2.showdata(a2);
        a3.concatenate(a1,a2);
        a3.showdata(a3);
        
        return 0;
    }
    Look into namespaces if you want to name something like "array" and not actually use one. Actually that is really a bad idea but it would be good practice so give it a try. Your main was void and it should be int, you didn't have a return value in your main as well.. Your loop ended and you still tried to implement a char with the above one. Watch your brackets and pay attention to your complier warnings. Also you had no constructor.

    You really should be using an vector here. They are so
    much easier to work with and setting the size of your array while the program is running is very poor practice. The code I post is not prefect and needs work so if this is a homework assignment don't submit this. Look at the loop and think about a vector and easy it would be to merge to the using insert. Look up insert at cplusplus.com is your are unfamiliar with this.

    Look at the code and I will do the best I can to help.
    Last edited by jocdrew21; 02-14-2014 at 02:27 PM.

  6. #6
    Registered User
    Join Date
    Jul 2013
    Location
    Germany
    Posts
    499
    I apologize for all the grammar errors in the above statement. Looks like I should proofread my posts if I am a little tipsy.

  7. #7
    Registered User
    Join Date
    Jul 2013
    Posts
    158
    The best alternative to using arrays is Vectors ...you should quit this.
    Take a gander at here
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Merge two arrays together
    By Fedryan in forum C Programming
    Replies: 2
    Last Post: 11-04-2013, 01:11 PM
  2. sub-arrays for merge Sort
    By abood1190 in forum C Programming
    Replies: 3
    Last Post: 05-15-2013, 05:35 PM
  3. Can you merge arrays?
    By SeriousTyro in forum C Programming
    Replies: 14
    Last Post: 09-21-2009, 10:38 AM
  4. how to merge two arrays recursevly..
    By transgalactic2 in forum C Programming
    Replies: 117
    Last Post: 01-11-2009, 04:47 PM
  5. How to merge 2 arrays?
    By planet_abhi in forum C Programming
    Replies: 3
    Last Post: 02-16-2003, 12:23 AM

Tags for this Thread