Welcome to Dream.In.Code
Getting VB Help is Easy!

Join 136,569 VB Programmers for FREE! Get instant access to thousands of VB experts, tutorials, code snippets, and more! There are 1,912 people online right now. Registration is fast and FREE... Join Now!




Find largest to smallest number

 
Reply to this topicStart new topic

Find largest to smallest number, Print largest to smallest of five numbers

penguindesktop
10 May, 2008 - 04:10 PM
Post #1

New D.I.C Head
*

Joined: 10 May, 2008
Posts: 13


My Contributions
OK, I did search for help on this, but I probably did not use the correct verbage. Here's what I have so far: I have to write an application that reads five integers and determints and prints the largest and the smallest integers in a group. I cannot use array's etc.. I must use what we have learned up to this point, which is If..Else statements. I have a conceptual idea of what to do, and have it working with only two numbers (no brainer right) If I make five variables and then check to see if each subsequent number is greater than or equal to the previous number, then I should come up the the largest to smallest?? I have commented out the number4 - 5 to keep the debugger complaing, I figured I can just add them later. Here is the code that I have come up with to date.

CODE

Module SortNumbers

    Sub Main()

        Dim number1 As Integer
        Dim number2 As Integer
        Dim number3 As Integer
        'Dim number4 As Integer
        'Dim number5 As Integer

      'read in the first number
        Console.Write("Enter the first Number")
        number1 = Console.ReadLine()

        'read in the second number
        Console.WriteLine("Enter the second Number")
        number2 = Console.ReadLine()
    
        'read in the third number
         Console.WriteLine("Enter the third Number")
         number3 = Console.ReadLine()

      If number2 >= number1 Then 'check to see if number2 is larger than or equal to number1
         Console.WriteLine(number2 & " " & number1) ' if it is than write that to the console
      Else : Console.WriteLine(number1 & " " & number2) ' or if number1 is bigger than print this message
      End If

      If number3 >= number2 Then 'If the third number is larger than the 2nd number
         Console.WriteLine(number3 & " " & number2 & " " & number1) ' this is where I am stuck.
      ElseIf number Then
      End If

   End Sub

End Module



I considered making more variables like:

CODE

dim first
dim second
dim third
dim fourth
dim fifth



and then assigning the "number*" variable to the other first - fifth variable, but I thought I was getting a little more involved with it. I wish I had picked this up a 22 instead of 43 years old. I was hoping my old vb6 (from 1998) credits would still be good, but... Thanks for any small contribution. I know I'ts just one little thing, and I do remember doing something like this with array's in Java back in the day. But I don't program on a daily basis.

Richard
User is offlineProfile CardPM
+Quote Post

Martyr2
RE: Find Largest To Smallest Number
10 May, 2008 - 06:03 PM
Post #2

Programming Theoretician
Group Icon

Joined: 18 Apr, 2007
Posts: 5,213



Thanked: 217 times
Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions
Well if you stop and think for a second, how would you know if a number is the highest number? You would have to compare it to the rest and if one number was higher, then you would use that number right?

So collect your five numbers in their variables like you are, but setup two more, one for largest and one for smallest. Start with setting them both to the first number. So now highest and smallest equal number1. Now test highest and smallest against each of the other four numbers using an if. If number2 is higher than "highest" set "highest" to number2. If it is smaller than "lowest" set "lowest" to number 2. Then continue on to number 3, 4 and 5.

At the end of your five tests you will have "highest" containing the highest number and "lowest" containing the lowest number. The idea is to record only when a number is higher or lower than the currently highest or lowest value.

vb

highest = number1
lowest = number1

if number2 > highest then
highest = number2
else if number2 < lowest then
lowest = number2
end if

' Continue to number3


Hope this makes sense. As you move through the numbers only the highest will be left in highest and the lowest in lowest variables.

Enjoy!

"At DIC we be high and low code ninjas... some of us take the high road, but Capty will always take the low. Get use to it." decap.gif

User is offlineProfile CardPM
+Quote Post

penguindesktop
RE: Find Largest To Smallest Number
10 May, 2008 - 06:45 PM
Post #3

New D.I.C Head
*

Joined: 10 May, 2008
Posts: 13


My Contributions
QUOTE(Martyr2 @ 10 May, 2008 - 07:03 PM) *

Well if you stop and think for a second, how would you know if a number is the highest number? You would have to compare it to the rest and if one number was higher, then you would use that number right?

So collect your five numbers in their variables like you are, but setup two more, one for largest and one for smallest. Start with setting them both to the first number. So now highest and smallest equal number1. Now test highest and smallest against each of the other four numbers using an if. If number2 is higher than "highest" set "highest" to number2. If it is smaller than "lowest" set "lowest" to number 2. Then continue on to number 3, 4 and 5.

At the end of your five tests you will have "highest" containing the highest number and "lowest" containing the lowest number. The idea is to record only when a number is higher or lower than the currently highest or lowest value.

vb

highest = number1
lowest = number1

if number2 > highest then
highest = number2
else if number2 < lowest then
lowest = number2
end if

' Continue to number3


Hope this makes sense. As you move through the numbers only the highest will be left in highest and the lowest in lowest variables.

Enjoy!

"At DIC we be high and low code ninjas... some of us take the high road, but Capty will always take the low. Get use to it." decap.gif



Thanks, that was fast. I'm on my way to try it out.
User is offlineProfile CardPM
+Quote Post

penguindesktop
RE: Find Largest To Smallest Number
10 May, 2008 - 07:44 PM
Post #4

New D.I.C Head
*

Joined: 10 May, 2008
Posts: 13


