Thursday, October 7, 2010

Thursday October 7th

I've turned some of the psuedo code I have been writing in class to real code. Here is the first part


Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim inputNumber As Integer = 0
Dim i As Integer = 1
Dim numberArray(100) As Integer
Dim arrayCount As Integer = 0

Dim maxNumber As Integer = 0



While inputNumber <> -99
inputNumber = InputBox("Input the number, enter -99 when done.")
numberArray(arrayCount) = inputNumber
arrayCount = arrayCount + 1
i = i + 1
End While


TextBox1.Text = arrayCount - 1


End Sub
End Class



I finally got this to work. I have to convert it into pseudocode, which I will post here as well if I get the chance.





Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim inputNumber As Integer = 0
Dim i As Integer = 0
Dim numberArray(100) As Integer
Dim arrayCount As Integer = 0

Dim maxNumber As Integer = 0



While inputNumber <> -99
inputNumber = InputBox("Input the number, enter -99 when done.")
numberArray(arrayCount) = inputNumber
i = i + 1
arrayCount = arrayCount + 1

End While


'' The last number entered is -99, and we don't want that to count as part of the array, right?
TextBox1.Text = "The last number you entered (excluding -99) was " & numberArray(i - 2).ToString & vbCrLf
TextBox1.Text = TextBox1.Text & "The array count is " & arrayCount & vbCrLf

TextBox1.Text = TextBox1.Text & "The numberArray(arrayCount - 2) is " & numberArray(arrayCount - 2) & vbCrLf


Call compare(i, numberArray, arrayCount, maxNumber)

TextBox1.Text = TextBox1.Text & "The maximum number is " & maxNumber & vbCrLf

End Sub


Sub compare(ByVal i As Integer, ByVal numberArray As Array, ByVal arrayCount As Integer, ByRef maxNumber As Integer)
i = i - 3
arrayCount = arrayCount - 2
While i >= 1
If numberArray(arrayCount) > numberArray(i) Then
maxNumber = numberArray(arrayCount)
TextBox1.Text = TextBox1.Text & numberArray(arrayCount) & " > " & numberArray(i) & vbCrLf
i = i - 1
Else
TextBox1.Text = TextBox1.Text & numberArray(arrayCount) & " < " & numberArray(i) & vbCrLf
maxNumber = numberArray(i)
arrayCount = i
i = i - 1

End If
End While

End Sub

1 comment:

Brad Jensen said...

based on our discussions, one way you could accept input from a user and keep the highest and lowest values would be like this

dim highestx as long
dim lowestx as long
dim uinputx as long
' set highest and lowest to values outside the range of input

highestx = -9999999999
lowestx = 9999999999

do
input inputx
if inputx = -99 then exit do ' sentinal value
if inputx < lowestx then lowestx = inputx
if inputx > highestx then highestx = inputx
loop

print "Highest value entered " & format(highestx)
print "Lowest value entered " & format(lowestx)