Second largest number in array in C++ by R4R Team

Array:
Array is the collection of different values having same data type.
-We have a integer Array that having all integer values.
-Character array is known as String.

Example-
Array is {3,2,4,1,5}
Second largest element is 4

program:

#include < iostream>
using namespace std;
class r4r
{
public:
//class method or function
int second_largest(int *a,int n)
{
int i,j,t;
for(i=0;i< n;i++)
{
for(j=i;j< n;j++)
{
if(a[j]< a[i])
{
t=a[i];
a[i]=a[j];
a[j]=t;
}
}
}
cout<<"Second largest number in array is: "<< a[n-2];
}
};
int main()
{
int a[100],i,n;
r4r obj;
cout<<"Enter number of element in array?"<< endl;
cin>>n;
cout<<"Enter number in array"<< endl;
for(i=0;i< n;i++){
cin>>a[i];
}
//pass in function
obj.second_largest(a,n);
return 0;
}


output-

Enter number of element in array
5
Enter number in array
6 2 4 1 9
Second largest number in array is: 6




Leave a Comment:
Search
Categories
R4R Team
R4Rin Top Tutorials are Core Java,Hibernate ,Spring,Sturts.The content on R4R.in website is done by expert team not only with the help of books but along with the strong professional knowledge in all context like coding,designing, marketing,etc!