finding 2nd largest number from 5 given numbers

write a C++ program to find the 2nd largest no. from given 5 numbers
Last edited on
Sort and pick the second.
can you please write the program which finds the 2nd largest number from given 5 numbers
Last edited on
Most People wont just hand you code for the hell of it. So ill give you a start.


First you have to select a library that you want to transfer in to your .cpp or program file.

#include <[library name]>

Then after that you have the option of using namespaces or function's or w.e you want.

using namespace [insert namespace];

After that you have to get down to the program, so to tell your c++ compiler to start at a certain point you use a function by the name of main.

1
2
3
4
5
[insert type] main ([insert w.e])
{


} 


Now inside the curly brackets you will have to write what you want c++ to do/ launch/ accomplish.

Most commonly sense you wish to deal with number's, it is recommended to create a variable with a type specifying to you number's type.
int, long, double,or float(sense you wish to use 5 different number you need 5 different variable or a array with 5 element's. But to keep it simple i would recommend the use of a array with 5 element's);

1
2
3
4
5
[insert type] main ([insert w.e])
{
float [insert Array][elements] ;

} 


Now that you have a array with five element's(no value has been given to them yet so they are called unsigned) we can manipulate them how ever we want.

Post what you think would be required to accomplish what your asking for.

1
2
3
4
5
6
7
8
[insert type] main ([insert w.e])
{
float [insert Array][elements] ;
blah blah blah
blah blah blah
blah blah blah
blah blah blah
} 
I can suggest the following algorithm


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
#include <iostream>

int main()
{
   int a, b, c, d, e;
   
   std::cout << "Enter 5 integer numbers: ";
   std::cin >> a >> b >> c >> d >> e;

   int max1, max2;

   if ( a < b ) max1 = b, max2 = a;
   else max1 = a, max2 = b;

   if ( max1 < c ) max2 = max1, max1 = c;
   else if ( max2 < c ) max2 = c; 

   if ( max1 < d ) max2 = max1, max1 = d;
   else if ( max2 < d ) max2 = d; 

   if ( max1 < e ) max2 = max1, max1 = e;
   else if ( max2 < e ) max2 = e;

   std::cout << "The second maximum value = " << max2 << std::endl;

   return ( 0 );
} 



Only I did not test the code.
Topic archived. No new replies allowed.