Average scores

PHOTO EMBED

Sat Oct 16 2021 16:23:50 GMT+0000 (UTC)

Saved by @funcProgramming

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);
content_copyCOPY