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.
Quote:
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
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
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..
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)
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 :(
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.
Re: Question Array + Function + GOD BLESS AMERICA
st.dev is just a simple formula that's already in the code
Quote:
dVar = sse / a 'calculates variance
dSD = dVar / 2 ' calculates st.deviation
dSD is where the answer lies
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.
Re: Question Array + Function + GOD BLESS AMERICA
Quote:
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
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
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
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
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.
Re: Question Array + Function + GOD BLESS AMERICA
Okay i got it
you are the man
i am the dog