Advertisement
grubcho

Average student grades - Nested dictionariues

Jul 12th, 2017
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.36 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace Average_student_grades
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             Dictionary<string, List<double>> students = new Dictionary<string, List<double>>();
  14.             int cnt = int.Parse(Console.ReadLine());
  15.  
  16.             for (int i = 0; i < cnt; i++)
  17.             {
  18.                 string[] input = Console.ReadLine().Split(' ').ToArray();
  19.                 string name = input[0];
  20.                 double grade = double.Parse(input[1]);
  21.  
  22.                 if (!students.ContainsKey(name))
  23.                 {
  24.                     students.Add(name, new List<double>());
  25.                 }
  26.                 students[name].Add(grade);                            
  27.             }
  28.             foreach (KeyValuePair<string, List<double>> student in students)
  29.             {
  30.                 string printName = student.Key;
  31.                 List<double> grades = student.Value;
  32.                 double avg = grades.Average();
  33.                 Console.Write($"{printName} -> ");
  34.  
  35.                 foreach (double grade in grades)
  36.                 {
  37.                     Console.Write($"{grade:f2} ");
  38.                 }
  39.                 Console.WriteLine($"(avg: {avg:f2})");
  40.             }
  41.         }
  42.     }
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement