Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and beginning April 20th, 2021 (Eastern Time) the Yahoo Answers website will be in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.

May I get some help with VB 2008 Coding?

Can someone tell me what I'm doing wrong in my code? I have to get the sum and the average, and the highest and lowest value of a list of 100 numbers in a text file. My program runs without error, and I get the sum and the average, but I am only getting 0's for my highest and lowest values in the file. The list of numbers in the file ranges from 6.895 to 999.475. I am outputting to a listbox, if that helps. Here is my code:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

' This procedure reads from a file

' and gives the Sum, average, highest, and lowest of the numbers

' Read a record from the file.

Dim textNumbers As StreamReader = New StreamReader("NumberSet.txt")

Me.lstNumbers.Items.Clear()

' Declare variables

Dim intNumber(100) As Integer ' ?

Dim decNumbers As Decimal

Dim decSum As Decimal ' To hold the Sum of the numbers in the file

Dim decAvg As Decimal ' To hold the average of the numbers in the file

Dim decMax As Decimal ' To hold the largest of the numbers in the file

Dim decMin As Decimal ' To hold the smallest of the numbers in the file

Dim intCount As Integer ' Loop Counter

' Get Sum of all numbers in the file

For intCount = 1 To (intNumber.Length - 1)

decNumbers = CDec(textNumbers.ReadLine())

decSum += decNumbers

lstNumbers.Text = decSum

' Get value of highest number

If decNumbers > (1000) Then

lstNumbers.Text = decMax

End If

' Get value of lowest number

If decNumbers < (7) Then

lstNumbers.Text = decMin

End If

Next intCount

' Get average of Sum of numbers

decAvg = decSum / 100

' Display the data in the list box.

lstNumbers.Items.Add("Sum: " & decSum)

lstNumbers.Items.Add("Average : " & decAvg)

lstNumbers.Items.Add("Highest : " & decMax)

lstNumbers.Items.Add("Lowest: " & decMin)

' Close the file.

textNumbers.Close()

End Sub

I've been working on this for hours, so I will really appreciate your help. Thank you. : )

Update:

Right on, Taz. That got me what I needed. You ROCK! Thank you!!

1 Answer

Relevance
  • Anonymous
    1 decade ago
    Favorite Answer

    I would do like this:

    Dim minValue as Decimal, maxValue as Decimal

    minValue = 10000000000000000 ' Some big number

    maxValue = 0

    '

    ' inside the FOR Loop

    '

    If decNumbers < minValue

    .....MinValue = decNumbers ' because the minvalue is big, this will ensure that it gets stored here

    End If

    If decNumbers > maxValue

    .....MaxValue = decNumbers

    End If

    '

    '

    '

    Source(s): TaZ
Still have questions? Get your answers by asking now.