# Thread: Question Array + Function + GOD BLESS AMERICA

1. ## 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  Reply With Quote

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```

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```  Reply With Quote

3. ## Re: Question Array + Function + GOD BLESS AMERICA

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..  Reply With Quote

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)`  Reply With Quote

5. ## 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   Reply With Quote

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.  Reply With Quote

7. ## 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  Reply With Quote

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.  Reply With Quote

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`  Reply With Quote

10. ## Re: Question Array + Function + GOD BLESS AMERICA

vb.net Code:
```Option Strict On
Public Class Form1
Dim r As New Random
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```  Reply With Quote

11. ## Re: Question Array + Function + GOD BLESS AMERICA

Hello! I want to add something to this code.

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 FunctionEnd Class`  Reply With Quote

12. ## Re: Question Array + Function + GOD BLESS AMERICA

So ...

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.  Reply With Quote

13. ## Re: Question Array + Function + GOD BLESS AMERICA

Okay i got it

you are the man

i am the dog  Reply With Quote

array, function #### Posting Permissions

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