Hello:
I have run into a snag in my loop program. I was able to figure out the maxium sum and average of the sum. But am not able to figure out how to:
1) List the minimum value
2) List the sum of the numers divisible by 5.
These are the numbers.text displayed:
5
15
6
12
7
11
21
25
8
10
14
This is the code:
Private Sub butStart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles butStart.Click
Dim sr As IO.StreamReader = IO.File.OpenText("numbers.txt")
Dim num As Integer
Dim count, max, min As Integer
Dim sum, avg As Double
sum = 0
count = 0
min = 0
Do While sr.Peek <> -1
num = CInt(sr.ReadLine)
lstAnswer.Items.Add(CStr(num))
sum = sum + num
count += 1
If count = 1 Then
max = num
Else
If num > max Then 'for later numbers look for a new max
max = num
End If
Loop
sr.Close()
' compute statistics
If count > 0 Then
avg = sum / count
lstAnswer.Items.Add("")
lstAnswer.Items.Add("Read " & CStr(count) & " numbers")
lstAnswer.Items.Add("Average value " & FormatNumber(avg, 2))
lstAnswer.Items.Add("Maximum value " & CStr(max))
lstAnswer.Items.Add("Minimum value " & CStr(min))
Else
lstAnswer.Items.Add("File was empty")
End If
End Sub
VB Loop Minimum Sum and DivisiblesLoops
Page 1 of 1
1 Replies - 933 Views - Last Post: 20 November 2009 - 01:25 AM
Replies To: VB Loop Minimum Sum and Divisibles
#2
Re: VB Loop Minimum Sum and Divisibles
Posted 20 November 2009 - 01:25 AM
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim sr As IO.StreamReader = IO.File.OpenText("C:\Documents and Settings\Administrator\Desktop\Numbers.txt")
Dim num As Integer
Dim count, max, min As Integer
Dim sum, avg As Double
Dim noDivbyfive As Integer
sum = 0
count = 0
Do While sr.Peek <> -1
num = CInt(sr.ReadLine)
lstAnswer.Items.Add(CStr(num))
sum = sum + num
count += 1
If num Mod 5 = 0 Then
noDivbyfive += num
End If
If count = 1 Then
max = num
min = num
Else
If num > max Then 'for later numbers look for a new max
max = num
End If
If num < min Then
min = num
End If
End If
Loop
sr.Close()
' compute statistics
If count > 0 Then
avg = sum / count
lstAnswer.Items.Add("")
lstAnswer.Items.Add("Read " & CStr(count) & " numbers")
lstAnswer.Items.Add("Average value " & FormatNumber(avg, 2))
lstAnswer.Items.Add("Maximum value " & CStr(max))
lstAnswer.Items.Add("Minimum value " & CStr(min))
lstAnswer.Items.Add("Sum of number div by 5 value " & noDivbyfive.ToString)
Else
lstAnswer.Items.Add("File was empty")
End If
End Sub
Page 1 of 1
|
|

New Topic/Question
Reply




MultiQuote



|