My Contributions
tongue.gif Your advice worked great, I got the "highest" variable to work out, but the "lowest" variable is always 0. I've tinkered with it a bit and still come up with "0" for the lowest.

CODE

Module SortNumbers

    Sub Main()

      'Make variable declarations for inputs from user

      Dim number1 As Integer
      Dim number2 As Integer
      Dim number3 As Integer
      Dim number4 As Integer
      Dim number5 As Integer

      'Define highest and lowest number
      Dim highest As Integer
      Dim lowest As Integer

      'Assign first number as the highest and lowest
      'since it's the only number initially
      highest = number1
      lowest = number1


      'read in the first number
      Console.Write("Enter the first Number ")
      number1 = Console.ReadLine()

      'read in the second number
      Console.Write("Enter the second Number ")
      number2 = Console.ReadLine()

      'read in the third number
      Console.Write("Enter the third Number ")
      number3 = Console.ReadLine()

      'read in the fourth number
      Console.Write("Enter the fourth Number ")
      number4 = Console.ReadLine()

      'read in the fifth number
      Console.Write("Enter the fifth Number ")
      number5 = Console.ReadLine()

      Console.WriteLine("Number1 is " & number1) 'put here to debug, ensure that the variables are
                                                                        'getting assigned correctly


      If number2 > highest Then
         highest = number2
      ElseIf number2 < lowest Then
         lowest = number2
      End If

      If number3 > highest Then
         highest = number3
      ElseIf number3 < lowest Then
         lowest = number3
      End If

      If number4 > highest Then
         highest = number4
      ElseIf number4 < lowest Then
         lowest = number4
      End If

      If number5 > highest Then
         highest = number5
      ElseIf number5 < lowest Then
         lowest = number5
      End If

      Console.WriteLine("The Hightest number is " & highest & ". The Lowest number is " & lowest)

   End Sub

End Module

User is offlineProfile CardPM
+Quote Post

no2pencil
RE: Find Largest To Smallest Number
10 May, 2008 - 07:50 PM
Post #5

My fridge be runnin OH NOEZ!
Group Icon

Joined: 10 May, 2007
Posts: 6,465



Thanked: 66 times
Dream Kudos: 2425
Expert In: Goofing Off

My Contributions
QUOTE(penguindesktop @ 10 May, 2008 - 11:44 PM) *

tongue.gif Your advice worked great, I got the "highest" variable to work out, but the "lowest" variable is always 0. I've tinkered with it a bit and still come up with "0" for the lowest.

CODE

      'Assign first number as the highest and lowest
      'since it's the only number initially
      highest = number1
      lowest = number1


      'read in the first number
      Console.Write("Enter the first Number ")
      number1 = Console.ReadLine()

...

      If number2 > highest Then
         highest = number2
      ElseIf number2 < lowest Then
         lowest = number2
      End If
...

      If number5 > highest Then
         highest = number5
      ElseIf number5 < lowest Then
         lowest = number5
      End If

      Console.WriteLine("The Hightest number is " & highest & ". The Lowest number is " & lowest)




I see where you are setting lowest to number1 before you input it. Then you never recheck number1 to see if it is lower than the others. So when you are prompted to put in numbers, if number1 is in fact the lowest, after going through the logic the value lowest will still contain the value of number1 at the start of the program... which was zero.

Also, it looks like have a slight type-o in your assignment for number1. It looks like you've left the e out of the name of the variable.
User is offlineProfile CardPM
+Quote Post

penguindesktop
RE: Find Largest To Smallest Number
12 May, 2008 - 05:06 AM
Post #6

New D.I.C Head
*

Joined: 10 May, 2008
Posts: 13


My Contributions
Got it, thanks for the help. I guess if I wanted to take all five numbers and print out all five in H to L order an array would be needed, and some "sort" function? We have not gotten to that point yet. Here is the code the worked. You are correct no2pencil, after I moved my assignments to a point AFTER they got a value the whole thing started working.

CODE

Module SortNumbers

    Sub Main()

      'Make variable declarations for inputs from user

      Dim number1 As Integer
      Dim number2 As Integer
      Dim number3 As Integer
      Dim number4 As Integer
      Dim number5 As Integer

      'Define highest and lowest number
      Dim highest As Integer
      Dim lowest As Integer

      'read in the first number
      Console.Write("Enter the first Number ")
      number1 = Console.ReadLine()

      'read in the second number
      Console.Write("Enter the second Number ")
      number2 = Console.ReadLine()

      'read in the third number
      Console.Write("Enter the third Number ")
      number3 = Console.ReadLine()

      'read in the fourth number
      Console.Write("Enter the fourth Number ")
      number4 = Console.ReadLine()

      'read in the fifth number
      Console.Write("Enter the fifth Number ")
      number5 = Console.ReadLine()

      'Assign first number as the highest and lowest
      'since it's the only number initially
      highest = number1
      lowest = number1

      If number2 > highest Then
         highest = number2
      ElseIf number2 < lowest Then
         lowest = number2
      End If

      If number3 > highest Then
         highest = number3
      ElseIf number3 < lowest Then
         lowest = number3
      End If

      If number4 > highest Then
         highest = number4
      ElseIf number4 < lowest Then
         lowest = number4
      End If

      If number5 > highest Then
         highest = number5
      ElseIf number5 < lowest Then
         lowest = number5
      End If

      Console.WriteLine("The Hightest number is " & highest & ". The Lowest number is " & lowest)

   End Sub

End Module

User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 12/2/08 11:55PM

Live VB Help!

VB Tutorials

Reference Sheets

VB Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month