Click here to Skip to main content
15,887,812 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i want to find the largest and 2nd largest but the second largest answer is not correct.

What I have tried:

#include <iostream>
using namespace std;
int main()
{
	int number, largest = 0, seclargest = 0;
	for (int i = 0; i < 4; i++)
	{
		cout << "Enter Any Four Numbers ";
		cin >> number;
		if (number>largest)
		{
			largest = number;
		}
		if (number > seclargest && number < largest)
		{
			seclargest = number;
		}
	}
		cout << largest << "\n" << seclargest;
	return 0;
}
Posted
Updated 14-Feb-18 9:47am
v2

Stop and think about what you are doing.
When you enter a number, it could be the largest so far, or the second largest, or smaller than either.

So you need two independent tests:

Is it larger than the largest?
If it is, then the previous largest is automatically the second largest, and teh largest becomes the new value.
Other wise, is it larger than teh second largest?
If it is, the second largest becomes the new value
Otherwise, ignore it.

That gives you the results you want, but I'd argue that 0 is a bad value to initialize to. I would use the constant value INT_MIN: C++ Integer Limits[^] which will give you the smallest possible value that can be entered - which may be a lot smaller than zero!
 
Share this answer
 
Comments
Maciej Los 14-Feb-18 13:22pm    
Short and to the point!
You have The Hammer, use it:
C++
#include <iostream>
#include <queue>
using namespace std;

int main()
{
  priority_queue<int > pq;
  for (int n=0; n<4; ++n)
  { 
    cout << "please enter  number " << (n+1) << "/4" << endl;
    int x; 
    cin >> x;
    pq.push(x);
  }
  
  cout << "reporting the biggest ones:" << endl;
  for (int n=0; n<2; ++n)
  { 
    cout << pq.top() << endl;
    pq.pop();
  }
}
 
Share this answer
 
Comments
jeron1 14-Feb-18 15:17pm    
+5, I was initially thinking std::sort and std::unique, but your solution is tidier. I do enjoy your use of containers and algorithms.
CPallini 14-Feb-18 16:14pm    
Thank you very much. :-)
First thing to do is problem analyze:
- Take sheet of paper and a pencil. Write down how largest and second evolve when a new number comes depending on it value, this is your algorithm.

If the program don't give expected results, something is wrong in program, it is time to turn to debugger.
-----
There is a tool that allow you to see what your code is doing, its name is debugger. It is also a great learning tool because it show you reality and you can see which expectation match reality.
When you don't understand what your code is doing or why it does what it does, the answer is debugger.
Use the debugger to see what your code is doing. Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.
-----
- Learn one or more analyze methods, E.W. Djikstra top-Down method is a good start.
https://en.wikipedia.org/wiki/Top-down_and_bottom-up_design[^]
https://en.wikipedia.org/wiki/Structured_programming[^]
https://en.wikipedia.org/wiki/Edsger_W._Dijkstra[^]
https://www.cs.utexas.edu/users/EWD/ewd03xx/EWD316.PDF[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900