In this program we will learn how to insert data in an array, how to show that data, how to allocate memory and insert new data in an existed data, and how to delete a specific index.
The code is given below:
//Code starts
#include<iostream>
#include<conio.h>
using namespace std;
class array{
private:
int a[10],size;
public:
array();
void create();
void print();
void insertion(int data,int index);
void deletion(int index);
};
array::array()
{
size=5;
}
void array::create()
{
for(int i=0;i<size;i++)
{
cout<<"Enter data in index ["<<i<<"] : ";
cin>>a[i];
}
}
void array::print()
{
cout<<"Your entered data "<<endl;
for(int i=0;i<size;i++)
{
cout<<"Data at index ["<<i<<"] : "<<a[i]<<endl;
}
}
void array::insertion(int data,int index)
{
for(int i=size-1;i>=index;i--)
{
a[i+1]=a[i];
}
size++;
a[index]=data;
cout<<"New data after insertion at index["<<index<<"]"<<endl;
for(int i=0;i<size;i++)
{
cout<<"Index ["<<i<<"] = "<<a[i]<<endl;
}
}
void array::deletion(int index )
{
for(int i=index+1;i<=size-1;i++)
{
a[i-1]=a[i];
}
size--;
cout<<"After deleting index["<<index<<"]"<<endl;
for(int i=0;i<=size-1;i++)
{
cout<<"Index ["<<i<<"] = "<<a[i]<<endl;
}
}
int main()
{
array a;
int x;
do{
cout<<"Enter 1 to enter data : "<<endl;
cout<<"Enter 2 to show entered data : "<<endl;
cout<<"Enter 3 to insert data in array : "<<endl;
cout<<"Enter 4 to delete data in array : "<<endl;
cout<<"Enter 5 to exit from program : "<<endl;
cout<<" Value = ";
cin>>x;
switch (x)
{
case 1:
a.create();
break;
case 2:
a.print();
break;
case 3:
int n,b;
cout<<"Enter data : ";
cin>>n;
cout<<"Enter index : ";
cin>>b;
a.insertion(n, b);
break;
case 4:
int v;
cout<<"Enter index to be deleted : ";
cin>>v;
a.deletion(v);
break;
case 5:
cout<<"Program terminated "<<endl;
return 1;
break;
}
}
while(1);
getch();
}
//Code Ends
OUTPUT:
The code is given below:
//Code starts
#include<iostream>
#include<conio.h>
using namespace std;
class array{
private:
int a[10],size;
public:
array();
void create();
void print();
void insertion(int data,int index);
void deletion(int index);
};
array::array()
{
size=5;
}
void array::create()
{
for(int i=0;i<size;i++)
{
cout<<"Enter data in index ["<<i<<"] : ";
cin>>a[i];
}
}
void array::print()
{
cout<<"Your entered data "<<endl;
for(int i=0;i<size;i++)
{
cout<<"Data at index ["<<i<<"] : "<<a[i]<<endl;
}
}
void array::insertion(int data,int index)
{
for(int i=size-1;i>=index;i--)
{
a[i+1]=a[i];
}
size++;
a[index]=data;
cout<<"New data after insertion at index["<<index<<"]"<<endl;
for(int i=0;i<size;i++)
{
cout<<"Index ["<<i<<"] = "<<a[i]<<endl;
}
}
void array::deletion(int index )
{
for(int i=index+1;i<=size-1;i++)
{
a[i-1]=a[i];
}
size--;
cout<<"After deleting index["<<index<<"]"<<endl;
for(int i=0;i<=size-1;i++)
{
cout<<"Index ["<<i<<"] = "<<a[i]<<endl;
}
}
int main()
{
array a;
int x;
do{
cout<<"Enter 1 to enter data : "<<endl;
cout<<"Enter 2 to show entered data : "<<endl;
cout<<"Enter 3 to insert data in array : "<<endl;
cout<<"Enter 4 to delete data in array : "<<endl;
cout<<"Enter 5 to exit from program : "<<endl;
cout<<" Value = ";
cin>>x;
switch (x)
{
case 1:
a.create();
break;
case 2:
a.print();
break;
case 3:
int n,b;
cout<<"Enter data : ";
cin>>n;
cout<<"Enter index : ";
cin>>b;
a.insertion(n, b);
break;
case 4:
int v;
cout<<"Enter index to be deleted : ";
cin>>v;
a.deletion(v);
break;
case 5:
cout<<"Program terminated "<<endl;
return 1;
break;
}
}
while(1);
getch();
}
//Code Ends
OUTPUT:
0 comments:
Post a Comment