Monday, 1 October 2012

Deletion and Insertion at any index of Array


#include <iostream.h>
const int SIZE = 10;
class Array
{
private:
int a[SIZE];
int n;
public:
Array()
{
for (int i=0;i<SIZE;i++)
a[i] = 0;
n = 0;
}
void read()
{
int num;
cout<<"how many values you want to enter ? "<<endl;
cin>>num;
for(int i = 0; i<num; i++)
{
cout<<"enter values "<<endl;
cin>>a[i];
}
n = num;
}
void display()
{
for (int i=0;i<n;i++)
cout << a[i];
cout << endl;
}
void del()
{
int index;
cout << "Enter index";
cin >> index;
for (int i=index;i<n-1;i++)
a[i] = a[i+1];
n--;
display();
}
void insert()
{
int index;
cout << "Enter index";
cin >> index;
for (int i=n;i>index;i--)
a[i] = a[i-1];
cout << "Enter Value";
cin >> a[i];
n++;
display();
}


};
void main()
{
Array itm, a2, a3;
itm.read();
itm.display();
a2.read();
a2.display();

itm.del();
itm.insert();
}

No comments:

Post a Comment