Declaring an array

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Killer42
    Recognized Expert Expert
    • Oct 2006
    • 8429

    #16
    Originally posted by dejavu33
    I still have no idea what im doing wrong. I cant seem to make it work.
    Can anyone point me to the right direction please.
    I would suggest the problem is most likely in your DisplayArray() routine. But I don't know specifically what is wrong with it. I have never dealt much with objects. Could you try playing with this...
    Code:
    Public Sub DisplayArray(ByVal theArray() As Variant)
        Dim I As Long
        For I = Lcount(theArray) To Ucount(theArray)
            Console.WriteLine("{0}", theArray(I))
        Next
        Console.WriteLine(ControlChars.Lf)
    End Sub
    My basic idea was to use something other than Objects. But keep in mind that this is written by someone who has never used your version of VB, so is probably all wrong. Plus, I can't test it.

    Hm...

    Actually, from your earlier post, it looks as though my Lcount and Ucount functions are probably obsolete, and most likely replaced by something like theArray.GetLow erBound and theArray.GetUpp erBound.

    Comment

    • SammyB
      Recognized Expert Contributor
      • Mar 2007
      • 807

      #17
      Originally posted by dejavu33
      I still have no idea what im doing wrong. I cant seem to make it work.
      Can anyone point me to the right direction please.
      OK, I put lots of comments into your code. See if they help:
      Code:
      	Sub Main()
      		Dim numbers(0 To 99) As Integer	'The array.
      		Dim num As Integer		'One of the numbers input by the user.
      		Dim i As Integer
      		i = 0
      		num = 0
      		'This reads all of the numbers, but doesn't do anything with them
      		'Each number that the user enters is placed into num, thus destroying the previous entry
      		Console.Write("Please enter the value (-1 to stop): " & vbTab)
      		num = Console.ReadLine()
      		While num <> -1
      			Console.Write("Please enter the value (-1 to stop):" & vbTab)
      			num = Console.ReadLine()
      		End While
      		'For this to work, the user would have to reenter all of the numbers
      		'He's not going to like that ;o)
      		Do
      			num = Console.ReadLine()
      			If (num <> -1) Then	 ' Just put this if into the previous loop
      				numbers(i) = num
      				i = i + 1
      			End If
      		Loop Until (num = -1 Or i = 100)
      		'display values in array
      		Console.WriteLine("x1" & vbTab & "x2" & vbTab & "x3")
      		DisplayArray(numbers)
       
      		For i As Integer = 0 To numbers.GetUpperBound(0)
      			Console.Write(i & vbTab & numbers(i))
      		Next
      		'creating two copies of the array
      		Dim numbers1 As Integer()
      		'why not just say "numbers1 = numbers"
      		For j As Integer = 0 To numbers1.GetUpperBound(0)
      			Console.Write(" " & numbers1(i))
      		Next
      		ModifyArray(numbers)
      		Console.Write(" " & numbers1(i))
      		Dim numbers2 As Integer()
      		numbers2 = numbers1
      		Console.WriteLine("x3")
      		Array.Sort(numbers)
      		DisplayArray(numbers)
      	End Sub
      	'Why is theArray declared as Object? My compiler won't allow this.
      	'Why pass by value? Now all of the data has to be copied?
      	Public Sub DisplayArray(ByVal theArray() As Object)
      		Dim obj As Object
      		For Each obj In theArray
      			Console.WriteLine("{0}", obj)
      		Next obj
      		Console.WriteLine(ControlChars.Lf)
      		'using selection sort algorithm to sort first copy of array
      		'Put the above comment where it belongs, before the Selectionsort routine
      	End Sub
      	Sub Selectionsort(ByVal List() As Long, ByVal min As Integer, _
      	ByVal max As Integer)
      		Dim i As Integer
      		Dim j As Integer
      		Dim bestValue As Long
      		Dim bestJ As Integer
      		For i = min To max - 1
      			bestValue = List(i)
      			bestJ = i
      			For j = i + 1 To max
      				If List(j) < bestValue Then
      					bestValue = List(j)
      					bestJ = j
      				End If
      			Next j
      			List(bestJ) = List(i)
      			List(i) = bestValue
      		Next i
      	End Sub
      	'sorting the second array using the array.sort algorithm
      	'this doesn't sort, it doubles each value of the array
      	'it also doesn't do anything to the "second" array, because you called it with the first array
      	Sub ModifyArray(ByVal arrayParameter As Integer())
      		For j As Integer = 0 To arrayParameter.GetUpperBound(0)
      			arrayParameter(j) *= 2
      		Next
      	End Sub

      Comment

      Working...