Program to implement variable size array














































Program to implement variable size array



//Program to Implement variable size array
#include<iostream>
using namespace std;
class intern
{
    int s,*arr,i; //s is size of an array
public:
    intern()
    {
        s=0; //Initializing value of size equal to zero
    }
    void getvalue() //Function for taking value of size from user
    {
        cout<<"Enter the size of an array: "; //Taking value of size from user
        cin>>s;
    }
    void putvalue() //Function for displaying the size of an array entered by user
    {
        cout<<"Your array size is "<<s<<endl;
    }
    void create() //Function for creating memory block
    {
        arr=new int[s]; // Creating a variable length array using dynamic memory allocation operator "new"
        cout<<"Your array of size "<<s<<" is created"<<endl;
    }
    void getdata() //Function for taking values of an array
    {
        for(i=0;i<s;i++)
        {
            cout<<"\nEnter element to be inserted: "; //taking value from user
            cin>>arr[i];
        }
    }
    void putdata() //Function for displaying array
    {
        for(i=0;i<s;i++)
        {
            cout<<"Your "<<i+1<<" element is "<<arr[i]<<endl;
        }
    }
    void destroy() //Function for destroying memory block
    {
        delete []arr;
        cout<<"Your memory block is destroyed"<<endl;
    }
};
main()
{
    intern i;
    i.getvalue();
    i.putvalue();
    i.create();
    i.getdata();
    i.putdata();
    i.destroy();
    return 0;
}


Comments

  • Sagar
    19-Oct-2019 01:14:17 PM
    here is my program's output given below

    Enter the size of an array: 2
    Your array size is 2
    Your array of size 2 is created
    Enter element to be inserted: 27

    Enter element to be inserted: 1
    Your 1 element is 27
    Your 2 element is 1
    Your memory block is destroyed
  • Sagar
    19-Oct-2019 01:08:04 PM
    Here is program given above which implement the variable size array
    I used one constructor and six member function.
    Each function has different work e.g.for displaying value,for creating array etc.
    I used comments also in program to make it clearly readable.
    I used one object and from that I accessed all the member function of the class intern.
    I hope you like my program and I get the chance to internship in your company for enhancing my knowledge and experience.
    THANK YOU for your consideration.