• Post Reply Bookmark Topic Watch Topic
  • New Topic

This is a code to find the 3rd max no.  RSS feed

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
   

I am not getting correct output
 
Sheriff
Posts: 4931
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Look what your code does:

Let's assume these were random numbers (line 2 represents max1, min1, line 3 represents max2, min2).
Do you see where the problem is? Your algorithm compares 7 and 4, and gives answer 7, while actual answer supposed to be 6. Think about other approach.

Interesting code formatting and indentation.
 
Bartender
Posts: 572
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you want to find the maximum number, you need a variable to store the currently found max.
If you want to find the second most maximum number, you need 2 variables - the current max and the current 2nd max.
So how many variables will you need to find the 3rd maximum number?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!