Tuple<string, Nullable<int>>[] scores = { new Tuple<string, Nullable<int>>("Jack", 78), new Tuple<string, Nullable<int>>(“Jill", 10), new Tuple<string, Nullable<int>>(“Mack", 48), new Tuple<string, Nullable<int>>("Sam", 91), new Tuple<string, Nullable<int>>(“Jam", null), new Tuple<string, Nullable<int>>(“Tam", 100), new Tuple<string, Nullable<int>>(“Rita", 89), new Tuple<string, Nullable<int>>(“Mary", null) }; private static double AverageScore(Tuple<string, Nullable<int>>[] scores, out int n) { n = 0; int sum = 0; foreach (var score in scores) { if (score.Item2.HasValue) { n += 1; sum += score.Item2.Value; } } if (n > 0) return sum / (double)n; else return 0; } int number; double mean = AverageScore(scores, out number); Console.WriteLine("Average test score: {0:N2} (n={1})", mean, number);
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter