You are on page 1of 4

//arrayope

#include<iostream.h>

#include<conio.h>

class arrays

int n, arr[50];

public:

void getdata();

void traverse();

void search(int);

void delet(int);

void insert(int,int);

};

void arrays::getdata()

int i;

cout<<"Getdata\n";

cout<<"Enter the size of list";

cin>>n;

for(i=0;i<n;i++)

cin>>arr[i];

void arrays::traverse()

cout<<"traverse\n";

cout<<"Elements are";

for(int i=0;i<n;i++)

cout<<"arr["<<i<<"]="<<arr[i]<<endl;

void arrays::search(int x)

{
cout<<"search"<<x<<"\n";

for(int i=0;i<n;i++)

if(x==arr[i])

cout<<x<<"is at position"<<i<<endl;

cout<<"arr["<<i<<"]="<<arr[i];

break;

if(i==n)

cout<<x<<" is not found\n";

void arrays::delet(int pos)

//cout<<"delete"<<arr[pos-1]<<"\n";

if(pos>n||pos<1)

cout<<"incorrect no"<<pos;

else

cout<<"Deleting succesfully"<<arr[pos-1]<<"\n";

for(int i=pos-1;i<n-1;i++)

arr[i]=arr[i+1];

n--;

void arrays::insert(int m,int pos)

{ int i;

cout<<"insert"<<m<<" at position "<<pos<<"\n";


for(i=n-1;i>=pos;i--)

arr[i+1]=arr[i];

arr[pos]=m;

n++;

void main()

arrays a;

int ch;

do

clrscr();

cout<<"0:Exit from the program\n";

cout<<"1:Read the array\n";

cout<<"2:Traverse the array\n";

cout<<"3:search an element\n";

cout<<"4:delete element at given position\n";

cout<<"5:insert given element\n";

cout<<"Enter your choice\n";

cin>>ch;

switch(ch)

case 0:cout<<"Closing the program\n";

break;

case 1:a.getdata();

break;

case 2:a.traverse();

break;

case 3:{

cout<<"Enter number to search";

int x;
cin>>x;

a.search(x);

break;

case 4:{int pos;

cout<<"Delete\n";

cout<<"Enter the position to delete";

cin>>pos;

a.delet(pos);

break;

case 5:{

cout<<"insert\n";

int m,pos;

cout<<"Enter the num to insert";

cin>>m;

cout<<"Enter the position to insert\n";

cin>>pos;

a.insert(m,pos);

break;

default:cout<<"Enter correct choice (0 to 5)\n";

getch();

} while(ch!=0);

getch();

You might also like