## 4 replies

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

• mattcase

Member

115 Points

582 Posts

### Averaging Input Scores

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

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

Dim AvgResult As Double

Label1.Text = AvgResult```
Matt
• Jeev

All-Star

16800 Points

3695 Posts

### Re: Averaging Input Scores

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
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
• ddelella

Participant

1329 Points

526 Posts

### Re: Averaging Input Scores

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)

For i As Integer = 1 To NumberOfQuestions
Try
MyControl = CType(Page.FindControl("rbQuestion" & i.ToString), RadioButton)
Catch ex As Exception
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!

• kedarrkulkar...

All-Star

27258 Points

6184 Posts

### Re: Averaging Input Scores

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

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

Dim AvgResult As Double

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

My Site
• mattcase

Member

115 Points

582 Posts