Showing posts with label Array. Show all posts

C++ Program to Find Duplicate Elements in Array


Array is the collection of similar data type, In this program we find duplicate elements from an array, Suppose array have 3, 5, 6, 11, 5 and 7 elements, in this array 5 appear two times so this is our duplicate elements.

Find Duplicate Elements in Array in C++

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

 void main()
 {
  int i,arr[20],j,no;
  clrscr();
  cout<<"Enter Size of array: ";
  cin>>no;
  cout<<"Enter any "<<no<<" num in array: ";
  for(i=0;i<no;i++)
  {
   cin>>arr[i];
  }
  cout<<"Dublicate Values are: ";
  for(i=0; i<no; i++)
   {
    for(j=i+1;j<no;j++)
    {
    if(arr[i]==arr[j])
    {
    cout<<"\n"<<arr[i];
    }
   }
   }
  getch();
 }

Output

Enter Size of Array : 5
Enter any 5 elements in Array: 
5 4 5 2 3 
Duplicate Elements are: 
5

C++ Program to Find Sum of an Array Elements


Sum of all Array elements means add all array Elements. Suppose we have 4 Elements in array and we want to find there sum.
arr[0]=4
arr[1]=2
arr[2]=1
arr[3]=6
Sum off all above elements are
arr[0]+arr[1]+arr[2]+arr[3]=4+2+1+6=13

C++ program to Find Sum of an Array Elements


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

 void main()
  {
   int arr[20],i,n,sum=0;
   clrscr();
   cout<<"How many elements you want to enter: ";
   cin>>n;
   cout<<"Enter any "<<n<<" elements in Array: ";
   for(i=0;i<n;i++)
   {
   cin>>arr[i];
   }
   cout<<"Sum of all Elements are: ";

   for(i=0;i<n;i++)
   {
    sum=sum+arr[i];
   }
   for(i=0;i<n;i++)
   {
   }
  cout<<sum;
  getch();
  }

Output

How many elements you want to enter : 5
Enter any 5 elements in Array: 
1 4 2 7 5
Sum of all Elements are: 19 

C++ Program to Reverse an Array


Array store all data in array on the basis of index. For reverse an array element you nedd to interchange elements of array on the basis of index value. .

C++ Program to Reverse an Array


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

 void main()
  {
   int a[20],b[20],i,j,n;
   clrscr();
   cout<<"How many elements you want to enter: ";
   cin>>n;
   cout<<"Enter any "<<n<<" elements in Array: ";
   for(i=0; i<n ;i++)
   {
   cin>>a[i];
   }
   cout<<"Reverse of Array: ";

   for(i=n-1,j=0; i>=0;i--,j++)
   {
    b[i]=a[j];
   }
   for(i=0; i<n ;i++)
   {
   cout<<b[i];
   }
  getch();
  }

Output

How many elements you want to enter : 5
Enter any 5 elements in Array: 
1 4 2 7 5
Reverse of Array: 5 7 2 4 1

C++ Program to Find Even and Odd Elements in Array


For Separate Even and Odd Elements of Array we take three array and one array for insert all Array Elements second for even elements and third for odd elements. We check condition for odd and even Elements arr[]%2==0.

Example


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

 void main()
  {
   int arr[20],even[20],odd[20],i,j=0,k=0,no;
   clrscr();
   cout<<"How Size of Array: ";
   cin>>no;
   cout<<"Enter any "<<no<<" elements in Array: ";
   for(i=0; i<no;i++)
   {
   cin>>arr[i];
   }
   for(i=0; i<no;i++)
   {
   if(arr[i]%2==0)
   {
    even[j]=arr[i];
    j++;
   }
   else
   {
   odd[k]=arr[i];
   k++;
   }
   }
  cout<<"\nEven Elements: ";
  for(i=0; i<j ;i++)
   {
    cout<<even[i]<<"  ";
   }
  cout<<"\nOdd Elements: ";
  for(i=0; i<k; i++)
   {
    cout<<odd[i]<<"  ";
   }
  getch();
  }

Output

Enter Size of Array : 5
Enter any 5 elements in Array: 
10 4 5 2 7 
Even Elements: 10 4 2
Odd Elements: 7 5

C++ Program to Delete an element from Array

C++ Program to Delete an element from Array

Delete an element in an array from specific position


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

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

Output

Enter data in array: 10 20 30 40 50
Stored Data in array: 10 20 30 40 50
Enter poss. of element to delete: 2
New data in array: 10 20 40 50

C++ program to Insert an element in an array at specific position

C++ program to Insert an element in an array at specific position

Insert an element in an array at specific position on the basis of index value.

Example


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

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

Merge an array

C++ Program to Merge two Arrays in third array

In this program we enter an elements in any two array and then these two array (elements of array) are store in third array.

Example

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

void main()
 {
   int a[10],b[10],c[20],i;
   clrscr();
   cout<<"Enter Elements in 1st Array: ";
   for(i=0;i<10;i++)
   {
   cin>>a[i];
   }
   cout<<"Enter Elements in 2nd Array: ";
   for(i=0;i<10;i++)
   {
   cin>>b[i];
   }
   cout<<"\nElements of Array After Merge: ";
   for(i=0;i<10;i++)
   {
    c[i]=a[i];
    c[i+10]=b[i];
   }
   for(i=0;i<20;i++)
   {
   cout<<c[i];
   }
  getch();
 }
Output
merge tow array

C++ Program to Pass Array in Function


In below program we pass array in function as an arguments and finally print all elements of an array.

Example


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

void pass(int[],int);
void main()
{
int a[]={1,2,3,4,5};
clrscr();
pass(a,5);
getch();
}
void pass(int b[],int n)
{
int i;
for(i=0;i<n;i++)
{
cout<<"/n"<<b[i];
}
}

Output

1
2
3
4
5
Output
pass array