alexbancheva

Average_Student_Grades_Lab_Sets_and_Dictionaries

Mar 21st, 2021
941
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.18 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace Average_Student_Grades_Lab_Sets_and_Dictionaries
  6. {
  7.     class Program
  8.     {
  9.         static void Main(string[] args)
  10.         {
  11.             int countStudents = int.Parse(Console.ReadLine());
  12.  
  13.             Dictionary<string, List<decimal>> grades = new Dictionary<string, List<decimal>>();
  14.  
  15.             for (int i = 0; i < countStudents; i++)
  16.             {
  17.                 string[] inputInfo = Console.ReadLine()
  18.                     .Split(" ");
  19.                 string name = inputInfo[0];
  20.                 decimal grade = decimal.Parse(inputInfo[1]);
  21.  
  22.                 if (!grades.ContainsKey(name))
  23.                 {
  24.                     grades[name] = new List<decimal>();
  25.                 }
  26.                 grades[name].Add(grade);
  27.             }
  28.  
  29.             foreach (var (keys, values) in grades)
  30.             {
  31.                 Console.Write($"{keys} -> ");
  32.  
  33.                 foreach (var value in values)
  34.                 {
  35.                     Console.Write($"{value:f2} ");
  36.                 }
  37.                 Console.WriteLine($"(avg: {values.Average():f2})");
  38.             }
  39.         }
  40.     }
  41. }
  42.  
Advertisement
Add Comment
Please, Sign In to add comment