15,959,936 members
1.00/5 (1 vote)
See more:
Hello everyone,

I am trying to develop my "problem solving" ability. I spent too much time. However, I cant find a solution finding the max 3 elements of a 2 dimensional array. For example,

1 12 4
7 9 15
2 2 2

is the array. I want to get 12,15 and 9 as output.
Posted

## Solution 1

Use a nested loop to go through the 2d array then you can ask,
if actual value is bigger than the first maximum, save the second maximum in the third position and the current first maximum in the secon position, then save actual value in first position. Then check the next number.
if actual value is smaller than first max, but bigger than second, the same starting directly in pos 2
if actual value is smaller than second max and bigger than third, then save actual value in 3rd position and check next.

To code that should be your work. If you have problems, then you can ask, but show us what you are trying.
If you want to ask other thing related to this, then please use the widget "improve questio" and edit the message, don't add a new question or a new solution. If you want to directly speak with me, then use "have a question or comment?"

v2
FoxRoot 12-Nov-12 10:47am
Thank you Nelek But I couldn't do it. Could you write the code itself. I will get a look and understand clearly.
Nelek 12-Nov-12 10:50am
No offense... but no. I am not going to write the code for you. The code needed to do that is very basic. If you want to know why I don't do it? then please read this:
what have you tried?[^]
Homework[^]
FoxRoot 12-Nov-12 10:55am
Thank you Nelek. You are a good teacher. I will pay more attention then If I can't solve it again, I will post "what I did."
Have a nice day.
Nelek 12-Nov-12 11:00am
Sergey Alexandrovich Kryukov 12-Nov-12 14:47pm
--SA

## Solution 3

VB
```Sub Find_max(ByVal a As Integer(,))
Dim i, j As Integer
Dim newArray As Integer() = New Integer(9) {}

For i = 0 To 2
For j = 0 To 2
newArray(3 * i + j) = a(i, j)
Next
Next

Array.Sort(newArray)

For index2 = 9 To 7 Step -1
Console.Write(newArray(index2) & vbTab)
Next```

lewax00 13-Nov-12 13:12pm
That looks like it should work. See, you can figure it out! :)
Sergey Alexandrovich Kryukov 13-Nov-12 14:19pm
Yes, it would work, still the solution is unsatisfactory -- please see my comment.
--SA
Sergey Alexandrovich Kryukov 13-Nov-12 14:18pm
Well, for such a simple problem, a solution could be much better.

First, a real bad thing is the immediate constants 2, 7 and 9 defining the dimension of the matrix. What if you need the change in the dimensions? what, going to change code in 5+ places? At the same time, there is nothing difficult in writing the function where the dimensions is the parameter, no problem at all. This existing code would work with parameter values in exact same way OP works with immediate constants. At the very list, OP should have declared constants explicitly, but only two of them; others should be calculated on the base of the two. This is a biggest problem in this solution, I consider it as a design bug.

Secondly, the fact that there is no a function which abstracts out ins, outs. Worse thing here is that there is no return value. In a good implementation, this should be System.Collections.Generic.ICollection<Integer>. Instead, there is no output of the algorithm, only Console.Write. This is also a design bug. The algorithms should work with data as much as possible. What if the application of data should be different? What, to review the algorithm? -- would be not fair.

Finally, using Array.Sort and an intermediate newArray of rank 1 is a simple solution, but for some application it could be too slow. For better speed, one solution is to create just separate sorted collection for N (3 in this case) elements, and scan all elements in a loop, adding a new one if it is bigger then available elements. For big input arrays and small target N, it can be a big performance gain. But of course, this is somewhat advanced stuff.

Sorry, my vote is only 3, but I'm not even 100% sure it deserves it. For such a simple problem, the solution should be much better.
--SA
FoxRoot 13-Nov-12 15:43pm
Thanks Sergey. This will be a homework for me. I will develop my solution and post it again :) Have a nice day. I really like CodeProject members.
Sergey Alexandrovich Kryukov 13-Nov-12 15:54pm
Great. It's a pleasure to see your nice words and, even more, your productive approach to criticism, which is one of important keys to success.
Good luck,
--SA

## Solution 2

By asking such questions and getting answers, you can seriously damage your "problem solving" skills. Which skills do you want to train: your own, or the experts'?!

Do it by yourself; this is the only way. If it's not apparent to you, your prospects are very poor.

—SA

FoxRoot 12-Nov-12 18:28pm
Thanks for your consideration Sergey. But I am trying too much. I can't resolve . So I am asking. You said do it by yourself. However, I can't do it by myself.
Sergey Alexandrovich Kryukov 12-Nov-12 19:02pm
That happens. In this case, you could show your code and explain what specifically is a problem.
--SA
FoxRoot 13-Nov-12 12:12pm
Sergey can you check my solution and comment on it?
Sergey Alexandrovich Kryukov 13-Nov-12 14:19pm
OK, I did it, but... you asked for it!
--SA