Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Q&A

Welcome to Software Development on Codidact!

Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.

Find the name of the student with the top mark, display their name and mark

+4
−9

I have a CSV file like this:

name,surname,score
Moon,Walko,148
Jerald,Gryder,150

I need to find the highest score. My attempt:

namespace test
{ 
    class Program 
    { 
        string[] Data; 
        static void Main(string[] args) 
        { 
            ReadIn(); 
        }

        public static void ReadIn() 
        { 
            StreamReader Reader = new StreamReader("TestData.csv");
            string datafile = Reader.ReadToEnd(); 
            char[] Seperators = new char[] { ' ', ',', '.' }; 
            string[] Data = datafile.Split(Seperators); 
            for (int i = 0; i < Data.Length; i++ ) 
            {
                Console.WriteLine(Data[i]); 
            } 
            Console.Read(); 
        } 
    } 
} 
History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.
Why should this post be closed?

3 comment threads

This might have been better in Code Reviews. (1 comment)
Downvotes (1 comment)
Just some tip... (1 comment)

2 answers

You are accessing this answer with a direct link, so it's being shown above all other answers regardless of its score. You can return to the normal view.

+7
−0

That's nice that you're provided your attempts but, as it was already mentioned in comments, it would be better to show us all the parts. Again, if you're not sure how to write it properly in C# that's fine - just show us some pseudo-code and we help you from there.

Anyway, this is a simple task to find the maximum value among some sequence of data.

The most obvious algorithm would be:

  1. Pick some initial value (*) as a "local" maximum.
  2. Read a value.
  3. Compare a local maximum with the current value.
  4. Assign the biggest one from these two as a local maximum.
  5. Repeat step 2 - 4 until the whole sequence is over.

[*] You can take the first value in a sequence as an initial.

First step is done, now it's time to actually write some code.

Let's start with creating a class that represents data in the file. This isn't necessary step for this task but I still suggest you to use it since it might be useful in the future:

public class Student
{
    public Student(string name, string surname, uint score)
    {
        Name = name;
        Surname = surname;
        Score = score;
    }

    public string Name { get; }

    public string Surname { get; }

    public uint Score { get; }

    public override string ToString() =>
        $"{Name} {Surname} [{Score}]";
}

Now, when data is described, we can parse it and do something with it. Using LINQ is probably easiest way:

const string filepath = "TestData.csv";

var bestStudent =
    File.ReadLines(filepath) 
        .Skip(1) // skip header
        .Aggregate<string, Student>(null, (acc, line) =>
        {
            var fields = line.Split(',', StringSplitOptions.RemoveEmptyEntries);
            var student = new Student(fields[0], fields[1], uint.Parse(fields[2]));
            if (acc is null || acc.Score < student.Score)
            {
                return student;
            }

            return acc;
        });

if (bestStudent is null)
{
    Console.WriteLine("Input file is empty");
}
else
{
    Console.WriteLine("Student with a highest mark:");
    Console.WriteLine(bestStudent);
}

I didn't write code that validate input (which you should do) since it's unrelated to this task. As well as I used only , symbol as a separator to parse a line. I'd suggest considering using some library to parse .csv file.

History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

0 comment threads

+2
−0

Normally you should use a CSV library to read CSV, but assuming your data is simple, a Split should be adequate. Using the File.ReadLines() method allows you to read the file as an IEnumerable<string> which you can then process with LINQ:

var ans = File.ReadLines("TestData.csv")
              .Skip(1) ' skip header line
              .Select(line => line.Split(',')) ' break lines into fields
              .MaxBy(parts => Int32.Parse(parts[2])); ' find line with maximum score
History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

0 comment threads

Sign up to answer this question »