one-dimensional arrays

search points in elements

Page 1 of 1

9 Replies - 23312 Views - Last Post: 14 December 2005 - 10:59 AM Rate Topic: -----

#1 polishPrincess83  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 27-September 05

one-dimensional arrays

Posted 07 December 2005 - 09:15 AM

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.

Is This A Good Question/Topic? 0
  • +

Replies To: one-dimensional arrays

#2 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,506
  • Joined: 12-July 02

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.
Was This Post Helpful? 0
  • +
  • -

#3 polishPrincess83  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 27-September 05

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

Was This Post Helpful? 0
  • +
  • -

#4 polishPrincess83  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 27-September 05

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
Was This Post Helpful? 0
  • +
  • -

#5 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,506
  • Joined: 12-July 02

Re: one-dimensional arrays

Posted 07 December 2005 - 01:03 PM

Something like
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?
Was This Post Helpful? 0
  • +
  • -

#6 polishPrincess83  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 27-September 05

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.

This post has been edited by polishPrincess83: 07 December 2005 - 01:28 PM

Was This Post Helpful? 0
  • +
  • -

#7 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,506
  • Joined: 12-July 02

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.
Was This Post Helpful? 0
  • +
  • -

#8 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,506
  • Joined: 12-July 02

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.

Well, we can do it with an if else structure as well...
Was This Post Helpful? 0
  • +
  • -

#9 AdmSteck  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 60
  • Joined: 26-November 05

Re: one-dimensional arrays

Posted 07 December 2005 - 06:26 PM

Your original code should be modified as such:

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.
Was This Post Helpful? 0
  • +
  • -

#10 polishPrincess83  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 27-September 05

Re: one-dimensional arrays

Posted 14 December 2005 - 10:59 AM

It worked! Thank you soooooo much. You guys are the best :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1