what are some ideas you can give me to code this in C#: Write a program called Scores.cs…
- User
what are some ideas you can give me to code this in C#: Write a program called Scores.cs that will ask the user for the name of this file and will calculate the average of each student. Store the averages in a dictionary, where the key is the student's name and the value is the average. Store the list of scores for each student in another dic tionary. When the user types a particular student's name, report the student's scores and their average to the screen. If the user enters a name that is not in the dictionary, indicate that that student is not in the class. If the user enters "q", quit the program.
- Assistant
Here are some ideas for how to implement this program in C#:
- Use the Console.ReadLine() method to prompt the user for the name of the file where the scores are stored.
- Use the System.IO.File class to read in each line from the file and process the scores for each student.
- As you read in each line, split it into an array of strings using the string.Split() method. The first element of the array will be the student's name, and the remaining elements will be their scores.
- Use a loop to calculate the average for each student. You can do this by converting the score strings to integers and then using the System.Linq.Enumerable.Average() method to calculate the mean.
- Store the averages and scores in a dictionary. You can create a new dictionary for each student, where the key is their name and the value is a List of their scores.
- Use a loop to prompt the user for a student's name. If the name is in the dictionary, display the student's scores and average. If the name is not in the dictionary, display a message indicating that the student is not in the class. If the user enters "q", exit the program using the break keyword.
- User
if this is the file how do I store 4 scores for each student? Mary 76 82 91 94 Joe 75 88 64 90 Ed 88 82 87 81 Kim 91 88 65 68
- Assistant
You can store the scores for each student in a dictionary where the key is the student's name and the value is a List of integers representing their scores. Here is an example implementation:
- User
why does my code not properly show the scores try { Write("Enter the path to the file: "); string fileName = ReadLine(); string line; string[] parts; string[] separators = {" "," ","\t"}; string studentName; double average; List scores = new List();
Dictionary<string, List<int>> dictScores = new Dictionary<string, List<int>>(); Dictionary<string, double> averages = new Dictionary<string, double>(); using (StreamReader sr = new StreamReader(fileName)) { while (!sr.EndOfStream) { line = sr.ReadLine(); if (line.Length > 0) { parts = line.Split(separators, StringSplitOptions.RemoveEmptyEntries); studentName = parts[0]; List<int> studentScores = new List<int>(); for (int i = 1; i < parts.Length; i++) { studentScores.Add(Convert.ToInt32(parts[i])); } dictScores[studentName] = studentScores; } } sr.Close(); Write("Enter the name of the student to look up or q to quit: "); studentName = ReadLine(); while (!studentName.Equals("q")) { if (dictScores.ContainsKey(studentName)) { List<int> studentScores = dictScores[studentName]; WriteLine("{0} scores were {1}", studentName, studentScores); } - Assistant