Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Can anyone solve this problem, its killin me

Status
Not open for further replies.

Zoomy

Programmer
Aug 28, 2002
6
US
Add the missing lines of code so that the output of the following code snippet is:
“Final array values: -978 –10 –9 18 49 99 287 463 5623 100123?”


Dim NumbersList() as Integer = {99, -10, 100123, 18, -978, 5623, 463, -9, 287, 49}
Dim a as Integer
Dim b as Integer
Dim i as Integer

For a = 1 to NumbersList.Length
For b = NumbersList.Length -1 to a Step -1
‘ADD CODE HERE
Next
Next

Debug.WriteLine(“Final array is:”)
For i = 0 to NumbersList.Length
Debug.WriteLine(“ “ + NumbersList(i))
Next
 
I assume you'd like to sort the array in ascending order. The for loops seem to indicate to me that you're intending to do it with Bubble sort, which is perfectly OK for small arrays (a few thousand members).
One more thing I'd like to add, it's that in vb.net, ALL arrays start from 0 and up (zero based), so the subscripts of the arrays run from 0 to (NumbersList.length -1).
I have modified a few lines of the code to suit, as follows:

Dim NumbersList() As Integer = {99, -10, 100123, 18, -978, 5623, 463, -9, 287, 49}
Dim a As Integer
Dim b As Integer
Dim i As Integer
' Note that the for parameters have been modified
For a = 0 To NumbersList.Length - 2
For b = a + 1 To NumbersList.Length - 1
'ADD CODE HERE (Bubble sort)
If NumbersList(a) > NumbersList(b) Then 'exchange
i = NumbersList(a)
NumbersList(a) = NumbersList(b)
NumbersList(b) = i
End If
Next
Next

Debug.WriteLine("Final array is:")
For i = 0 To NumbersList.Length - 1
'I have changed the + to & for concatenation of strings
Debug.WriteLine(" " & NumbersList(i))
Next

'Best regards
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top