0
#include<iostream>

using namespace std;

class array
{
    public:
        int size;
        int *A;
};


    for(;j<arr2->size;j++)
    {
        arr3.A[k++]=arr2->A[j];
        length++;
    }

    for(int a=0;a<length;a++)
    {
        cout << arr3.A[a];
    }

}

int main()
{
    array arr1,arr2;
    cout<<"Enter the size of the array 1 :\n";
    cin>> arr1.size;
    arr1.A = new int(arr1.size);
    cout<<"Enter the elements in the array 1 :\n";
    for(int i=0;i<arr1.size;i++)
    {
        cin>>arr1.A[i];
    }
    cout<<"Enter the size of the array 2 :\n";
    cin>> arr2.size;
    arr2.A = new int(arr2.size);
    cout<<"Enter the elements in the array 2 :\n";
    for(int i=0;i<arr2.size;i++)
    {
        cin>>arr2.A[i];
    }
    set_union(&arr1,&arr2);


}

This is my code to implement union of arrays but in my ide after taking the input of size of second array, program gets stuck or gets terminated. I mean , it doesn't go to the part where the program ask user to put elements in the array. Problem starts after the statement where it ask to put the size of the array 2.

INZAMAM
  • 11
  • Change `new int(arr2.size);` to `new int[arr2.size];`. Similarly, change `new int(arr1.size)` to `new int[arr1.size]`. See [How to create a dynamic array of integers](https://stackoverflow.com/questions/4029870/how-to-create-a-dynamic-array-of-integers). The same is explained in any beginner [c++ book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – Jason Sep 04 '22 at 12:10
  • The expression `new int(arr1.size)` creates *one single* `int` value, and initializes it to `arr1.size`. You probably want `new int[arr1.size]`. – Some programmer dude Sep 04 '22 at 12:10
  • For future questions, please make sure you copy-paste the correct [mre] to show us. Also please take some time to read [the help pages](http://stackoverflow.com/help), take the SO [tour], read [ask], as well as [this question checklist](https://codeblog.jonskeet.uk/2012/11/24/stack-overflow-question-checklist/). – Some programmer dude Sep 04 '22 at 12:11

0 Answers0