fork download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int main() {
  5. // your code goes here
  6. return 0;
  7. }
Success #stdin #stdout 0.01s 5312KB
stdin

class array{
private:
    int size;
    int length;
    int*items;
public:
    array(int arrsize)

{
    size =arrsize;
    length =0;
    items =new int[arrsize];
}
void fill()
{
    int nu_of_items;
    cout<<"How many items you want to fill "<<endl;
    cin>>nu_of_items;
    if(nu_of_items>size)
    {
        cout<<"You cannot exceed the array size"<<endl;
        return;

    }
    else{
        for(int i=0;i<nu_of_items;i++){
            cout <<"Enter item on "<<i<<endl;
            cin >>items[i];
            length++;
        }
    }
}
void Display()
{
    cout<<"Display Array content"<<endl;
    for(int i=0;i<length;i++)

    {
        cout<<items[i]<<endl;
    }

}
int getSize()
{
    return size;
}
int getLength()
{
    return length;
}
int search(int key)
{
    int index=-1;
    for(int i=0;i<length;i++)
    {
       if(items[i]==key)
       {
           index=i;
           break;
       }
    }
       return index;

    }
void Append(int newItem)

{
  if(length<size)
  {
      items[length]=newItem;
      length++;
  }else
  {
      cout<<"Array is fill"<<endl;
  }
}
void insert(int index,int newItem)
{
    if(index >=0&& index <size)
    {
        for(int i=length;i>index;i--)
        {
           items[i]=items[i-1];
        }
        items[index]=newItem;
        length++;
    }
    else{
        cout<<"Enter -index out of Range"<<endl;
    }
}
void Delete(int index)
{
    if(index >=0 && index < size)
    {
        for(int i=index;i< length -1;i++)
        {
           items[i]=items[i+1];
           length--;
        }


    }else{
    cout <<"index out of Range"<<endl;
    }
    }

};


int main()
{
    cout<<"Hello This is Array ADT demo"<<endl;
    int arraysize;
    cout<<"Enter the Array Size "<<endl;
    cin>>arraysize;
    array myarray(arraysize);
    myarray.fill();
    cout<<"Array size = " << myarray.getSize() <<endl;
    cout<< "while length = " <<myarray.getLength() <<endl;
    myarray.Display();
    cout<<"Enter the value to search for"<<endl;
    int key;
    cin>>key;
     int index = myarray.search(key);
    if(index == -1){
        cout<<"Item not found"<<endl;
    }
    else{
        cout<<"Item found @ position "<<index<<endl;
        }
        int newItem;
        cout<<"Enter newItem to add to the array"<<endl;
        cin>>newItem;
        myarray.Append(newItem);
        myarray.Display();
        cout<<"Enter index and item"<<endl;
        cin>>index;
        cin>>newItem;
        myarray.insert(index,newItem);
        myarray.Display();
            cout<<"Array size = " << myarray.getSize() <<endl;
    cout<< "while length = " <<myarray.getLength() <<endl;

    cout <<"Enter index to delete its item"<<endl;
    cin>>index;
    myarray.Delete(index);
    myarray.Display();
    cout<<"Array size = " << myarray.getSize() <<endl;
    cout<< "while length = " <<myarray.getLength() <<endl;

return 0;

}
stdout
Standard output is empty