vb.net counting an element of an array that has 4 possibilities -


i have array of 18 questions. each question has 4 possibilities of answer ranging 1 4. i'd count how many people answered question1 , on. codes below. have better way it? can't list 18 questions that.

dim question(18) string  dim ans1arr() string = {0, 0, 0, 0} dim ans2arr() string = {0, 0, 0, 0}      if ques(0) = 1         ans1arr(0) = ans1arr(0) + 1     elseif ques(0) = 2         ans1arr(1) = ans1arr(1) + 1     elseif ques(0) = 3         ans1arr(2) = ans1arr(2) + 1     elseif ques(0) = 4         ans1arr(3) = ans1arr(3) + 1     end if      if ques(1) = 1         ans2arr(0) = ans2arr(0) + 1     elseif ques(1) = 2         ans2arr(1) = ans2arr(1) + 1     elseif ques(1) = 3         ans2arr(2) = ans2arr(2) + 1     elseif ques(1) = 4         ans2arr(3) = ans1arr(3) + 1     end if  

you can use lookup(of tkey, telement) similar dictionary apart fact returns empty sequence if key not available:

dim lookup = question.tolookup(function(qnum) qnum) dim countanswerone int32 = lookup(1).count() dim countanswertwo int32 = lookup(2).count() ' ... ' 

can tested with:

dim question(18) int32 dim rnd new random() int32 = 0 17     question(i) = rnd.next(1, 5) next dim lookup = question.tolookup(function(qnum) qnum)  console.write("one: {0} two: {1} three: {2} four: {3}",      lookup(1).count(), lookup(2).count(), lookup(3).count(), lookup(4).count()) 

which outputs f.e: one: 5 two: 6 three: 5 four: 2


Comments

Popular posts from this blog

php - Submit Form Data without Reloading page -

linux - Rails running on virtual machine in Windows -

php - $params->set Array between square bracket -