console app help/ RESOLVED
Hi everyone, I am new to vb.net and I am having problems getting a console application to read a text file into an array and perform various calculations on it after that. I have done everything I think I know how to do and cannot even get the program to read the file when I call my readinputfile function. Any help would be greatly appreciated. I am including my code that I have.
Module Module1
Sub Main()
Dim array AsInteger()
Dim i AsInteger
Dim avg AsInteger
Dim n AsString
array = NewInteger() {}
Console.WriteLine("Please enter the filename to be evaluated")
Console.ReadLine()
ReadInputFile(array, n)
For i = 0 To array.GetUpperBound(0)
array(i) = array
Next
For i = 0 To array.GetUpperBound(0)
Console.WriteLine("Mean" & vbTab & Mean(array, n))
Console.WriteLine("Maximum" & vbTab & Maximum(array, n))
Console.WriteLine("Minimum" & vbTab & Minimum(array, n))
Console.WriteLine("Median" & vbTab & Median(array, n))
Console.WriteLine("Mode" & vbTab & Mode(array, n))
Next
Console.WriteLine("Press any key to continue")
Console.ReadLine()
EndSub
Function Mean(ByVal ar() AsInteger, ByVal n AsInteger) AsInteger
'allocating first array and copying its reference
Dim sum AsInteger = 0
Dim i AsInteger
For i = 0 To n
sum += ar(i)
Next
Return sum / n
EndFunction
Function ReadInputFile(ByRef ar() AsInteger, ByVal filename AsString) AsInteger
Dim n AsInteger = 0
Try
Dim fs AsNew System.IO.StreamReader(filename)
Dim line AsString
Do
line = fs.ReadLine()
IfNot line IsNothingThen
ar = Convert.ToInt16(line)
n += 1
EndIf
LoopUntil line IsNothing
fs.Close()
Return n
Catch E As Exception
' let the user know what went wrong
Console.WriteLine("The file could not be read:")
Console.WriteLine(E.Message)
EndTry
Return n 'number of elements read from file
EndFunction
Function Maximum(ByVal ar() AsInteger, ByVal n AsInteger) AsInteger
' n = number of elements in array
Dim max AsInteger = ar(0)
Dim i AsInteger
For i = 1 To n
If (ar(i) > max) Then max = ar(i)
Next
Return max
EndFunction
Function Minimum(ByVal ar() AsInteger, ByVal n AsInteger) AsInteger
' n = number of elements in array
Dim min AsInteger = ar(0)
Dim i AsInteger
For i = 1 To n
If (ar(i) < min) Then min = ar(i)
Next
Return min
EndFunction
Function Median(ByVal ar() AsInteger, ByVal n AsInteger) AsInteger
'n = number of elements in array
Dim i AsInteger
Dim count AsInteger = ar.GetLength(0)
If count = 0 ThenReturn (0)
ar.Sort(ar)
For i = 0 To n
If count Mod 2 = 0 Then
Return (ar((count / 2) - 1) + ar((count / 2))) / 2
Else
Return ar(count / 2)
EndIf
Next
EndFunction
Function Mode(ByVal ar() AsInteger, ByVal n AsInteger) AsInteger
Dim i AsInteger
Dim Count AsInteger
Dim Number() AsDecimal
Dim CountOfNumber AsInteger
Dim CurrentNumber AsDecimal
Dim Counter AsInteger
Dim HighestNumberIndex AsInteger
Dim HighestNumberCount AsInteger
Count = ar.GetUpperBound(0)
If Count = 0 ThenReturn 0
ar.Sort(ar)
ReDim Number(0)
CurrentNumber = ar(0)
HighestNumberIndex = 0
HighestNumberCount = 0
Number(0) = CurrentNumber
For i = 0 To n
While Counter <= Count
If CurrentNumber = ar(Counter) Then
CountOfNumber += 1
If CountOfNumber > HighestNumberCount Then
HighestNumberCount = CountOfNumber
HighestNumberIndex = Number.GetUpperBound(0)
EndIf
Else
ReDimPreserve Number(Number.GetUpperBound(0) + 1)
CurrentNumber = ar(Counter)
Number(Number.GetUpperBound(0)) = CurrentNumber
CountOfNumber = 1
EndIf
Counter += 1
EndWhile
Next
Return Number(HighestNumberIndex)
EndFunction
Function StdDev(ByVal ar() AsInteger, ByVal n AsInteger) AsInteger
Dim i AsInteger
Dim avg AsInteger, SumSq AsInteger
avg = Mean(ar, n)
For i = 1 To n
SumSq = SumSq + (ar(i) - avg) ^ 2
Next i
Return StdDev = Sqr(SumSq / ((ar(0)) - 1))
EndFunction
EndModule
Hi everyone, I am new to vb.net and I am having problems getting a console application to read a text file into an array and perform various calculations on it after that. I have done everything I think I know how to do and cannot even get the program to read the file when I call my readinputfile function. Any help would be greatly appreciated. I am including my code that I have.
Module Module1
Sub Main()
Dim array AsInteger()
Dim i AsInteger
Dim avg AsInteger
Dim n AsString
array = NewInteger() {}
Console.WriteLine("Please enter the filename to be evaluated")
Console.ReadLine()
ReadInputFile(array, n)
For i = 0 To array.GetUpperBound(0)
array(i) = array
Next
For i = 0 To array.GetUpperBound(0)
Console.WriteLine("Mean" & vbTab & Mean(array, n))
Console.WriteLine("Maximum" & vbTab & Maximum(array, n))
Console.WriteLine("Minimum" & vbTab & Minimum(array, n))
Console.WriteLine("Median" & vbTab & Median(array, n))
Console.WriteLine("Mode" & vbTab & Mode(array, n))
Next
Console.WriteLine("Press any key to continue")
Console.ReadLine()
EndSub
Function Mean(ByVal ar() AsInteger, ByVal n AsInteger) AsInteger
'allocating first array and copying its reference
Dim sum AsInteger = 0
Dim i AsInteger
For i = 0 To n
sum += ar(i)
Next
Return sum / n
EndFunction
Function ReadInputFile(ByRef ar() AsInteger, ByVal filename AsString) AsInteger
Dim n AsInteger = 0
Try
Dim fs AsNew System.IO.StreamReader(filename)
Dim line AsString
Do
line = fs.ReadLine()
IfNot line IsNothingThen
ar = Convert.ToInt16(line)
n += 1
EndIf
LoopUntil line IsNothing
fs.Close()
Return n
Catch E As Exception
' let the user know what went wrong
Console.WriteLine("The file could not be read:")
Console.WriteLine(E.Message)
EndTry
Return n 'number of elements read from file
EndFunction
Function Maximum(ByVal ar() AsInteger, ByVal n AsInteger) AsInteger
' n = number of elements in array
Dim max AsInteger = ar(0)
Dim i AsInteger
For i = 1 To n
If (ar(i) > max) Then max = ar(i)
Next
Return max
EndFunction
Function Minimum(ByVal ar() AsInteger, ByVal n AsInteger) AsInteger
' n = number of elements in array
Dim min AsInteger = ar(0)
Dim i AsInteger
For i = 1 To n
If (ar(i) < min) Then min = ar(i)
Next
Return min
EndFunction
Function Median(ByVal ar() AsInteger, ByVal n AsInteger) AsInteger
'n = number of elements in array
Dim i AsInteger
Dim count AsInteger = ar.GetLength(0)
If count = 0 ThenReturn (0)
ar.Sort(ar)
For i = 0 To n
If count Mod 2 = 0 Then
Return (ar((count / 2) - 1) + ar((count / 2))) / 2
Else
Return ar(count / 2)
EndIf
Next
EndFunction
Function Mode(ByVal ar() AsInteger, ByVal n AsInteger) AsInteger
Dim i AsInteger
Dim Count AsInteger
Dim Number() AsDecimal
Dim CountOfNumber AsInteger
Dim CurrentNumber AsDecimal
Dim Counter AsInteger
Dim HighestNumberIndex AsInteger
Dim HighestNumberCount AsInteger
Count = ar.GetUpperBound(0)
If Count = 0 ThenReturn 0
ar.Sort(ar)
ReDim Number(0)
CurrentNumber = ar(0)
HighestNumberIndex = 0
HighestNumberCount = 0
Number(0) = CurrentNumber
For i = 0 To n
While Counter <= Count
If CurrentNumber = ar(Counter) Then
CountOfNumber += 1
If CountOfNumber > HighestNumberCount Then
HighestNumberCount = CountOfNumber
HighestNumberIndex = Number.GetUpperBound(0)
EndIf
Else
ReDimPreserve Number(Number.GetUpperBound(0) + 1)
CurrentNumber = ar(Counter)
Number(Number.GetUpperBound(0)) = CurrentNumber
CountOfNumber = 1
EndIf
Counter += 1
EndWhile
Next
Return Number(HighestNumberIndex)
EndFunction
Function StdDev(ByVal ar() AsInteger, ByVal n AsInteger) AsInteger
Dim i AsInteger
Dim avg AsInteger, SumSq AsInteger
avg = Mean(ar, n)
For i = 1 To n
SumSq = SumSq + (ar(i) - avg) ^ 2
Next i
Return StdDev = Sqr(SumSq / ((ar(0)) - 1))
EndFunction
EndModule
Last edited: