I need some help to be able to handle the following logic. The program gets many integers input like 10,16,3,17,21,29,6.
Logic to be done:
Scenario 1:
First select the biggest 4 numbers of input which is 16,17,21,29. Now assign the values to A,B,C and D:
- A = smallest in the selected 4
- B = biggest in the selected 4
- C =
second smallest in the selected 4 - D = third smallest in the selected
4
Result to be Displayed:
A = 16
B = 29
C = 17
D = 21
Scenario: 2
If the user gives 3 inputs like 3,6,10 assign only to A,B,C and should ignore D
Result to be Displayed:
A = 3
B = 10
C = 6
Assuming that you have your input values in an array, you can sort it using the static
Array.Sort()method and then pick the top/bottom ones by indexing (eg.values[value.Length - 1]gets the highest value). Do make sure to do some bounds checking to avoid runtime exceptions, and to solve “scenario 2” of the assignment correctly.If you want something more modern, you could also use some LINQ magic to get the top 4 items:
The four highest values are now in
highOnesfor you to print in whatever order you please. Once again, make sure to do some bounds checking – there might be less than four numbers in the array.