Asked by vsu1
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
Console.WriteLine(" Input your value:")
value(i) = Console.ReadLine()
s = value(i)
Next
Dim a As Double = s / no
Console.WriteLine(" value is " & a)
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
Console.WriteLine(" Input your value:")
value(i) = Console.ReadLine()
s = value(i)
Next
Dim a As Double = s / no
Console.WriteLine(" value is " & a)
Answers
Answered by
MathMate
You just have a minor typo,
s = value(i)
should read
s += value(i)
Everything falls in place.
s = value(i)
should read
s += value(i)
Everything falls in place.
Answered by
MathMate
Are the numbers to be averaged always integers?
If not, you have some corrections to do.
If not, you have some corrections to do.
Answered by
vsu1
thanks yes the numbers are always to be averaged as integers.
Answered by
MathMate
You're welcome anytime, I love programming!
Answered by
vsu1
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: ")
user(i) = Console.ReadLine()
Console.Write(" Enter password: ")
password(i) = Console.ReadLine()
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))
Console.WriteLine(password(i))
Next
End Sub
Sub swap(ByRef str1 As String, ByRef str2 As String)
Dim temp As String
temp = str1
str1 = str2
str2 = temp
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: ")
user(i) = Console.ReadLine()
Console.Write(" Enter password: ")
password(i) = Console.ReadLine()
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))
Console.WriteLine(password(i))
Next
End Sub
Sub swap(ByRef str1 As String, ByRef str2 As String)
Dim temp As String
temp = str1
str1 = str2
str2 = temp
Answered by
jim
I didn't verify your code, but I see the general way to do it. Suppose you have
user(1) = "vsu1"; password(1)="123456"
user(2) = "jim"; password(2)="abcdef"
now, after the swap above, you have
user(1) = "jim"; password(1)="123456"
user(2) = "vsu1"; password(2)="abcdef"
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))
swap(password(j), password(j + 1))
End If
and now user and password will stay together.
user(1) = "vsu1"; password(1)="123456"
user(2) = "jim"; password(2)="abcdef"
now, after the swap above, you have
user(1) = "jim"; password(1)="123456"
user(2) = "vsu1"; password(2)="abcdef"
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))
swap(password(j), password(j + 1))
End If
and now user and password will stay together.
Answered by
MathMate
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.
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.
Answered by
vsu1
thanks i really appericate the help! Im starting to like programming more myself!
Answered by
MathMate
Good to hear! Till next time.
There are no AI answers yet. The ability to request AI answers is coming soon!
Submit Your Answer
We prioritize human answers over AI answers.
If you are human, and you can answer this question, please submit your answer.