Find largest to smallest number

Print largest to smallest of five numbers

Page 1 of 1

5 Replies - 26229 Views - Last Post: 12 May 2008 - 06:06 AM Rate Topic: -----

#1 penguindesktop  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 13
  • Joined: 10-May 08

Find largest to smallest number

Post icon  Posted 10 May 2008 - 05:10 PM

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.

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:

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

Is This A Good Question/Topic? 0
  • +

Replies To: Find largest to smallest number

#2 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4405
  • View blog
  • Posts: 12,262
  • Joined: 18-April 07

Re: Find largest to smallest number

Posted 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.

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." :snap:
Was This Post Helpful? 0
  • +
  • -

#3 penguindesktop  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 13
  • Joined: 10-May 08

Re: Find largest to smallest number

Posted 10 May 2008 - 07:45 PM

View PostMartyr2, on 10 May, 2008 - 07:03 PM, said:

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.

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." :snap:



Thanks, that was fast. I'm on my way to try it out.
Was This Post Helpful? 0
  • +
  • -

#4 penguindesktop  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 13
  • Joined: 10-May 08

Re: Find largest to smallest number

Posted 10 May 2008 - 08:44 PM

:P 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.

 
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


Was This Post Helpful? 0
  • +
  • -

#5 no2pencil  Icon User is offline

  • Admiral Fancy Pants
  • member icon

Reputation: 5382
  • View blog
  • Posts: 27,350
  • Joined: 10-May 07

Re: Find largest to smallest number

Posted 10 May 2008 - 08:50 PM

View Postpenguindesktop, on 10 May, 2008 - 11:44 PM, said:

:P 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.

 
	  '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.
Was This Post Helpful? 0
  • +
  • -

#6 penguindesktop  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 13
  • Joined: 10-May 08

Re: Find largest to smallest number

Posted 12 May 2008 - 06:06 AM

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.

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


Was This Post Helpful? 1
  • +
  • -

Page 1 of 1