CSharp examples for Language Basics:Array
Check the value frequency in an array
using System;/*w ww . j a v a 2 s .c o m*/ class StudentPoll { static void Main() { int[] responses = {1, 2, 5, 4, 3, 5, 2, 1, 2, 5, 1, 2, 5, 4, 3, 5, 2, 1, 2, 5, 4, 3, 1, 2, 5, 4, 3, 5, 2, 5, 2,1, 2, 5, 4, 3, 5, 2, 4, 3, 5, 2,1, 2,1, 2, 5, 4, 3, 5, 2, 5, 4, 3, 5, 2,1, 3, 3, 1, 4, 3, 3, 3, 2, 3, 3, 2, 14}; var frequency = new int[6]; // array of frequency counters // for each answer, select responses element and use that value // as frequency index to determine element to increment for (var answer = 0; answer < responses.Length; ++answer) { try { ++frequency[responses[answer]]; } catch (IndexOutOfRangeException ex) { Console.WriteLine(ex.Message); Console.WriteLine($" responses[{answer}] = {responses[answer]}\n"); } } Console.WriteLine($"{"Rating"}{"Frequency",10}"); for (var rating = 1; rating < frequency.Length; ++rating){ Console.WriteLine($"{rating,6}{frequency[rating],10}"); } } }