-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfinding the second largest number in an array.cpp
More file actions
72 lines (58 loc) · 1.74 KB
/
finding the second largest number in an array.cpp
File metadata and controls
72 lines (58 loc) · 1.74 KB
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
// Program to find the second largest number in an array
#include <iostream>
using namespace std;
int main()
{
int n, num[50], largest, second;
cout<<"Enter number of elements:- " << endl;
cin>>n;
for(int i=0; i<n; i++){
cout<<"Enter Array Element"<<(i+1)<<": ";
cin>>num[i];
}
/* Here we are comparing first two elements of the
* array, and storing the largest one in the variable
* "largest" and the other one to "second" variable.
*/
if(num[0]<num[1])
{
largest = num[1];
second = num[0];
}
else
{
largest = num[0];
second = num[1];
}
for (int i = 2; i< n ; i ++)
{
/* If the current array element is greater than largest
* then the largest is copied to "second" and the element
* is copied to the "largest" variable.
*/
if (num[i] > largest)
{
second = largest;
largest = num[i];
}
/* If current array element is less than largest but greater
* then second largest ("second" variable) then copy the
* element to "second"
*/
else if (num[i] > second && num[i] != largest)
{
second = num[i];
}
}
cout<<"Second Largest Element in array is: "<<second;
return 0;
}
/*
Algorithm:-
* We need to find the second highest number in an array.
* The program will scan through each element of the array one by one.
* It will keep two variables to hold the highest and the second highest number.
* For each element, it will check if it is bigger than the highest number or not.
* If yes, it will update both highest and the second highest numbers.
* At the end of the loop, we can print the second highest number of the array.
*/