|
-
May 21st, 2013, 11:07 AM
#1
Thread Starter
Junior Member
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.
-
May 21st, 2013, 11:33 AM
#2
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:
'
Private Function CreateArray(ByVal count As Integer) As Integer()
Dim arr As Integer() = New Integer(count - 1) {}
For i = 0 To count - 1
arr(i) = 5
Next
Return arr
End Function
And change your code to:-
vbnet Code:
'
Dim a, sum, sserror, fvar, dSD As Decimal
Integer.TryParse(InputBox("Enter number"), a)
a = Int(Rnd() * 100)
Dim array As Integer() = CreateArray(a)
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
-
May 21st, 2013, 11:50 AM
#3
Thread Starter
Junior Member
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..
-
May 21st, 2013, 12:07 PM
#4
Re: Question Array + Function + GOD BLESS AMERICA
I already changed your code to call CreateArray. Its on line 6:-
vbnet Code:
Dim array As Integer() = CreateArray(a)
-
May 21st, 2013, 12:21 PM
#5
Thread Starter
Junior Member
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
-
May 21st, 2013, 12:28 PM
#6
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.
-
May 21st, 2013, 12:40 PM
#7
Thread Starter
Junior Member
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
-
May 21st, 2013, 12:41 PM
#8
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!
-
May 21st, 2013, 12:44 PM
#9
Re: Question Array + Function + GOD BLESS AMERICA
 Originally Posted by sosarya
st.dev is just a simple formula that's already in the code
If so then:-
vbnet Code:
' Private Function CalculateDeviation(ByVal sse As Decimal, ByVal a As Decimal) As Decimal Dim dVar As Decimal dVar = sse / a 'calculates variance Return dVar / 2 ' calculates st.deviation End Function
-
May 21st, 2013, 03:02 PM
#10
Re: Question Array + Function + GOD BLESS AMERICA
vb.net Code:
Option Strict On
Public Class Form1
Dim r As New Random
Private Sub Form1_Load(sender As Object, e As System.EventArgs) Handles Me.Load
Dim test(7) As Decimal ' set up a test array of
For i = 0 To 7
test(i) = r.Next(11) ' random values 0 to 10
Next
Me.Text = StandardDeviation(test).ToString ' function
End Sub
Function StandardDeviation(ByVal values() As Decimal) As Decimal
Dim Mean As Decimal = values.Sum / values.Length ' get average of all array values
Dim Diff As Decimal
For Each d In values
Diff = CDec(Diff + ((d - Mean) ^ 2)) ' calculate difference squares & sum them
Next
Return CDec(Math.Sqrt(Diff / values.Length)) ' return deviation
End Function
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!
-
May 22nd, 2013, 03:57 PM
#11
Thread Starter
Junior Member
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..
 Originally Posted by dunfiddlin
vb.net Code:
Option Strict On Public Class Form1 Dim r As New Random Private Sub Form1_Load(sender As Object, e As System.EventArgs) Handles Me.Load Dim test(7) As Decimal ' set up a test array of For i = 0 To 7 test(i) = r.Next(11) ' random values 0 to 10 Next Me.Text = StandardDeviation(test).ToString ' function End Sub Function StandardDeviation(ByVal values() As Decimal) As Decimal Dim Mean As Decimal = values.Sum / values.Length ' get average of all array values Dim Diff As Decimal For Each d In values Diff = CDec(Diff + ((d - Mean) ^ 2)) ' calculate difference squares & sum them Next Return CDec(Math.Sqrt(Diff / values.Length)) ' return deviation End Function End Class
-
May 22nd, 2013, 04:13 PM
#12
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!
-
May 22nd, 2013, 04:20 PM
#13
Thread Starter
Junior Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|