I have a couple problems i'm not sure how to solve. Under function process string, i'm taking a line of string from a notepad, using split to split up the numbers, and then trying to store them in a integer array. I'm pretty new to arrays, so i'm not sure what i did wrong. I also need to display the contents of an array to a message box along with a letter grade next to it. I've also played around with this, but don't know how to do it. Finally, i need to calculate the amount of numbers below average in array z under store_values function. Under the numbers below average, i'm not quite sure how to do this, as what i have keeps screwing up. Here is the project list, I think everything else I've done is correct.
At the bottom of this assignment is some text.
Copy and paste it into Notepad, and save the file to your hard drive (any location).
Using the previous assignment as a seed for breaking the string into the three numbers write code that:
Reads the data from a data file, line by line.
Each line that is read must be passed to a function that returns an array of three numbers (do not use String arrays to break the string into three numbers).
Assume the delimieter is provided by the user (so it could be comma as shown below, or it could be Tab, or space, etc).
Once the data is read into an 2-D array, write a handler code that displays the final score which is 50% of the average of the first two grades + 50% of the third grade. For each row, display yhe final score and a letter grade of A, B, C, D or F.
The handler above should also show the min, max, avg and number of grades below average as in the previous two assignments.
Note that your code should work for any number of lines of data as long as each line follows the format below:
Data:
82,79,80
75,59,89
68,72,85
98,90,89
89,82,91
62,75,60
At the bottom of this assignment is some text.
Copy and paste it into Notepad, and save the file to your hard drive (any location).
Using the previous assignment as a seed for breaking the string into the three numbers write code that:
Reads the data from a data file, line by line.
Each line that is read must be passed to a function that returns an array of three numbers (do not use String arrays to break the string into three numbers).
Assume the delimieter is provided by the user (so it could be comma as shown below, or it could be Tab, or space, etc).
Once the data is read into an 2-D array, write a handler code that displays the final score which is 50% of the average of the first two grades + 50% of the third grade. For each row, display yhe final score and a letter grade of A, B, C, D or F.
The handler above should also show the min, max, avg and number of grades below average as in the previous two assignments.
Note that your code should work for any number of lines of data as long as each line follows the format below:
Data:
82,79,80
75,59,89
68,72,85
98,90,89
89,82,91
62,75,60
Code:
Imports System.IO
Public Class Stats
'Jeffrey Martinez 'Project 15
Function process_String(ByVal ps As String, ByVal d As String) As Integer()
'gathers data and splits it, storing it in string array
Dim stringarray() = Split(ps, "d")
Dim usrinput = Array.ConvertAll(stringarray, Function(str) Int32.Parse(str))
Return (usrinput)
End Function
Function store_values(ByVal x As String) As String
Dim total_array() As Integer
Dim real_array(9, 3) As Integer
Dim Sr As StreamReader
Dim s As String
Dim i As Integer = 0
Dim Minimum As Integer
Dim z(9) As Integer 'array to copy column 4 of real_array to
Dim maximum As Integer
Dim average_ As Double 'number might contain fractional part
Dim total_ As Integer
Dim underaverage As Integer
Sr = File.OpenText(x)
Do While Sr.Peek() <> -1
s = Sr.ReadLine
total_array = process_String(s, txtrddelim.Text)
real_array(i, 0) = total_array(0)
real_array(i, 1) = total_array(1)
real_array(i, 2) = total_array(2)
i += i
If i = real_array.GetUpperBound(0) Then
ReDim Preserve real_array(real_array.GetUpperBound(0) + 1, 3)
End If
Loop
Sr.Close()
'gives fourth column of array that column being the final score
For i = 0 To real_array.GetUpperBound(0)
real_array(i, 3) = (((real_array(i, 0) + real_array(i, 1) / 2) * 0.5) + (0.5 * real_array(i, 2)))
Next
'makes copy to pass to min max avg etc
For i = 0 To z.GetUpperBound(0)
z(i) = real_array(i, 3)
Next
Minimum = min(z)
maximum = max(z)
average_ = Average(z)
total_ = Total(z)
underaverage = belowavg(z)
'display statistics
Return ("statistics:" & ControlChars.CrLf & "average is " & average_.ToString & ControlChars.CrLf & "max is " & maximum.ToString & ControlChars.CrLf & "min is " & Minimum.ToString & ControlChars.CrLf & "amount of numbers under average: " & average_.ToString & ControlChars.CrLf & "total " & total_.ToString _
& z.ToString)
End Function
Function findgrad(ByVal x As Integer) As String
Dim finalgrade As String
If x < 60 Then
finalgrade = "f"
ElseIf x < 70 Then
finalgrade = "d"
ElseIf x < 80 Then
finalgrade = "c"
ElseIf x < 90 Then
finalgrade = "b"
ElseIf x >= 90 Then
finalgrade = "a"
End If
Return (finalgrade)
End Function
Function Total(ByVal a() As Integer) As Integer
'define variables
Dim intTotal As Integer = 0
Dim intCount As Integer
'total all numbers
For intCount = 0 To (a.GetUpperBound(0))
intTotal += a(intCount)
Next
'return total
Return (intTotal)
End Function
Function Average(ByVal b() As Integer) As Double
'define variables
'use double for devision
Dim intTotal As Integer = 0
Dim dblAverage As Double
Dim intcount As Integer
'gathers total
For intcount = 0 To (b.GetUpperBound(0))
intTotal += b(intcount)
Next
'devide to get average
dblAverage = intTotal / (b.GetUpperBound(0) + 1)
Return (dblAverage)
End Function
Function min(ByVal c() As Integer) As Integer
'define variables
Dim intCount As Integer
Dim IntLowest As Integer
'define lowest variable
IntLowest = c(0)
For intCount = 1 To (c.GetUpperBound(0))
If c(intCount) < IntLowest Then
IntLowest = c(intCount)
End If
Next
'return lowest value
Return (IntLowest)
End Function
Function max(ByVal c() As Integer) As Integer
'define variables
Dim intCount As Integer
Dim IntHighest As Integer
'define lowest variable
IntHighest = c(0)
For intCount = 1 To (c.GetUpperBound(0))
If c(intCount) > IntHighest Then
IntHighest = c(intCount)
End If
Next
'return lowest value
Return (IntHighest)
End Function
Function belowavg(ByVal d() As Integer) As Integer
'define variables
Dim cntr As Integer = 0
Dim intTotal As Integer = 0
Dim dblAverage As Double
Dim intcount As Integer
For intcount = 0 To (d.GetUpperBound(0))
intTotal += d(intcount)
Next
dblAverage = intTotal / (d.GetUpperBound(0) + 1)
'counter for counting how many numbers are below average
For i = 0 To d.GetUpperBound(0)
If d(i) < dblAverage Then
cntr += 1
End If
Next
Return (cntr)
End Function
Private Sub btnQuit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnQuit.Click
Me.Close()
End Sub
Private Sub btnReset_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnReset.Click
txtUserinput.Text = ""
End Sub
Private Sub btnOpenfile_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOpenfile.Click
'changes default of where opendialog box willshow
OpenFileDialog1.InitialDirectory = "C:\Users\AllStarJam\Desktop"
'if they press cancel tells user no file selected
If OpenFileDialog1.ShowDialog() = Windows.Forms.DialogResult.Cancel Then
MessageBox.Show("no file selected")
End If
MessageBox.Show((store_values(OpenFileDialog1.FileName)))
End Sub
End Class