Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

I�m trying to understand arrays and how to give values to the array with an inputbox or second form. I just don�t seem to understand how it all works so for the purpose of this question I created some code that in my mind should have assigned a value to the array but it didn�t would someone pleas tell me what I�m doing wrong?

 

For Each score In Grade
           score = Convert.ToInt32(InputBox("Score?"))
           If score <= 0 Then
               Exit For
           End If
       Next
 

Posted
Did the array already contain any items? If so then this would have replaced them. If there are no items in the array then this will do nothing.

Could you post the part of the code where you declare and initalise the array.

 

 

Thanks for the reply this is my complet code. To tell you the truth i really know nothing about arrays I have finals on the 22 of this mounth and really need to understand them. So thanks for the help :-)

 

Dim max_grade As Integer = 0
   Dim av_grade As Integer = 0
   Dim min_grade As Integer = 100
   Dim GradeTotal As Integer
   Dim Grade(100) As Integer
   Dim score As Integer


Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click


       For Each score In Grade
           score = Convert.ToInt32(InputBox("Score?"))
           If score <= 0 Then
               Exit For
           End If
       Next
       Dim I As Integer
       For i = 0 To 100
           If Grade(I) > max_grade Then max_grade = Grade(I)
           If Grade(I) < min_grade Then min_grade = Grade(I)
           av_grade = av_grade + Grade(I)
       Next I

   End Sub

Posted
Problem is that the variable score will be a copy of each item in the array' date=' changing this copy will not affect the value in the array itself.[/quote']

 

 

Ok I see what you are saying. So what would be the proper way for me to populate this array (Grade) with integer values using an InputBox?

Posted

Ok thanks. The easy things never come easy for me.

 

I have one last question. I would like to write a function that will loop thru the array checking each value. So that I can get the highest grade entered and the lowest and then and average. I will figure everything out my self but can you tell me how to check values in the array with a loop?

Posted

Something like this:

 

Dim counterArray(254) As Integer 'array with 255 slots

Dim i As Byte

Dim MaxValue As Integer

Dim MinValue As Integer

Dim indexOfMaxValue As Byte

Dim indexOfMinValue As Byte

Dim Total As integer

Dim avg As integer

 

For i = 0 To counterArray.Length - 1

 

Total = counterArray(i) + Total

 

If counterArray(i) > MaxValue Then

MaxValue = counterArray(i)

indexOfMaxValue = i

 

Elseif counterArray(i) < MinValue Then

MinValue = counterArray(i)

indexOfMinValue = i

 

End If

Next i

 

avg = Total / counterArray.Length

"Everything should be made as simple as possible, but not simpler."

"It's not that I'm so smart , it's just that I stay with problems longer ."

- Albert Einstein

Posted

What is wrong with this? I jsut cant seem to figuer it out.

 

Dim HighGrade As Integer = 0
   Dim av_grade As Integer = 0
   Dim LowGrade As Integer = 100
   Dim GradeTotal As Integer
   Dim Grade(100) As String
   Dim score As Integer
   Dim I As Integer

Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click

[b]'This part Works fine[/b]

       For I = 0 To 100
           Grade(I) = InputBox("Enter Score")
           If Grade(I) = "" Then
               Grade(I) = -1
           ElseIf Val(Grade(I)) < 0 Then
               Exit Sub
           End If

       Next

   End Sub

[b]'Here is where I'm haveing truble. I don't know what is wrong[/b]

Function CalcGrade()
       Dim x As Integer
       For x = 0 To I
           If Val(Grade(I)) = 0 Then
               Exit Function

           If Val(Grade(I)) > HighGrade Then
               HighGrade = Val(Grade(I))
               If Val(Grade(I)) < LowGrade Then
                   LowGrade = Val(Grade(I))
               End If
           End If
           End If
       Next
   End Function

Private Sub btnCalc_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalc.Click
       CalcGrade()
       Me.lblAboveAve.Text = HighGrade
       Me.lblBelowAve.Text = LowGrade

[b]'I was hoping this would set the varebel back to oreginal[/b]  

     HighGrade = 0
       av_grade = 0
       LowGrade = 100


   End Sub
End Class

Posted
Have a look at this little change to your code....

               Exit For    'does this fix it?

 

 

No the problem is when it loop thru the array it is reading the value as 0 witch leads me to believe that it isn�t reading the value at all. The reason I know it is reading a value of 0 is; I set break points on the function when I run through it the HighGrade variable doesn�t change. How ever the LowGrade variable witch is initialized to 100 changes to 0.

Posted (edited)

Function CalcGrade()
       
       For I = 0 To 100  'Or For I = 0 To Grade.Length - 1

          If not Grade(I) = 0 then 

                   If Val(Grade(I)) > HighGrade Then
                        HighGrade = Val(Grade(I))
                   End if

                    If Val(Grade(I)) < LowGrade Then
                        LowGrade = Val(Grade(I))
                    End If
   
           End if

       Next I

End Function

 

Are the grades from 0 to 5 ? ....or ?

Edited by DR00ME

"Everything should be made as simple as possible, but not simpler."

"It's not that I'm so smart , it's just that I stay with problems longer ."

- Albert Einstein

Posted

YaHoooooooo

 

I just wanted to say thank you to all those who helped. I now have a fairly good understanding of one dimensional array�s.

 

Here is my completed code for those interested. Comments and suggestions on better or more efficient ways of doing this are welcome

 

'Global
   Dim HighGrade As Integer = 0
   Dim av_grade As Integer = 0
   Dim LowGrade As Integer = 100
   Dim GradeTotal As Integer
   Dim Grade(100) As String
   Dim score As Integer
   Dim I As Integer

Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
       For I = 0 To 100
           Grade(I) = InputBox("Enter Score")
           If Grade(I) = "" Then
               Exit For
           End If
           If Val(Grade(I)) < 0 Then
               Exit For
           End If
           GradeTotal += Val(Grade(I))
       Next
       av_grade = GradeTotal / I
   End Sub
   Function CalcGrade()
       Dim x As Integer
       For x = 0 To I - 1

           If Val(Grade(x)) > HighGrade Then
               HighGrade = Val(Grade(x))
           End If
           If Val(Grade(x)) < LowGrade Then
               LowGrade = Val(Grade(x))
           End If
       Next
   End Function



   Private Sub btnCalc_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalc.Click
       CalcGrade()
       Me.lblAboveAve.Text = HighGrade
       Me.lblBelowAve.Text = LowGrade
       HighGrade = 0
       av_grade = 0
       LowGrade = 100
       GradeTotal = 0

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...