NevLabs

Under Construction

NevLabs

Under Construction

NevLabs

Under Construction

NevLabs

Under Construction

NevLabs

Under Construction

Showing posts with label Array operations. Show all posts
Showing posts with label Array operations. Show all posts

Thursday, August 16, 2012

C++ program to perform array operations


//Program to create array,display array,search for an element and delete
#include<iostream.h>
#include<conio.h>
#include<process.h>
class array
{
int a[100];
int terms;
public:
array();
void create();
void display();
void search();
void delete();
};
array::array()
{terms=0;}
//create an array
void array::create()
{
int i;
cout<<"How many elements in the array:";
cin>>terms;
if((terms>100)||(terms<0))
{
cout<<"Sorry! the size does not support";
getch();
return;
}
{cout<<"enter the element";
for(i=0;i<terms;++i)
cin>>a[i];
}}
//Display the content of the array
void array::display()
{int i;
for(i=0;i<terms;i++)
cout<<a[i]<<" ";
}
//search
void array::search()
{int m;
cout<<"enter the element to search:";
cin>>m;
for(int i=0;i<terms;i++)
if(a[i]==m)
{cout<<m<<" is found in "<<i+1<<" position";break;}
else
if(i==terms)
cout<<"not found";
}
//Delete
void array::delete()
{int p;
cout<<"enter the position to delete:";
cin>>p;
if((p>terms)||(p<1))
cout<<"Position invalid!";
else
{terms--;
for(int i=0;i<terms;i++)
a[i]=a[i+1];
}}

void main()
{ clrscr();
array ar;
abcd:
cout<<"\n\nARRAY\n";
cout<<".........\n";
cout<<" 1.create\n 2.Display \n 3.Search\n 4.Delete \n 5.Exit\n\n";
cout<<"Enter your option...";

int inp;
 cin>>inp;
switch(inp)
{ case 1: ar.create();goto abcd;
 case 2: ar.display();goto abcd;
 case 3: ar.search();goto abcd;
 case 4: ar.delete();goto abcd;
 case 5: exit(0);
}
}