Need VB help

Discuss how to write good code, break bad code, your current pet projects, or the best way to approach novel problems

Need VB help

Post by danny616 on Thu Dec 15, 2011 7:44 pm
([msg=63307]see Need VB help[/msg])

I have a visual basic project due in 2hrs. and i cant figure it out i hate arrays! well i was told to go to this site and it seems like you guys are the best to ask! so here it is.


Specifics
You will use 1 file to enter all the scores. The file is named Data.txt and should be stored in the projects Debug folder. The scores are floating point numbers.

One button should calculate the mean, range, and standard deviation and display them.
You must use separate functions to calculate the 3 statistics.

One button should display the frequencies in tabular form. For this exercise, the frequencies we are interested in are as follows:
# scores < 60
60 <= # scores < 70
70 <= # scores < 80
80 <= # scores < 90
90 <= # scores

You must use a separate array to hold the totals for the individual ranges.

All scores should be displayed in a listbox.
danny616
New User
New User
 
Posts: 3
Joined: Thu Dec 15, 2011 7:42 pm
Blog: View Blog (0)


Re: Need VB help

Post by LoGiCaL__ on Thu Dec 15, 2011 8:37 pm
([msg=63309]see Re: Need VB help[/msg])

Paste in code tags what you have already.
User avatar
LoGiCaL__
Addict
Addict
 
Posts: 1061
Joined: Sun May 30, 2010 12:33 pm
Blog: View Blog (0)


Re: Need VB help

Post by danny616 on Thu Dec 15, 2011 9:02 pm
([msg=63310]see Re: Need VB help[/msg])

lol its quite pathetic. im not good at arrays. quite frankly i dont get them at all.

Code: Select all
Option Strict On

Public Class Form1

    Private names() As String = IO.File.ReadAllLines("data.txt")
    Private scores(names.Count - 1) As double

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        For i As Integer = 0 To names.Count - 1
            scores(i) = CInt(names(i))
        Next
    End Sub
    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        Me.Close()
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim sum As Double = 0
        mean(sum)
        OutputListBox.Items.Add(sum)
    End Sub

    Function mean(ByRef sum As Double) As Double
        Dim total As Double = scores(0)

        For i As Double = 0 To scores.Count - 1
            sum =
        Next
        Return sum
    End Function

End Class


-- Thu Dec 15, 2011 9:03 pm --

LoGiCaL__ wrote:Paste in code tags what you have already.


-- Thu Dec 15, 2011 9:06 pm --

here is the whole project. http://www.megaupload.com/?d=909L5NH0
danny616
New User
New User
 
Posts: 3
Joined: Thu Dec 15, 2011 7:42 pm
Blog: View Blog (0)


Re: Need VB help

Post by LoGiCaL__ on Thu Dec 15, 2011 9:42 pm
([msg=63311]see Re: Need VB help[/msg])

Not that familiar with vb but this following line looks like it leaves sum unassigned..

Code: Select all
Function mean(ByRef sum As Double) As Double
        Dim total As Double = scores(0)

        For i As Double = 0 To scores.Count - 1
            sum =                //<----This Line------------------
        Next
        Return sum
    End Function
User avatar
LoGiCaL__
Addict
Addict
 
Posts: 1061
Joined: Sun May 30, 2010 12:33 pm
Blog: View Blog (0)


Re: Need VB help

Post by danny616 on Thu Dec 15, 2011 9:46 pm
([msg=63312]see Re: Need VB help[/msg])

yea it does i didnt get that far. i need it to pass the values from "data.txt" which is in the debug folder. to three functions
One function to calculate the range of data.txt, one function to calculate the mean of data.txt, and another function to calculate the standard deviation. its pretty hard!

-- Thu Dec 15, 2011 10:04 pm --

got all the way to the range function

Code: Select all
Option Strict On
Option Explicit On

Partial Public Class Form1

    Private scores() As Double

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

            ' First, load the scores from the file into memory
            Call LoadScores()

            Dim value As Double
            Dim value2 As Double
            Dim value3 As Double

            'calculate the mean
            value = CalculateMean()
            OutputListBox.Items.Add("Mean = " & value.ToString)
            'calculate the range
            value2 = CalculateRange()
            OutputListBox.Items.Add("Range = " & value2.ToString)
            'calculate the standard deviation
            value2 = Calculatedeviation()
            OutputListBox.Items.Add("Standard deviation = " & value3.ToString)






        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

   
    Private Sub LoadScores()

        Dim stringScores() As String

        ' Reads all of the scores in the file into an array of strings
        stringScores = IO.File.ReadAllLines("data.txt")

        ' Resize the scores array to hold all of the values in the file
        ReDim scores(stringScores.Length)

        Dim counter As Integer

        ' converts strings to numbers, and store them in scores
        For Each sValue As String In stringScores
            ' Converts the string value from the file into a double and stores it in the current location in the scores array
            scores(counter) = CDbl(sValue)
            counter += 1
        Next
    End Sub

    ' Calculates mean
    Function CalculateMean() As Double

        If scores.Length <> 0 Then
            Dim total As Double

            For Each value As Double In scores
                total += value
            Next

            Return total / scores.Length
        Else
            ' Avoid a divide by zero error
            Return 0
        End If
    End Function

    Function CalculateRange() As Double
        If scores.Length <> 0 Then
            Dim total As Double

            For Each value As Double In scores
                total += value
            Next

            Return total

        End If
    End Function

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        Me.Close()
    End Sub


End Class
danny616
New User
New User
 
Posts: 3
Joined: Thu Dec 15, 2011 7:42 pm
Blog: View Blog (0)



Return to Programming

Who is online

Users browsing this forum: No registered users and 0 guests