Processing a CSV download.
★★☆The results of three unit assessments for a course can be downloaded from an examining body website in CSV format. The data in grade book.txt is formatted as: forename, surname, unit 1 result, unit 2 result, unit 3 result. This program reads the data and outputs the results from a chosen unit.
Raghu,Aleksandra,78,63,0
Michelle,Chase,34,56,0
Max,Clayton,89,88,0
Selene,Dipti,79,82,0
Damon,Smith,56,63,0
Helen,Sorina,90,90,0
Briana,Takehiko,76,87,0
Write a program that asks the user which unit to output the results for. The program then outputs the name of each student and the result they achieved in that unit.
Remember to add a comment before a subprogram, selection or iteration statement to explain its purpose.
read_grade_book that:grade_book List<string[]> declared in the main program.output_assignment that:List<string[]> grade_book is declared at the class level.read_grade_book is called.output_assignment is called.Which unit do you want to output the results for? :
1
Raghu Aleksandra: 78
Michelle Chase: 34
Max Clayton: 89
Selene Dipti: 79
Damon Smith: 56
Helen Sorina: 90
Briana Takehiko: 76
Which unit do you want to output the results for? :
2
Raghu Aleksandra: 63
Michelle Chase: 56
Max Clayton: 88
Selene Dipti: 82
Damon Smith: 63
Helen Sorina: 90
Briana Takehiko: 87
// Grade book program
using System;
using System.IO;
using System.Collections.Generic;
class Submission
{
---
// -------------------------
// Globals
// -------------------------
---
static List<string[]> grade_book = new List<string[]>();
---
// -------------------------
// Subprograms
// -------------------------
// Read the CSV data from the file
---
static void read_grade_book(string filename)
{
---
StreamReader file = new StreamReader(filename);
// Read each line of data until end of file
string record;
string[] fields;
---
while ((record = file.ReadLine()) != null)
{
---
fields = record.Split(',');
grade_book.Add(fields);
---
}
---
file.Close();
---
}
---
// Output the results from a single field for each student
static void output_assignment()
{
---
Console.WriteLine("Which unit do you want to output the results for? :");
int unit = Convert.ToInt32(Console.ReadLine());
---
foreach (string[] student in grade_book)
{
---
string output = student[0] + " " + student[1] + ": " + student[unit + 1];
Console.WriteLine(output);
---
}
---
}
---
// -------------------------
// Main program
// -------------------------
public static void Main(string[] args)
{
---
read_grade_book("grade book.txt");
output_assignment();
---
}
---
}