12,501,304 members (40,306 online)
Rate this:
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 12-Nov-12 4:26am
FoxRoot1.1K

Rate this:

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
TuranEmre 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[^]
TuranEmre 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
Rate this:

Solution 3

```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
TuranEmre 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
Nelek 13-Nov-12 19:08pm

If you follow Sergey's instructions and you change your code, please don't add a new solution. Just use the "improve solution" widget (green, right bottom of your text) to edit the message, so you can improve it without adding not relevant posts. You can afterwards tell us you changed it and then we will check it again
TuranEmre 14-Nov-12 4:28am

Got it. Thank you Nelek. Have a nice day
Nelek 14-Nov-12 4:45am

You are welcome :)
TuranEmre 14-Nov-12 4:56am

I have one 1 question but I am confused. Because it will come to easy for you but I am researching more than 1 montyh :(
Could you have a check
http://www.codeproject.com/Questions/480960/HowpluscanplusIplusopenplusaplushtmlplusfilepluswi
I am using netbeans
Nelek 14-Nov-12 4:59am

TuranEmre 14-Nov-12 5:12am

I solved :) I am very happy :)
Nelek 14-Nov-12 8:29am

Nice... congratulations
Rate this:

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
TuranEmre 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
TuranEmre 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

Top Experts
Last 24hrsThis month
 David_Wimbley 230 OriginalGriff 213 Karthik Bangalore 115 Richard MacCutchan 85 Afzaal Ahmad Zeeshan 70
 OriginalGriff 6,558 ppolymorphe 3,669 Maciej Los 3,283 David_Wimbley 2,652 Karthik Bangalore 2,619