VB.Net sorting an array

Sorting an array and returning a string

Page 1 of 1

4 Replies - 7188 Views - Last Post: 18 July 2009 - 08:00 AM Rate Topic: -----

#1 cpgdallas  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 15-July 09

VB.Net sorting an array

Post icon  Posted 16 July 2009 - 03:44 PM

Hello,

Here is my situtation.

I have an app. that receives input (numeric) from the user.
That number is associated with a string (month of the year)

I must be able to total, average, find min., find max. of the input numbers.

I have been able to do all of these but I must also return a string with both the min., and max..

I also have a listbox that displays the input numbers with the associated month.
" 3 for the month of January".

I am trying to sort the array and return the string from the associated lowest.

I have this array call RainArray:
decRainFall(0) = CDec(strMonths(0))


As well as this array called MonArray.
strMonths(0) = "January"


I am trying to sort the RainArray
Dim RainArray(11) As Decimal
		Array.Sort(decRainFall)

		Dim decLowestMonth(0) As Decimal
		Dim strLowestMonth(0) As String
		Dim decHighestMonth(11) As Decimal
		Dim strHighestMonth(11) As String

		lblMaximum.Text = strLowestMonth.ToString


Nothing is returning. Be gentle.

Is This A Good Question/Topic? 0
  • +

Replies To: VB.Net sorting an array

#2 mark.bottomley  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 176
  • View blog
  • Posts: 990
  • Joined: 22-April 09

Re: VB.Net sorting an array

Posted 16 July 2009 - 03:56 PM

this is the same post as appeared yesterday??? (So similar it is scary)

strMonths(0) = "January"

decRainFall(0) = CDec(strMonths(0))

These 2 statements don't work together - the first one assigns a string containing "January" to the array and the second tries to convert the string "January" to a decimal number - won't work, will throw an exception and do nothing.

Please post all of the class for your code as these pieces don't make a coherent picture.
Was This Post Helpful? 0
  • +
  • -

#3 T3hC13h  Icon User is offline

  • D.I.C Regular

Reputation: 65
  • View blog
  • Posts: 337
  • Joined: 05-February 08

Re: VB.Net sorting an array

Posted 16 July 2009 - 04:56 PM

Array.Sort will not work here since if you change the order of values in your array you won't know to which month they belong

Don't declare a single element array when you need a single value object.


I'm not sure whats up with this but lets see if I can sort out what your trying to do.
It looks like your trying to assign the top and bottom of the sorted array to Decimal variables then resolve the name of the month for that corresponds to those months.
	   
 Array.Sort(decRainFall)
Dim decLowestMonth(0) As Decimal
		Dim strLowestMonth(0) As String
		Dim decHighestMonth(11) As Decimal
		Dim strHighestMonth(11) As String
		lblMaximum.Text = strLowestMonth.ToString

Here is how you would do that. But since weve rearanged our orignal array we can't tell which months correspond to the highest and lowest values.
	   
 Array.Sort(decRainFall)
dim decLowerMonth as Decimal = decRainFall(0)
dim decHighestMonth as Decimal = decRainFall(11)




Heres an alternate method that doesn't involve storing the names of months or Array.Sort.
   


if
'The array you'll use to hold the rainfall values provided by the user, this could be a class level field
Dim decRainfall(11) As Decimal

'Code to populate rainfall array

		'Integer variable to hold the index of he current lowest rainfall value while we search the array
		Dim intLowestMonth As Integer = 0
		'Decimal value to hold the value of the current lowest rainfall while we search the array, since we starting value (incase all values in the array are > 0) I've given the first rainfall value
		Dim decLowestRainfall As Double = decRainfall(0)
		'Loop through each element of the array
		For i As Integer = 0 To decRainfall.Length - 1
			'Compare each value in the array to the lowest value variable
			If decRainfall(i) < decLowestRainfall Then
				'When we find a new lowest well save the value to compare later and save the index for later use.
				decLowestRainfall = decRainfall(i)
				intLowestMonth = i
			End If
		Next
'Ok now that we've found the lowest value we can get the name of the month. I'm assuming the values of the decRainfall are order according to month.
'Here we declare a string veriable and have used the DateTime class to get the name of the month. The ToString function of the DateTime class allows for a format string paramater "MMMM" will return the full name of the month.
		 Dim strLowestMonth As String = New DateTime(1, intLowestMonth, 1).ToString("MMMM")
 



Again, if you would post your code we would have a better picture of what your doing.
Was This Post Helpful? 0
  • +
  • -

#4 cpgdallas  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 15-July 09

Re: VB.Net sorting an array

Posted 16 July 2009 - 06:36 PM

View Postmark.bottomley, on 16 Jul, 2009 - 02:56 PM, said:

this is the same post as appeared yesterday??? (So similar it is scary)

strMonths(0) = "January"

decRainFall(0) = CDec(strMonths(0))

These 2 statements don't work together - the first one assigns a string containing "January" to the array and the second tries to convert the string "January" to a decimal number - won't work, will throw an exception and do nothing.

Please post all of the class for your code as these pieces don't make a coherent picture.

Same question...same person.
Was This Post Helpful? 0
  • +
  • -

#5 cpgdallas  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 15-July 09

Re: VB.Net sorting an array

Posted 18 July 2009 - 08:00 AM

View Postcpgdallas, on 16 Jul, 2009 - 05:36 PM, said:

View Postmark.bottomley, on 16 Jul, 2009 - 02:56 PM, said:

this is the same post as appeared yesterday??? (So similar it is scary)

strMonths(0) = "January"

decRainFall(0) = CDec(strMonths(0))

These 2 statements don't work together - the first one assigns a string containing "January" to the array and the second tries to convert the string "January" to a decimal number - won't work, will throw an exception and do nothing.

Please post all of the class for your code as these pieces don't make a coherent picture.

Same question...same person.

I figured it out! Well... I say 'I' but everyone's input keep pushing me thinking. I appreciate all the help that was given! Thank you.
I realize I didn't post enough code but I really didn't know what to post since I didn't know where the problem was and I was unsure about posting all of the code for the program.
Here's what I did:
I have an array with the months that is initialized when the form loads.
strMonths(0)="January"
ect for each month.

I use this method to obtain rain total input.
 Dim intCounter As Integer  ' Loop counter
		' Get rainfall totals for each month.
		For intCounter = 0 To intMAX_MONTH
			Do While Decimal.TryParse(InputBox( _
									   "Enter the rainfall total for " _
									   & strMonths(intCounter), _
									   "Enter rainfall total"), decRainFall(intCounter)) = False
				MessageBox.Show("You must enter a valid number")
			Loop
		Next intCounter


Then, to obtain my maximum rain total input by the user and return the month to the label:
' Declare variables for the display.
		Dim intCounter As Integer = 0
		' Display the maximum rainfall.
		' Calculates which month had the highest rainfall.
		Dim intMaxIndex As Integer = 0
		Dim decHighest As Decimal = decRainFall(0)
		For intCounter = 1 To (strMonths.Length - 1)
			If decRainFall(intCounter) > decHighest Then
				decHighest = decRainFall(intCounter)
				' Locates the string month index
				intMaxIndex = intCounter
			End If
		Next intCounter

		lblMaximum.Text = "The maximum rainfall for the year was " _
		& decHighest.ToString & " (" & strMonths(intMaxIndex).ToString & ")"


This was the last program for the class and the book has four chapters that we haven't been assigned but I think I'll finish the book.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1