Click here to Skip to main content
15,885,537 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have been given an algorithm which I then have to make a program for which allows the user to enter in 8 Numbers into an array. Each time a number is entered, the index is incremented and a new maximum is found based on the numbers entered.

The algorithm goes as is:
VB
BEGIN
	Set Index to 1
	Set ‘Maximum’ to 0
		WHILE Index < 8
		IF Number(Index) > Maximum THEN
			Maximum = Number(Index)
			MaxIndex = Index 
		ENDIF
		Increment Index
	ENDWHILE
	Display Maximum
END

I have got an idea about the code so far but I am unable to complete the task as this code does not function properly. So far I have this:

VB
Public Class Form1
    Dim Maximum As Integer
    Dim Num As Integer
    Dim Index As Integer
 
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Maximum = 0
        txtNumber.Text = Num
    End Sub

    Private Sub txtShoeSize_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtShoeSize.KeyPress
        If Not (Char.IsNumber(e.KeyChar) Or Char.IsControl(e.KeyChar)) Then e.Handled = True
    End Sub
    Private Sub btnEnter_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEnter.Click
        If Index = 7 Then
            MsgBox("Maximum amount of data entered, reset to add more.")
        ElseIf txtShoeSize.Text = "" Then
            MsgBox("Enter a Number.")
        ElseIf Num >= Maximum Then
            Maximum = Num
            NumArray.Items.Add(txtNum.Text)
            Index = Index + 1
        Else
            Index = Index + 1
            NumArray.Items.Add(txtNum.Text)
        End If
        txtNum.Clear()
    End Sub

    Private Sub btnDisplayMax_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDisplayMax.Click

        MsgBox("Maximum Value: " & Maximum)
    End Sub
End Class

I really urgently need help with this code. Thanks
Posted
Updated 24-Oct-12 5:44am
v2

 
Share this answer
 
I would make a small change in the way you write your conditions so as to remove some redundant duplication of code.
VB
If Index = 7 Then
   MsgBox("Maximum amount of data entered, reset to add more.")
ElseIf txtShoeSize.Text = "" Then
   MsgBox("Enter a Number.")
Else
   If Num >= Maximum Then
      Maximum = Num
   End If
   NumArray.Items.Add(txtNum.Text)
   Index = Index + 1
End If   


If you provide a little more information as to the "does not function properly", that would help us help you more. Describe to us what actually happens versus what should happen.
 
Share this answer
 
i cant find where you are read the variable num, are you missing this

VB
If (Integer.TryParse(txtnum) = True) Then
 num = Integer.Parse(txtnum) 
Else 
MessageBox.show("Not A number")
return
end if
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900