#include <iostream>
using namespace std;
class Array{
private:
int sizze,length,*ptr;
public:
// Creation
Array(int size) {
ptr=new int[size];
length=0;
sizze=size;
}
// Fill
void Fill() {
cout<<"enter the number of elements which you want to fill : ";
int x;cin>>x;
if (x>sizze or x<0) {
cout<<"Invalid count (must be between 0 and "<<sizze<<")\n";
return ;
}
for (int i=0;i<x;i++) {
cout<<"enter the element "<<i+1<<" : \n";
cin>>ptr[i];
}
}
// Append : push new element in the end of array
void Append(int value) {
if (length==sizze) {
cout<<"the array is full\n";
return ;
}
ptr[length]=value;
length++;
}
// insert : insert the value in index the index is 0-based
void insert(int value,int index) {
if (index<0 or index>=length) {
cout << "Index out of bounds\n";
return ;
}
for (int i=length-1;i>=index;i--) {
ptr[i+1]=ptr[i];
}
ptr[index]=value;
length++;
}
// Search return the nearst index if the value was found otherwise return -1
int search(int value) {
int index=-1;
for (int i=0;i<length;i++) {
if (ptr[i]==value) {
index=i;
break;
}
}
return index;
}
// delete the value in index index is 0-based
void Delete(int index) {
if (index<0 or index>=length) {
cout << "Index out of bounds\n";
return ;
}
for (int i=index+1;i<length;i++) {
ptr[i-1]=ptr[i];
}
length--;
}
void Enlarge(int new_size) {
int *old=ptr;
ptr=new int[new_size];
for (int i=0;i<min(length,new_size);i++) {
ptr[i]=old[i];
}
delete [] old;
}
int get_size() {
return sizze;
}
int get_length() {
return length;
}
void merge(Array &other) {
int *old=ptr;
int New_size=other.get_size()+sizze;sizze=New_size;
ptr=new int[New_size];
for (int i=0;i<length;i++)ptr[i]=old[i];
delete []old;
for (int i=0;i<other.get_length();i++)ptr[length++]=other.ptr[i];
}
void Display() {
for (int i=0;i<length;i++)cout<<ptr[i]<<" ";
cout<<"\n";
}
};
int main() {
Array a(5);
a.Fill();
a.Append(42);
a.insert(99, 1);
cout << "Index of 42: " << a.search(42) << "\n";
a.Delete(0);
a.Enlarge(10);
Array b(3);
b.Append(7);
b.Append(8);
a.merge(b);
cout << "Size: " << a.get_size() << ", Length: " << a.get_length() << "\n";
return 0;
}