#include <iostream>
using namespace std;
class Array
{
private:
int *Arr;
int size;
public:
Array(int size)
{
this->size = size;
Arr = new int[size];
}
void Create()
{
cout << "Enter Elements Into Array " << endl;
for (int i = 0; i < size; i++)
{
cout << "Enter Element Number " << (i + 1) << ": ";
cin >> Arr[i];
}
cout << endl
<< "Elements Insterted Sucessfully." << endl;
}
void Display()
{
cout << "The Array Elements Are: " << endl;
for (int i = 0; i < size; i++)
{
cout << Arr[i] << " ";
}
cout << endl;
}
void DescendingSort()
{
for (int i = 0; i < size; i++)
{
for (int j = i + 1; j < size; j++)
{
int temp;
if (Arr[i] > Arr[j])
{
temp = Arr[i];
Arr[i] = Arr[j];
Arr[j] = temp;
}
}
}
}
int GetIndex(int index)
{
if (index >= 0 && index < (size + 1))
{
return Arr[index];
}
else
{
return -1;
}
}
void Set(int index, int ele)
{
if (index >= 0 && index < (size + 1))
{
Arr[index] = ele;
}
}
Array Merge(Array &Arrb)
{
Array newArray(size + Arrb.size);
int i = 0, j = 0, k = 0;
while (i < size && j < Arrb.size)
{
if (Arr[i] < Arrb.GetIndex(j))
{
newArray.Set(k++, Arr[i++]);
}
else
{
newArray.Set(k++, Arrb.GetIndex(j++));
}
}
for (; i < size; i++)
{
newArray.Set(k++, Arr[i]);
}
for (; j < Arrb.size; j++)
{
newArray.Set(k++, Arrb.GetIndex(j));
}
return newArray;
}
~Array()
{
delete[] Arr;
cout << endl
<< "Array Destroyed Form Heap Memory.";
}
};
int main()
{
int size = 0;
cout << endl
<< "Enter Your First Array Size: ";
cin >> size;
Array firstArray(size);
firstArray.Create();
firstArray.DescendingSort();
firstArray.Display();
cout << endl
<< "Enter Your Second Array Size: ";
cin >> size;
Array secondArray(size);
secondArray.Create();
secondArray.DescendingSort();
secondArray.Display();
Array MergedArray = firstArray.Merge(secondArray);
MergedArray.Display();
return 0;
}