# one-dimensional arrays

Page 1 of 1

## 9 Replies - 27422 Views - Last Post: 14 December 2005 - 10:59 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=13618&amp;s=34fd4377ba7bbf9832dac603bc1dcf69&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 polishPrincess83

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

# one-dimensional arrays

Posted 07 December 2005 - 09:15 AM

This is the instructions:

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
Else
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

• g+ + -o drink whiskey.cpp

Reputation: 249
• Posts: 13,507
• 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.

### #3 polishPrincess83

Reputation: 0
• 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

### #4 polishPrincess83

Reputation: 0
• 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
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(1).points = 349
final(2).points = 399
final(3).points = 449
final(4).points = 500

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
Else
End If

I can't figure out how to make it 0 to 299 for element 0 and so on

• g+ + -o drink whiskey.cpp

Reputation: 249
• Posts: 13,507
• 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

```

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 polishPrincess83

Reputation: 0
• 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

• g+ + -o drink whiskey.cpp

Reputation: 249
• Posts: 13,507
• 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.

• g+ + -o drink whiskey.cpp

Reputation: 249
• Posts: 13,507
• 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...

Reputation: 1
• Posts: 61
• 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

```

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 polishPrincess83

Reputation: 0
• 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