Averaging Input Scores RSS

4 replies

Last post Jun 09, 2011 05:19 PM by mattcase

Member

374 Points

518 Posts

Averaging Input Scores

Jun 07, 2011 07:44 PM|LINK

Hi All,

I am trying to create a result that shows the AVERAGE of the scores that a user inputs into my site.  The only thing is that I have an N/A option that end users can select if they do not want to add an answer to that question.

How do I average only the questions whos answer is not equal "0"?

I have attached my code for you to see.

Thanks.

Dim Q1 As Integer
Dim Q2 As Integer
Dim Q3 As Integer
Dim Q4 As Integer
Dim Q5 As Integer
Dim Q6 As Integer
Dim Q7 As Integer
Dim Q8 As Integer
Dim Q9 As Integer
Dim Q10 As Integer

Dim AddResult As Integer

AddResult = (Q1 + Q2 + Q3 + Q4 + Q5 + Q6 + Q7 + Q8 + Q9 + Q10)

Dim AvgResult As Double

AvgResult = AddResult / 10

Label1.Text = AvgResult
Matt

All-Star

24182 Points

3719 Posts

Re: Averaging Input Scores

Jun 07, 2011 08:10 PM|LINK

this uses linq and all I did was put all the values in a list of integers

List<int> qs = new List<int>() {0, 1, 2, 1, 0, 1, 1, 0};

int number= qs.Where(c => c > 0).Count();

double avg = qs.Sum()/number;

Jeev
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
If you get the answer to your question, please mark it as the answer.
• Edited by Jeev on Jun 07, 2011 08:11 PM
• Edited by Jeev on Jun 07, 2011 08:14 PM

Participant

1982 Points

470 Posts

Re: Averaging Input Scores

Jun 07, 2011 08:11 PM|LINK

First things first...10 separate variables is overkill.  You also should not include the type of control in the control name.  You can simply use a basic Dictionary to link a question ID to the answer value.  Assuming each answer is a numeric value your code would look something like the following:

Public Function GetAverage(ByVal NumberOfQuestions As Integer)
Dim Responses As New Dictionary(Of Integer, Double)
Dim MyControl As RadioButton

For i As Integer = 1 To NumberOfQuestions
Try
MyControl = CType(Page.FindControl("rbQuestion" & i.ToString), RadioButton)
Catch ex As Exception
Throw New Exception("Question #" & i.ToString & " Not Found")
End Try

Next

Dim Average As Double = 0
For Each Key As Integer In Responses.Keys
IIf(Responses(Key) = 0, NumberOfQuestions -= 1, Average += Responses(Key))
Next

If NumberOfQuestions > 0 Then
Return Average / NumberOfQuestions
End If

Return 0
End Sub

The benefits of the code above allow an ever growing number of questions as long as the answer controls are all named the same and are simple radio buttons.  If you want to vary the answer types you can change the CType to let it gather the control and then use If TypeOf MyControl Is X then the necessary logic to get the answer.  Hope that helps!

• Edited by ddelella on Jun 07, 2011 08:11 PM
• Edited by ddelella on Jun 07, 2011 08:13 PM
• Marked as answer by mattcase on Jun 09, 2011 05:19 PM

All-Star

34013 Points

5468 Posts

Re: Averaging Input Scores

Jun 07, 2011 08:37 PM|LINK

how about instead declaring 10 diffrent variables, use integer array of size 10 and assign values in array like this

Dim Q(10) as Integer

Dim AddResult As Integer

AddResult = (Q1 + Q2 + Q3 + Q4 + Q5 + Q6 + Q7 + Q8 + Q9 + Q10)

Dim AvgResult As Double

AvgResult = AddResult / (10 - Array.FindAll(Q, AddressOf CheckZero).Length)

Label1.Text = AvgResult

' separate function
Private Function CheckZero(ByVal str As String) As Boolean
If str = "0" Then
Return True
End If
End Function

findAll method used for array will give count of for how many items in array value is zero.. simply doing minus that count while taking average will give u required resutl..

note that for findAll method you need to add separate emthod like the one i used above CheckZero

hope this helps...

Cheers!
KK

Please mark as Answer if post helps in resolving your issue
My Site

Member

374 Points

518 Posts

Re: Averaging Input Scores

Jun 09, 2011 05:19 PM|LINK

Thanks!

Matt