Array in data structure

Advertisements

Prev Tutorial Next Tutorial

Array in Data Structure in C++

An Array is a collection of similar data type value in a single variable. An array is a derived data type in C, which is constructed from fundamental data type of C language.

array

Insert Element in Array at Specific Position in C++

Insert Element in Array in C++

	
#include<iostream.h>
#include<conio.h>

void main()
  {
  int i,a[5],no,pos;
  clrscr();
  cout<<"Enter element in array: ";
  for(i=0;i<5;i++)
  {
  cin>>a[i];
  }
  cout<<"\nStored element in array: ";
  for(i=0;i<5;i++)
  {
  cout<<a[i];
  }
  cout<<"\nEnter position for enter element: ";
  cin>>pos;
  if(pos>5)
  {
  cout<<"\nThis is out of range";
  }
  else
  {
  cout<<"\nEnter new element: ";
  cin>>no;
  --pos;
  for(i=5;i>=pos;i--)
  {
  a[i+1]=a[i];
  }
  a[pos]=no;
  cout<<"\nStored data in array: ";
  for(i=0;i<6;i++)
  {
  cout<<a[i];
  }
  }
  getch();
  }

Output

	
Enter elements in array: 5
4
6
7
2
8
Stored data in array: 2 4 6 7 8
Enter position for enter element: 3
Enter new element: 11
Stored data in array: 4 2 5 7 8

Sort Array Elements in C++

Sort Array Element in C++

#include<iostream.h>
#include<conio.h>

void main()
{
int i,a[10],temp,j;
clrscr();
cout<<"Enter any 10 num in array: \n";
for(i=0;i<=10;i++)
{
cin>>a[i];
}
cout<<"\nData before sorting: ";
for(j=0;j<10;j++)
{
cout<<a[j];
}
for(i=0;i<=10;i++)
{
for(j=0;j<=10-i;j++)
{
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
cout<<"\nData after sorting: ";
for(j=0;j<10;j++)
{
cout<<a[j];
}
getch();
}

Output

	
Enter any 10 num in array:
10
50
30
20
60
80
70
90
100
Data before sorting: 10 50 30 20 60 80 70 90 100
Data after sorting: 10 20 30 40 50 60 70 80 90 100

Delete Array Element at Specific Position in C++

Delete element from Array in C++

	
#include<stdio.h>
#include<conio.h>

void main()
 {
 int i,a[5],no,pos;
 clrscr();
 cout<<"Enter element in array: ";
 for(i=0;i<5;i++)
 {
 cin>>a[i];
 }
 cout<<"\nStored element in array: ";
 for(i=0;i<5;i++)
 {
 cout<<a[i];
 }
 cout<<"\nEnter poss. of element to delete: ";
 cin>>pos;
 if(pos>5)
 {
 cout<<"\nThis value is out of range ";
 }
 else
 {
 --pos;
 for(i=pos;i<=4;i++)
 {
 a[i]=a[i+1];
 }
 cout<<"\nAfter deletion elements in array: ";
 for(i=0;i<4;i++)
 {
 cout<<a[i];
 }
 }
 getch();
 }
 

Output

	
Enter element in array: 10 30 50 20
Stored element in array: 10 30 50 20
Enter poss. of element to delete: 3
After deletion elements in array: 10 30 20

Prev Tutorial Next Tutorial

Google Advertisment

Buy This Ad Space @$20 per Month, Ad Size 600X200 Contact on: hitesh.xc@gmail.com or 9999595223

Magenet is best Adsense Alternative here we earn $2 for single link, Here we get links ads. Magenet

For Projects 9999595223

Google Advertisements


Buy Websites 9999595223

Buy College Projects with Documentation Contact on whatsapp 9999595223. Contact on: hitesh.xc@gmail.com or 9999595223 Try this Keyword C++ Programs

Advertisements