Você está na página 1de 5

Question 19

#include <iostream>
#include<bits/stdc++.h>
using namespace std;
void high(int a[],int m)
{ int max1=0,max2=0,max3=0,low1,low2,low3;
for(int i=0;i<m;i++)
{
if(max1<a[i])
{
max1=a[i];
}
}
for(int i=0;i<m;i++)
{
if(a[i]!=max1)
{ if(max2<a[i])
{
max2=a[i];
}
}
}
for(int i=0;i<m;i++)
{
if(a[i]!=max1&&a[i]!=max2)
{
if(max3<a[i])
{
max3=a[i];
}
}
}
low1=low2=low3=max1;
for(int i=0;i<m;i++)
{
if(low1>a[i])
{
low1=a[i];
}

}
for(int i=0;i<m;i++)
{
if(a[i]!=low1)
{
if(low2>a[i])
{
low2=a[i];
}
}
}
for(int i=0;i<m;i++)
{
if(a[i]!=low1&&a[i]!=low2)
{
if(low3>a[i])
{
low3=a[i];
}
}
}
cout<<"\nfirst highest number of the array="<<max1;
cout<<"\nsecond highest number of the array="<<max2;
cout<<"\nthird highest number of the array="<<max3;
cout<<"\nfirst lowest number of the array="<<low1;
cout<<"\nsecond lowest number of the array="<<low2;
cout<<"\nthird lowest number of the array="<<low3;
}
void binsearch(int a[],int n)
{ int m;
cout<<"\nEnter the element to be searched";
cin>>m;
for(int i=0;i<n;i++)
{
if(a[i]==m)
cout<<"\nElement found at position"<<i+1;
}

}
void sorta(int a[],int n)
{ sort(a,a+n);
cout<<"\n";
for(int i=0;i<n;i++)
{
cout<<a[i]<<" ";
}
}
void sortd(int a[],int n)
{ sort(a,a+n);
cout<<"\n";
for(int i=0;i<n;i++)
{
cout<<a[n-i]<<" ";
}
}
void prime(int a[],int n)
{ int ctr,m,b[n];
for(int i=0;i<n;i++)
{ ctr=0;
for(int j=2;j<a[i];j++)
{
if(a[i]%j==0)
{
ctr++;
}
if(ctr==0&&a[i]!=1)
{
m++;
a[i]=b[m];
cout<<"\n";
cout<<b[m]<<" ";
}
}

}
}
void edit(int a[],int n)
{ int temp[n+1],m,ele;
cout<<"\nEnter the element to be entered";
cin>>ele;
cout<<"at position?";
cin>>m;
for(int i=0;i<m-1;i++)
{
a[i]=temp[i];
}
temp[m-1]=ele;
for(int i=m;i<=n;i++)
{
temp[i]=a[i-1];
}
}
void del(int a[],int n)
{ int m;
cout<<"\nEnter the position of element to be entered";
cin>>m;
for(int i=m;i<n-1;i++)
{
a[i]=a[i+1];
}
}
void rev(int a[],int n)
{ int temp,i,j;
if(n%2==0)
{
for(i=0,j=((n/2)-1);i<j;++i,--j)
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
for(j=n-1,i=(n/2);i<j;i++,--j)
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
if(n%2!=0)
{
for(i=0,j=((n-1)/2)-1;i<j;i++,--j)
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
for(j=n-1,i=((n-1)/2);i<j;i++,--j)
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
cout<<"\n";
for(i=0;i<n;i++)
cout<<a[i]<<" ";
}

int main()
{
int n,a[20],ch;
cout<<"\nEnter number of elements";
cin>>n;
cout <<"\nInput Array";
for(int i=0;i<n;i++)
{
cin>>a[i];
}
cout<<"\n Available actions for the array";
cout<<"\n1.Display 3 highest and lowest elements of the array";
cout<<"\n2.Search for an element in the array";
cout<<"\n3.Sort the array in ascending or descending order";
cout<<"\n4.Find the prime in the array and store them in another array(prime)";
cout<<"\n5.Insert a number at a desired position";
cout<<"\n6.Delete a number from a desired position";
cout<<"\n7.Reverse half of the array";
cin>>ch;
switch(ch)
{
case 1: high(a,n);
break;
case 2: binsearch(a,n);
break;
case 3: sorta(a,n);
break;
case 4: sortd(a,n);
break;
case 5: edit(a,n);
break;
case 6: del(a,n);
break;
case 7: rev(a,n);
break;
}

return 0;
}

Você também pode gostar