0
The largest two number
How to find the two largest values among the 10 number in c++. Numbers input by the user.
1 Resposta
+ 5
There are different ways how to go about this.
One way would be to find the largest number, save it and ignore it the second time you look for the biggest number, which will then give you the second-largest number. Finding the largest number itself is rather easy:
Save the first number as a variable, then iterate over all numbers, and whenever one is bigger than the one you saved in your variable, you update that variable.
Another solution would be to sort the array, resulting in the largest number being ordered at the beginning or ending, depending on how you do the sorting. Then you can simply output as many as you desire.
If you haven't implemented too many algorithms yet, I would advise for the first method though, as it's probably simpler to understand and execute.