#include<iostream.h>
#include<stdlib.h>
#include<conio.h>
class array
{
private:
int a[50],n,p,t,i;
public:
array();
void insert();
void del();
void display();
void menu();
};
array::array()
{
cout<<"enter number of elements : ";
cin>>n;
n--;
for(i=0;i<=n;i++)
cin>>a[i];
cout<<"array is created ";
}
void array::insert()
{
cout<<"1.At beginning"<<endl
<<"2.At particular position"<<endl
<<"3.At ending"<<endl;
int ch;
cout<<"enter your choice : ";
cin>>ch;
switch(ch)
{
case 1: n++;
for(i=n-1;i>=0;i--)
a[i+1]=a[i];
cout<<"enter the element you want to insert : ";
cin>>a[0];
break;
case 2: cout<<"enter position value : ";
cin>>p;
p--;
if(p>n)
cout<<"position value is greater than array size ";
else
{
n++;
for(i=n-1;i>=p;i--)
a[i+1]=a[i];
cout<<"enter the element you want to insert : ";
cin>>a[p];
}
break;
case 3: n++;
cout<<endl<<"enter the element you want to insert";
cin>>a[n];
}
}
void array::del()
{
cout<<"1.At beginning"<<endl
<<"2.At particular position"<<endl
<<"3.At ending"<<endl
<<"enter your choice : ";
int ch;
cin>>ch;
switch(ch)
{
case 1: for(i=0;i<=n;i++)
a[i]=a[i+1];
n--;
break;
case 2: cout<<"enter position value : ";
cin>>p;
p--;
if(p>n)
cout<<"position value is greater than array size";
else
{
for(i=p;i<=n;i++)
a[i]=a[i+1];
n--;
}
break;
case 3: n--;
}
}
void array::display()
{
for(i=0;i<=n;i++)
cout<<" "<<a[i];
}
void array::menu()
{
int ch;
while(1)
{
cout<<endl
<<"Array operation menu"<<endl
<<"1.insert"<<endl
<<"2.delete"<<endl
<<"3.display"<<endl
<<"4.exit"<<endl
<<"enter your choice : ";
cin>>ch;
switch(ch)
{
case 1: insert();
break;
case 2: del();
break;
case 3: display();
break;
case 4: exit(1);
break;
default:
cout<<"sorry! invalid choice";
}
}
}
void main()
{
clrscr();
array a;
a.menu();
getch();
}
#include<stdlib.h>
#include<conio.h>
class array
{
private:
int a[50],n,p,t,i;
public:
array();
void insert();
void del();
void display();
void menu();
};
array::array()
{
cout<<"enter number of elements : ";
cin>>n;
n--;
for(i=0;i<=n;i++)
cin>>a[i];
cout<<"array is created ";
}
void array::insert()
{
cout<<"1.At beginning"<<endl
<<"2.At particular position"<<endl
<<"3.At ending"<<endl;
int ch;
cout<<"enter your choice : ";
cin>>ch;
switch(ch)
{
case 1: n++;
for(i=n-1;i>=0;i--)
a[i+1]=a[i];
cout<<"enter the element you want to insert : ";
cin>>a[0];
break;
case 2: cout<<"enter position value : ";
cin>>p;
p--;
if(p>n)
cout<<"position value is greater than array size ";
else
{
n++;
for(i=n-1;i>=p;i--)
a[i+1]=a[i];
cout<<"enter the element you want to insert : ";
cin>>a[p];
}
break;
case 3: n++;
cout<<endl<<"enter the element you want to insert";
cin>>a[n];
}
}
void array::del()
{
cout<<"1.At beginning"<<endl
<<"2.At particular position"<<endl
<<"3.At ending"<<endl
<<"enter your choice : ";
int ch;
cin>>ch;
switch(ch)
{
case 1: for(i=0;i<=n;i++)
a[i]=a[i+1];
n--;
break;
case 2: cout<<"enter position value : ";
cin>>p;
p--;
if(p>n)
cout<<"position value is greater than array size";
else
{
for(i=p;i<=n;i++)
a[i]=a[i+1];
n--;
}
break;
case 3: n--;
}
}
void array::display()
{
for(i=0;i<=n;i++)
cout<<" "<<a[i];
}
void array::menu()
{
int ch;
while(1)
{
cout<<endl
<<"Array operation menu"<<endl
<<"1.insert"<<endl
<<"2.delete"<<endl
<<"3.display"<<endl
<<"4.exit"<<endl
<<"enter your choice : ";
cin>>ch;
switch(ch)
{
case 1: insert();
break;
case 2: del();
break;
case 3: display();
break;
case 4: exit(1);
break;
default:
cout<<"sorry! invalid choice";
}
}
}
void main()
{
clrscr();
array a;
a.menu();
getch();
}
No comments:
Post a Comment