Results 1 to 13 of 13

Thread: Question Array + Function + GOD BLESS AMERICA

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Mar 2013
    Posts
    27

    Question Question Array + Function + GOD BLESS AMERICA

    Hello all!
    I am currently trying to write a program for my class.

    The program's purpose is to find standard deviation of a group of numbers.

    I managed to write the whole the program without using any functions but the instructor wants to see function in this program. This is where I need help.

    Here is my program.


    Dim a,sum,sserror,fvar,dSD As Decimal
    Integer.Tryparse(Inputbox("Enter number"),a)
    a = int(Rnd()*100)

    Dim array(a-1) As Integer

    For i=0 to a-1
    array(i) = 5
    Next

    For j=0 to a-1
    sum = sum + array(j)
    Next

    avg= sum/a

    For k=0 to a-1
    sserror = sserror + (array(k) - avg)*2
    Next

    dVar= sse / a 'calculates variance
    dSD = dVar / 2 ' calculates st.deviation


    The whole calculation code should be done in function part of the program and it should just call the function to textbox.
    The output is the standard deviation of random numbers. It should be shown in textbox1.text

    Instructor wants us to make this output by using the code textbox1.text = stdevFUNCTION(array)

    I really appreciate any help

    MAY THE FORCE BE WITH YOU

    EVEN IF YOU DON'T LIKE STAR WARS
    Last edited by sosarya; May 21st, 2013 at 12:43 PM.

  2. #2
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    5,681

    Re: Question Array + Function + GOD BLESS AMERICA

    Well here is one way: You could place the part that creates that first array inside a function:-
    vbnet Code:
    1. '
    2.     Private Function CreateArray(ByVal count As Integer) As Integer()
    3.         Dim arr As Integer() = New Integer(count - 1) {}
    4.         For i = 0 To count - 1
    5.             arr(i) = 5
    6.         Next
    7.         Return arr
    8.     End Function

    And change your code to:-
    vbnet Code:
    1. '
    2.         Dim a, sum, sserror, fvar, dSD As Decimal
    3.         Integer.TryParse(InputBox("Enter number"), a)
    4.         a = Int(Rnd() * 100)
    5.  
    6.         Dim array As Integer() = CreateArray(a)
    7.  
    8.         For j = 0 To a - 1
    9.             sum = sum + array(j)
    10.         Next
    11.  
    12.         avg = sum / a
    13.  
    14.         For k = 0 To a - 1
    15.             sserror = sserror + (array(k) - avg) * 2
    16.         Next
    17.  
    18.         dVar = sse / a 'calculates variance
    19.         dSD = dVar / 2 ' calculates st.deviation
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena


    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. -jmcilhinney

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Mar 2013
    Posts
    27

    Re: Question Array + Function + GOD BLESS AMERICA

    Hello Niya thanks for your answer.

    I need to ask,

    How and which part of the code should I call the function, into the textbox?

    I tried textbox1.text = createarray(array) but it is not working

    Sorry I am a total newbie in functions..

  4. #4
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    5,681

    Re: Question Array + Function + GOD BLESS AMERICA

    I already changed your code to call CreateArray. Its on line 6:-
    vbnet Code:
    1. Dim array As Integer() = CreateArray(a)
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena


    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. -jmcilhinney

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Mar 2013
    Posts
    27

    Re: Question Array + Function + GOD BLESS AMERICA

    It had to be coded like this textbox1.text = stdevFUNCTION(array) because the instructor wants like this way

    st.deviation result is our output which should appear in textbox1.text

    I am confused
    Last edited by sosarya; May 21st, 2013 at 12:27 PM. Reason: fix

  6. #6
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    5,681

    Re: Question Array + Function + GOD BLESS AMERICA

    Oh, I misunderstood the question. To be honest, I don't know what a deviation is so I wouldn't know how to pick apart your code to put only the relevant parts into a function but I can point you to resources that explain how functions work and you can put the necessary parts into a function yourself. Here is one resource.
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena


    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. -jmcilhinney

  7. #7

    Thread Starter
    Junior Member
    Join Date
    Mar 2013
    Posts
    27

    Re: Question Array + Function + GOD BLESS AMERICA

    st.dev is just a simple formula that's already in the code
    dVar = sse / a 'calculates variance
    dSD = dVar / 2 ' calculates st.deviation

    dSD is where the answer lies

  8. #8
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,242

    Re: Question Array + Function + GOD BLESS AMERICA

    I'm sorry but in what sense have you "managed to write the whole the program"? With Option Strict Off, I get 5 errors in the code. With Option Strict On, it likes up like a Christmas tree! And as far as I can see (without correcting all those errors and actually trying it because life, frankly, is too short), the final calculation will always return 0 (if anything at all). It's nonsense from beginning to end.
    As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"

    Reviews: "dunfiddlin likes his DataTables" - jmcilhinney

    Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!

  9. #9
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    5,681

    Re: Question Array + Function + GOD BLESS AMERICA

    Quote Originally Posted by sosarya View Post
    st.dev is just a simple formula that's already in the code
    If so then:-
    vbnet Code:
    1. '
    2.     Private Function CalculateDeviation(ByVal sse As Decimal, ByVal a As Decimal) As Decimal
    3.         Dim dVar As Decimal
    4.         dVar = sse / a 'calculates variance
    5.         Return dVar / 2 ' calculates st.deviation
    6.     End Function
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena


    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. -jmcilhinney

  10. #10
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,242

    Re: Question Array + Function + GOD BLESS AMERICA

    vb.net Code:
    1. Option Strict On
    2.  
    3. Public Class Form1
    4.     Dim r As New Random
    5.     Private Sub Form1_Load(sender As Object, e As System.EventArgs) Handles Me.Load
    6.         Dim test(7) As Decimal ' set up a test array of
    7.         For i = 0 To 7
    8.             test(i) = r.Next(11) ' random values 0 to 10
    9.         Next
    10.         Me.Text = StandardDeviation(test).ToString ' function
    11.     End Sub
    12.  
    13.     Function StandardDeviation(ByVal values() As Decimal) As Decimal
    14.         Dim Mean As Decimal = values.Sum / values.Length ' get average of all array values
    15.         Dim Diff As Decimal
    16.         For Each d In values
    17.             Diff = CDec(Diff + ((d - Mean) ^ 2)) ' calculate difference squares & sum them
    18.         Next
    19.         Return CDec(Math.Sqrt(Diff / values.Length)) ' return deviation
    20.     End Function
    21. End Class
    As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"

    Reviews: "dunfiddlin likes his DataTables" - jmcilhinney

    Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!

  11. #11

    Thread Starter
    Junior Member
    Join Date
    Mar 2013
    Posts
    27

    Re: Question Array + Function + GOD BLESS AMERICA

    Hello! I want to add something to this code.

    I want to add a inputbox which asks for number count.

    For example if you enter 5, then you have 5 random numbers..

    Quote Originally Posted by dunfiddlin View Post
    vb.net Code:
    1. Option Strict On
    2.  
    3. Public Class Form1
    4.     Dim r As New Random
    5.     Private Sub Form1_Load(sender As Object, e As System.EventArgs) Handles Me.Load
    6.         Dim test(7) As Decimal ' set up a test array of
    7.         For i = 0 To 7
    8.             test(i) = r.Next(11) ' random values 0 to 10
    9.         Next
    10.         Me.Text = StandardDeviation(test).ToString ' function
    11.     End Sub
    12.  
    13.     Function StandardDeviation(ByVal values() As Decimal) As Decimal
    14.         Dim Mean As Decimal = values.Sum / values.Length ' get average of all array values
    15.         Dim Diff As Decimal
    16.         For Each d In values
    17.             Diff = CDec(Diff + ((d - Mean) ^ 2)) ' calculate difference squares & sum them
    18.         Next
    19.         Return CDec(Math.Sqrt(Diff / values.Length)) ' return deviation
    20.     End Function
    21. End Class

  12. #12
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,242

    Re: Question Array + Function + GOD BLESS AMERICA

    So ...

    Private Sub Form1_Load(sender As Object, e As System.EventArgs) Handles Me.Load
    inputbox goes here and value gets used as array size on the next line
    Dim test(7) As Decimal ' set up a test array of

    ... you should be able to do that without too much problem.
    As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"

    Reviews: "dunfiddlin likes his DataTables" - jmcilhinney

    Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!

  13. #13

    Thread Starter
    Junior Member
    Join Date
    Mar 2013
    Posts
    27

    Re: Question Array + Function + GOD BLESS AMERICA

    Okay i got it

    you are the man

    i am the dog
    Last edited by sosarya; May 22nd, 2013 at 04:31 PM.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width