This is the instructions:
Minimum Points Maximum Points Grade
0 299 F
300 349 D
350 399 C
400 449 B
450 500 A
Code the Display Grade Click even procedure so it searches the points array for the number of points entered and then displays the grade.
This is what I have so far:
Private Sub uiDisplayButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles uiDisplayButton.Click
Dim points() As String = {"0", "300", "350", "400", "450"}
Dim grade() As String = {"F", "D", "C", "B", "A"}
Dim x As Integer
Dim element As Integer
Dim searchFor As String
searchFor = Me.uiPointsTextBox.Text
'search the array
Do While x < points.Length _
AndAlso searchFor <> points(x)
x = x + 1
Loop
If x < points.Length Then
Me.uiGradeLabel.Text = Convert.ToString(grade(x))
Else
Me.uiGradeLabel.Text = "not valid"
End If
End Sub
When I test it I can only use 0,300,350,400,450..... how do I do it so the first element is between 0 to 299? I need to test 210 to display an F and 455 to display an A.
one-dimensional arrayssearch points in elements
Page 1 of 1
9 Replies - 19040 Views - Last Post: 14 December 2005 - 10:59 AM
Replies To: one-dimensional arrays
#2
Re: one-dimensional arrays
Posted 07 December 2005 - 09:30 AM
As you are using string literals, you will not be able to get a range. Since you have used arrays here, I am going to assume that your assignment requires their use. As such, I would make the grade array an array on Integers (the upper limts (0,300,etc)...when a number is entered by the user, check the arrays to see where it falls (what is it above, and what is it below), then you'll know which grade to take.
#3
Re: one-dimensional arrays
Posted 07 December 2005 - 09:33 AM
I'm sorry, but I'm not understanding what you mean by that. Yes we are supposed to use the arrays.... I'm thinking that maybe I have to do an if then statement or something to that sort just not sure how
This post has been edited by polishPrincess83: 07 December 2005 - 09:34 AM
#4
Re: one-dimensional arrays
Posted 07 December 2005 - 12:55 PM
ok- i've messed around with it a little more this is what I have now:
Structure Item
Public points As String
Public grade As String
End Structure
Private Sub uiDisplayButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles uiDisplayButton.Click
Dim x As Integer
Dim searchFor As String
'declare array
Dim final(4) As Item
'assign points and grade to the array
final(0).points = 299
final(0).grade = "F"
final(1).points = 349
final(1).grade = "D"
final(2).points = 399
final(2).grade = "C"
final(3).points = 449
final(3).grade = "B"
final(4).points = 500
final(4).grade = "A"
'assign input to variable
searchFor = Me.uiPointsTextBox.Text
'search the array
Do While x < final.Length _
AndAlso searchFor <> final(x).points
x = x + 1
Loop
If x < final.Length Then
Me.uiGradeLabel.Text = Convert.ToString(final(x).grade)
Else
Me.uiGradeLabel.Text = "not valid"
End If
I can't figure out how to make it 0 to 299 for element 0 and so on
Structure Item
Public points As String
Public grade As String
End Structure
Private Sub uiDisplayButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles uiDisplayButton.Click
Dim x As Integer
Dim searchFor As String
'declare array
Dim final(4) As Item
'assign points and grade to the array
final(0).points = 299
final(0).grade = "F"
final(1).points = 349
final(1).grade = "D"
final(2).points = 399
final(2).grade = "C"
final(3).points = 449
final(3).grade = "B"
final(4).points = 500
final(4).grade = "A"
'assign input to variable
searchFor = Me.uiPointsTextBox.Text
'search the array
Do While x < final.Length _
AndAlso searchFor <> final(x).points
x = x + 1
Loop
If x < final.Length Then
Me.uiGradeLabel.Text = Convert.ToString(final(x).grade)
Else
Me.uiGradeLabel.Text = "not valid"
End If
I can't figure out how to make it 0 to 299 for element 0 and so on
#5
Re: one-dimensional arrays
Posted 07 December 2005 - 01:03 PM
Something like
Now I am not near a vb compiler, so that may not be the exact code, but it should give an fairly good idea.
Better question...is this VB or vb.net?
Dim points() As Integer = {0, 300, 350, 400, 450}
Dim grade() As String = {"F", "D", "C", "B", "A"}
Dim index As Integer
Dim index2 As Integer
Dim searchFor as Integer
index2 = 0
searchFor = Me.uiPointsTextBox.Text
For index = 1 to UBound(points)
If searchFor <= points(index) Then
index2=index-1
Exit For
End If
Next
If index2=0 then
index2 = UBound(points)
End If
Me.uiGradeLabel.Text = grade(index2)
Now I am not near a vb compiler, so that may not be the exact code, but it should give an fairly good idea.
Better question...is this VB or vb.net?
#6
Re: one-dimensional arrays
Posted 07 December 2005 - 01:14 PM
VB.Net
I really appreciate your help- but that's too advanced for me... we haven't learned "For Next" or "Ubound" so for me to have that in my code, would be out of the ordinary.... but thank you very much.
I really appreciate your help- but that's too advanced for me... we haven't learned "For Next" or "Ubound" so for me to have that in my code, would be out of the ordinary.... but thank you very much.
This post has been edited by polishPrincess83: 07 December 2005 - 01:28 PM
#7
Re: one-dimensional arrays
Posted 07 December 2005 - 01:16 PM
Well then you'll likey have to switch the UBound function to array.length.
#8
Re: one-dimensional arrays
Posted 07 December 2005 - 02:17 PM
polishPrincess83, on 7 Dec, 2005 - 04:11 PM, said:
VB.Net
I really appreciate your help- but that's too advanced for me... we haven't learned "For Next" or "Ubound" so for me to have that in my code, would be out of the ordinary.... but thank you very much.
I really appreciate your help- but that's too advanced for me... we haven't learned "For Next" or "Ubound" so for me to have that in my code, would be out of the ordinary.... but thank you very much.
Well, we can do it with an if else structure as well...
#9
Re: one-dimensional arrays
Posted 07 December 2005 - 06:26 PM
Your original code should be modified as such:
So far we've changed the points array and searchFor to integer values. This allows us to do mathmatical comparisons on them. If you store them as string values, the computer will do comparisons as if they were letters and words instead of numbers. In addition, I added '501' to the points array and two 'Not Valid's to the grades array. These will act as traps for any invalid values that are entered into the form. We also assigned 0 to x. In your original code, you tried to use x before you assisgned a value to it.
Alright. Here's were we compare the values. If you step through the code with different values you can see how it works. We keep looping until either we reach the end of the the points array (which should never really happen with this code) or the value we are searching for is greater than or equal to the value in the array. Lastly we set the text of the uiGradeLabel equal to the same index of the grade array that we ended the loop of the points array with. Hope this helps. If you have any questions on how this works, please let me know. Happy coding.
Dim points() As Integer = {0, 300, 350, 400, 450, 501}
Dim grade() As String = _
{"Not Valid", "F", "D", "C", "B", "A", "Not Valid"}
Dim x As Integer = 0
Dim searchFor As Integer
searchFor = Integer.Parse(Me.uiPointsTextBox.Text)
So far we've changed the points array and searchFor to integer values. This allows us to do mathmatical comparisons on them. If you store them as string values, the computer will do comparisons as if they were letters and words instead of numbers. In addition, I added '501' to the points array and two 'Not Valid's to the grades array. These will act as traps for any invalid values that are entered into the form. We also assigned 0 to x. In your original code, you tried to use x before you assisgned a value to it.
Do While x < points.Length AndAlso searchFor >= points(x) x = x + 1 Loop Me.uiGradeLabel.Text = Convert.ToString(grade(x))
Alright. Here's were we compare the values. If you step through the code with different values you can see how it works. We keep looping until either we reach the end of the the points array (which should never really happen with this code) or the value we are searching for is greater than or equal to the value in the array. Lastly we set the text of the uiGradeLabel equal to the same index of the grade array that we ended the loop of the points array with. Hope this helps. If you have any questions on how this works, please let me know. Happy coding.
#10
Re: one-dimensional arrays
Posted 14 December 2005 - 10:59 AM
It worked! Thank you soooooo much. You guys are the best
Page 1 of 1
|
|

New Topic/Question
Reply



MultiQuote



|