#include <iostream>
usingnamespace std;
main( )
{
int n, c;
float v, l = 0.0, s = 0.0;
cout << "SECOND LARGEST NUMBER of n values\n\n";
cout << "Number of values to input: ";
cin >> n;
cout << "\nEnter values...\n";
c = 1;
while( c <= n )
{
cout << "Value " << c << ": ";
cin >> v;
if( c==1 )
l = s = v;
if( v > l )
{
s = l;
l = v;
}
elseif( v > s || l == s )
s = v;
c = c + 1;
}
cout << "\nSecond largest value " << s << endl;
return 0;
}
#include <iostream>
int main()
{
int i, N;
double val, largest = -10000000, second_largest = -10000000;
cout << "SECOND LARGEST NUMBER of n values\n\n";
do
{
cout << "Number of values to input: ";
cin >> N;
if(N <= 2)
{
cout << "The number of values must be 2 or higher! Please try again\n\n";
}
}
while(N <= 2);
i = -1;
while(++i < N)
{
cout << "\nEnter values...\n";
cin >> val;
if(val > largest)
{
second_largest = largest; largest = val;
}
elseif(val > second_largest)
{
second_largest = val;
}
}
cout << endl;
cout << "The largest value " << largest << endl;
cout << "The second largest value " << second_largest << endl;
return 0;
}
1. Read 'n' numbers: while (std::cin >> n) [CTRL-Z] to stop reading
2. store the input in a std::vector container.
3. Use the std::sort function to sort the values in numeric order #include<algorithm>
4. grab the 2nd to last element, or sort in reverse order and get the 2nd element.