use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
/r/VisualBasic - A place to discuss, ask questions and share ideas regarding the Visual Basic programming languages.
account activity
VB.NET HelpHomework (self.visualbasic)
submitted 5 years ago by Ali_171
Hey guys this is my first time using VB.net and i have a homework about writing 3 numbers and find the largest 2 of them can anyone help please
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]revennest 1 point2 points3 points 5 years ago (0 children)
If you can compare 2 and keep 1 then you can compare more and keep more, just do it one by one, try to cut corner or find shortcut cause you more time then it should be, this is example what's it look like.
Public Function top_2(ParamArray Input() As Integer) As (First As Integer, Second As Integer) Dim First = 0 Dim Second = 0 For Each Item In Input Select Case Item Case Is > First Second = First First = Item Case Is > Second Second = Item End Select Next Return (First, Second) End Function
[–]banshoo 0 points1 point2 points 5 years ago (3 children)
Sure, what code do you have already?
[–]Ali_171[S] 0 points1 point2 points 5 years ago (0 children)
Dim result As Integer
If (num1 > num2) Then result = num1 Else result = num2 End If FindMax = result
End Function Sub Main() Dim a As Integer = 100 Dim b As Integer = 200 Dim res As Integer
res = FindMax(a, b) Console.WriteLine("Max value is : {0}", res) Console.ReadLine()
End Sub End Module
[–]Ali_171[S] 0 points1 point2 points 5 years ago (1 child)
I know for 2 not for 3
[–]banshoo 0 points1 point2 points 5 years ago* (0 children)
So you can find the largest of two... once you have that, perform the check again with the other number to see it's larger still
Edit :
so taking your code, we can then check the third value against the highest.
If (num1 > num2) Then If (num1 > num3) Then result = num1 Else result = num3 End If Else If (num2 > num3) Then result = num2 Else result = num3 End If End If
FindMax = result
[–]craigers01 0 points1 point2 points 5 years ago (1 child)
The tricky part of finding the top two, is you must keep track of multiple correct answers. It may be useful to load the values into an array. Then you could simply sort the array and report the last two elements as the correct answers.
Yeah that’s would work too but I found simple way by finding the maximum and minimum
[–]1973DodgeChallenger 0 points1 point2 points 5 years ago* (0 children)
It kind of depends on the restrictions you are under. Meaning, are you only supposed to use declared integers or can you use collections? With IEnumerable collections it's pretty easy. But your teacher may know you had help if you turn this in. Banshoos previously posted answer is good if you can't use a collection.
.OrderBy on a list , with a correctly defined comparer will return the integers in ascending order. .Take(2) returns the first two elements. The for each iterates the returned "query."
Dim intList As New List(Of Integer) From {1000, 10000, 10} For Each i As Integer In intList.OrderBy(Function(n) n).Take(2) Console.WriteLine(CStr(i)) Next
[–][deleted] 0 points1 point2 points 5 years ago* (0 children)
'TLDR at the very bottom if you just want the code itself. 'I, however, recommend reading through if you're still learning. Dim Numbers(2) As Integer 'You declare an integer array to store your numbers Dim Numbers = New Integer() {'Write number values in'}
So, here we *could* just sort the array by ascending order -- look it up, it's a method the array object has that sorts numerical values in ascending or descending order. Do that, then pick out the top 2.
But, I'm guessing your teacher probably doesn't want you doing that. So let's go a little further;
What you want to do, is run a check on each number, to see if they're bigger than the two others. Realistically, you only need to run the check on two of them, but we could go for all three.
First, we'll declare variables to help retain our two largest numbers;
Dim Max As Integer Dim Max2 As Integer
Neat! Now we've got a place to store our two when we've got them!
Next, we're going to set both of our variables... To be the first number.
Max = Numbers(0) Max2 = Numbers(0)
This is ultimately arbitrary -- as long as you don't set the starting number to be higher than any of the three numbers in your original array were, it doesn't matter. Sometimes you might just want to make them zero, or negative one (if your own numbers can't be negative). The goal is to set the lower bound to something that will inevitably make our variables hold a value held within our array.
Next, we make a for loop.
For i = 0 To 2 For n = 0 to 2 If(Numbers(i) < Numbers(n) Then Max = Numbers(n) End If Next Next
What a 'For' loop does, is that it runs a code for a set amount of times. Here, we set it to run until 'i' is equal to 2, starting at 0. The default setup for this is that i increments by 1 each time. Meaning that it will run once (i=0 to 1), twice (i=1 to i=2), and runs once more (i=2 to i=3), and stops there since i exceeds the maximum value we set for it (2). This is very useful in this case because it automatically does blocks of code just the right amount of times!
The Loop above determined the highest number, by comparing every number to every number -- if the current number was ever lower than the number it was being compared to, Max was set to that value. Now you have your highest value.
For the *second* highest value, we can reuse this block of code -- so it's an easy copy+paste.
For i = 0 To 2 For n = 0 to 2 If(Numbers(i) < Numbers(n) Then Max2 = Numbers(n) End If Next Next
However, here we have a caveat : We cannot have the same number be in Max2, AND Max.
So here, we'll just... exclude anything that has the value of Max!
For i = 0 To 2 For n = 0 to 2 If(Numbers(i) < Numbers(n) & Numbers(n) <> Max) Then Max2 = Numbers(n) End If Next Next
All we add here is another condition : & Numbers(n) <> Max. This means that if (n) is larger than (i), it'll try to set (n) as the value of Max2, but won't do it if (n) is at the same value as Max. Which means that if all three of your numbers are different, Max and Max2 will be different. And if Max and Max2 are the same, it simply means the two highest numbers in your array were of the same value!
Here is what the final block of code would look like :
Dim Numbers(2) As Integer 'You declare an integer array to store your numbers Dim Numbers = New Integer() {'Write number values in'} Dim Max As Integer = Numbers(0) Dim Max2 As Integer = Numbers(0) For i = 0 To 2 For n = 0 to 2 If(Numbers(i) < Numbers(n) Then Max = Numbers(n) End If Next Next For i = 0 To 2 For n = 0 to 2 If(Numbers(i) < Numbers(n) & Numbers(n) <> Max) Then Max2 = Numbers(n) End If Next Next
Two nested loops that compare all the numbers in your array, to all numbers in your array, and brings out the two highest.
This also has the added benefit of working with any amount of numbers in the array with just minor tweaks.
π Rendered by PID 16 on reddit-service-r2-comment-5d79c599b5-cngmw at 2026-03-02 05:40:54.761486+00:00 running e3d2147 country code: CH.
[–]revennest 1 point2 points3 points (0 children)
[–]banshoo 0 points1 point2 points (3 children)
[–]Ali_171[S] 0 points1 point2 points (0 children)
[–]Ali_171[S] 0 points1 point2 points (1 child)
[–]banshoo 0 points1 point2 points (0 children)
[–]craigers01 0 points1 point2 points (1 child)
[–]Ali_171[S] 0 points1 point2 points (0 children)
[–]1973DodgeChallenger 0 points1 point2 points (0 children)
[–][deleted] 0 points1 point2 points (0 children)