-
Notifications
You must be signed in to change notification settings - Fork 0
/
second_largest_array.cpp
46 lines (36 loc) · 1016 Bytes
/
second_largest_array.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
/******************************************************************************
Online C++ Compiler.
Code, Compile, Run and Debug C++ program online.
Write your code in this editor and press "Run" button to compile and execute it.
*******************************************************************************/
#include<iostream>
using namespace std;
int main()
{
int i,j,max,n;
cout<<"enter the size of an array: ";
cin>>n;
int arr[n];
cout<<"size of an array is: "<<n<<endl;
cout<<"enter the elements:";
for(i=0;i<n;i++)
{
cin>>arr[i];
}
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(arr[i]>arr[j])
{
max = arr[i];
arr[i] = arr[j];
arr[j] = max;
}
}
}
cout<<"Largest element in the array is: "<<max;
cout<<"\n";
cout<<"Second largest element in the array is: "<<arr[n-2];
return 0;
}