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
Q1 = Question1RadioButton.SelectedValue
Q2 = Question2RadioButton.SelectedValue
Q3 = Question3RadioButton.SelectedValue
Q4 = Question4RadioButton.SelectedValue
Q5 = Question5RadioButton.SelectedValue
Q6 = Question6RadioButton.SelectedValue
Q7 = Question7RadioButton.SelectedValue
Q8 = Question8RadioButton.SelectedValue
Q9 = Question9RadioButton.SelectedValue
Q10 = Question10RadioButton.SelectedValue
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
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
Responses.Add(i, MyControl.SelectedValue)
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!
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
Q(0) = Question1RadioButton.SelectedValue
Q(1)= Question2RadioButton.SelectedValue
Q(2) = Question3RadioButton.SelectedValue
Q(3) = Question4RadioButton.SelectedValue
Q(4) = Question5RadioButton.SelectedValue
Q(5) = Question6RadioButton.SelectedValue
Q(6) = Question7RadioButton.SelectedValue
Q(7) = Question8RadioButton.SelectedValue
Q(8) = Question9RadioButton.SelectedValue
Q(9) = Question10RadioButton.SelectedValue
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 BooleanIf str = "0" ThenReturn TrueEnd IfEnd 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
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 Q1 = Question1RadioButton.SelectedValue Q2 = Question2RadioButton.SelectedValue Q3 = Question3RadioButton.SelectedValue Q4 = Question4RadioButton.SelectedValue Q5 = Question5RadioButton.SelectedValue Q6 = Question6RadioButton.SelectedValue Q7 = Question7RadioButton.SelectedValue Q8 = Question8RadioButton.SelectedValue Q9 = Question9RadioButton.SelectedValue Q10 = Question10RadioButton.SelectedValue Dim AddResult As Integer AddResult = (Q1 + Q2 + Q3 + Q4 + Q5 + Q6 + Q7 + Q8 + Q9 + Q10) Dim AvgResult As Double AvgResult = AddResult / 10 Label1.Text = AvgResultJeev
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;
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
If you get the answer to your question, please mark it as the answer.
ddelella
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 Responses.Add(i, MyControl.SelectedValue) 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 SubThe 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
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 Q(0) = Question1RadioButton.SelectedValue Q(1)= Question2RadioButton.SelectedValue Q(2) = Question3RadioButton.SelectedValue Q(3) = Question4RadioButton.SelectedValue Q(4) = Question5RadioButton.SelectedValue Q(5) = Question6RadioButton.SelectedValue Q(6) = Question7RadioButton.SelectedValue Q(7) = Question8RadioButton.SelectedValue Q(8) = Question9RadioButton.SelectedValue Q(9) = Question10RadioButton.SelectedValue 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 FunctionfindAll 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...
KK
Please mark as Answer if post helps in resolving your issue
My Site
mattcase
Member
374 Points
518 Posts
Re: Averaging Input Scores
Jun 09, 2011 05:19 PM|LINK
Thanks!