# visual basic

posted by on .

Im trying to get this program to average three number having a problem the sample nmubers im using are 6 6 2 average supposed to be 4.6667 but im just geting .6667 I think it has something to do with my s=value(i) not sure what i need to do now can anybody help?

Dim no As Integer = 3
Dim value() As Integer
ReDim value(0 To no - 1)
Dim s As Integer = 0
For i As Integer = 0 To no - 1
s = value(i)
Next
Dim a As Double = s / no
Console.WriteLine(" value is " & a)

• visual basic - ,

You just have a minor typo,
s = value(i)
s += value(i)

Everything falls in place.

• visual basic - ,

Are the numbers to be averaged always integers?
If not, you have some corrections to do.

• visual basic - ,

thanks yes the numbers are always to be averaged as integers.

• visual basic - ,

You're welcome anytime, I love programming!

• visual basic - ,

Ok got one more i need help on the program below im supposed to sort the usernames and passwords in alphabetical order by username. Then display the sorted resort. I was able to sort username alphabetically but im not sure how to get the passwords to match up with the usernames.

Dim n As Integer = 5
Dim user(0 To n - 1) As String
Dim password(0 To n - 1) As String

For i As Integer = 0 To n - 1
Console.Write(" Enter user name: ")

Next

'Sorting the entered user names

'using bubble sort

For i As Integer = 0 To 4
For j As Integer = 0 To n - i - 2

If (user(j) > user(j + 1)) Then

swap(user(j), user(j + 1))

End If
Next
Next

For i As Integer = 0 To n - 1

Console.WriteLine(user(i))

Next

End Sub

Sub swap(ByRef str1 As String, ByRef str2 As String)

Dim temp As String

temp = str1

str1 = str2

str2 = temp

• visual basic - ,

I didn't verify your code, but I see the general way to do it. Suppose you have

now, after the swap above, you have

which is wrong, but you can fix it easily by swapping the passwords along with the names, like:

If (user(j) > user(j + 1)) Then
swap(user(j), user(j + 1))
End If

and now user and password will stay together.

• visual basic - ,

Exactly!
With Jim's proposed corrections, you'd be on your way.

Also, if the value of n is a constant, you could replace Dim n... by
Const N as integer=5
This will help the compiler check for code that tries to modify N.

• visual basic - ,

thanks i really appericate the help! Im starting to like programming more myself!

• visual basic - ,

Good to hear! Till next time.