6. Number Analysis Create an application that reads the numbers from the file yo
ID: 3630502 • Letter: 6
Question
6. Number Analysis Create an application that reads the numbers from the file your application created for Programming Challenge 5. ( If you have not completed that assignment, use the file named NumberSet.txt in the Chap9 folder on the student disk. It contains a series of 100 real numbers.) Your application should perform the following: • Display the total of the numbers • Display the average of the numbers • Display the highest number in the file • Display the lowest number in the fileSample program download link:
http://media.pearsoncmg.com/aw/aw_gaddis_vb2010/Student_Sample_Programs.zip
Explanation / Answer
Imports System.IO
Public Class Form
Private Sub btnCalculate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalculate.Click
' Declare variables for calculations
Dim decTotal As Decimal
Dim decAvg As Decimal
Dim intCount As Integer
' Declare variable for StreamWriter
' Store files in the variable
Dim numberFile As StreamReader
REM: Open the file
numberFile = File.OpenText("C:UsersporraloucaMy DocumentsNumberSet.txt")
' Finds sum
Do Until numberFile.Peek = -
decTotal += CDec(numberFile.ReadLine())
intCount +=
Loop
' Displays output in the label
lblTotal.Text = decTotal.ToString
' Display the number average
decAvg = decTotal / intCount
lblAvg.Text = decAvg.ToString
REM: Close the file.
numberFile.Close()
REM: Open the file.
numberFile = File.OpenText("C:UsersporraloucaMy DocumentsNumberSet.txt")
Dim decNumArray(intCount) As Decimal
' Initialize Counter.
intCount =
Do Until numberFile.Peek = -
decNumArray(intCount) = CDec(numberFile.ReadLine())
intCount +=
Loop
REM: Close the file.
numberFile.Close()
' Find the Highest.
Dim decHighest As Decimal = decNumArray()
For Each decVal As Decimal In decNumArray
If decVal > decHighest Then
decHighest = decVal
End If
Next
' Display the Highest value in the label.
lblHighest.Text = decHighest.ToString
' Find the Lowest.
Dim decLowest As Decimal = decNumArray()
For intCount = To (decNumArray.Length - )
If decNumArray(intCount) < decLowest Then
decLowest = decNumArray(intCount)
End If
Next
' Display the highest number.
lblLowest.Text = decLowest.ToString
End Sub
Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
' Closes the application
Me.Close()
End Sub
End Class
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.