Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: VB.NET arrays
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 5:26am
FoxRoot897
Rate this: bad
good
Please Sign up or sign in to vote.

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?"
  Permalink  
v2
Comments
TuranEmre at 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 at 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 at 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 at 12-Nov-12 11:00am
   
You are welcome. If you come back with problems in your code, then post a comment, so I will receive a notice
Sergey Alexandrovich Kryukov at 12-Nov-12 14:47pm
   
(Sigh...) Please look at my answer to see what I think...
--SA
Rate this: bad
good
Please Sign up or sign in to vote.

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
  Permalink  
Comments
lewax00 at 13-Nov-12 13:12pm
   
That looks like it should work. See, you can figure it out! :)
Sergey Alexandrovich Kryukov at 13-Nov-12 14:19pm
   
Yes, it would work, still the solution is unsatisfactory -- please see my comment.
--SA
Sergey Alexandrovich Kryukov at 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 at 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 at 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 at 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 at 14-Nov-12 4:28am
   
Got it. Thank you Nelek. Have a nice day
Nelek at 14-Nov-12 4:45am
   
You are welcome :)
TuranEmre at 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 at 14-Nov-12 4:59am
   
Sorry, I can't help you on that. I am not so good at web programming. Let see if Sergey can help you. He will receive notification of these comments as well.
TuranEmre at 14-Nov-12 5:12am
   
I solved :) I am very happy :)
Nelek at 14-Nov-12 8:29am
   
Nice... congratulations
Rate this: bad
good
Please Sign up or sign in to vote.

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
  Permalink  
Comments
TuranEmre at 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 at 12-Nov-12 19:02pm
   
That happens. In this case, you could show your code and explain what specifically is a problem.
--SA
TuranEmre at 13-Nov-12 12:12pm
   
Sergey can you check my solution and comment on it?
Sergey Alexandrovich Kryukov at 13-Nov-12 14:19pm
   
OK, I did it, but... you asked for it!
--SA

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

  Print Answers RSS
0 OriginalGriff 359
1 Maciej Los 290
2 Sergey Alexandrovich Kryukov 230
3 Shweta N Mishra 185
4 PIEBALDconsult 149
0 OriginalGriff 7,660
1 Sergey Alexandrovich Kryukov 7,072
2 DamithSL 5,604
3 Manas Bhardwaj 4,986
4 Maciej Los 4,760


Advertise | Privacy | Mobile
Web04 | 2.8.1411023.1 | Last Updated 13 Nov 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100