tags:

views:

276

answers:

5

Hi

I have a file with the following text inside

mimi,m,70
tata,f,60
bobo,m,100
soso,f,30

I did the reading from file thing and many many other methods and functions, but how I can get the best male name and his grade according to the grade.

here is the code I wrote. Hope it's not so long

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace practice_Ex
{
    class Program
    {
        public static int[] ReadFile(string FileName, out string[] Name, out char[] Gender)
        {
            Name = new string[1];
            int[] Mark = new int[1];
            Gender = new char[1];
            if (File.Exists(FileName))
            {
                FileStream Input = new FileStream(FileName, FileMode.Open, FileAccess.Read);
                StreamReader SR = new StreamReader(Input);
                string[] Current;
                int Counter = 0;
                string Str = SR.ReadLine();
                while (Str != null)
                {
                    Current = Str.Split(',');
                    Name[Counter] = Current[0];
                    Mark[Counter] = int.Parse(Current[2]);
                    Gender[Counter] = char.Parse(Current[1].ToUpper());
                    Counter++;
                    Array.Resize(ref Name, Counter + 1);
                    Array.Resize(ref Mark, Counter + 1);
                    Array.Resize(ref Gender, Counter + 1);
                    Str = SR.ReadLine();
                }
            }
            return Mark;
        }

        public static int MostFreq(int[] M, out int Frequency)
        {
            int Counter = 0;
            int Frequent = 0;
            Frequency = 0;
            for (int i = 0; i < M.Length; i++)
            {
                Counter = 0;
                for (int j = 0; j < M.Length; j++)
                    if (M[i] == M[j])
                        Counter++;
                if (Counter > Frequency)
                {
                    Frequency = Counter;
                    Frequent = M[i];
                }
            }
            return Frequent;
        }

        public static int Avg(int[] M)
        {
            int total = 0;
            for (int i = 0; i < M.Length; i++)
                total += M[i];
            return total / M.Length;
        }

        public static int AvgCond(char[] G, int[] M, char S)
        {
            int total = 0;
            int counter = 0;
            for (int i = 0; i < G.Length; i++)
                if (G[i] == S)
                {
                    total += M[i];
                    counter++;
                }
            return total / counter;
        }

        public static int BelowAvg(int[] M, out int AboveAvg)
        {
            int Bcounter = 0;
            AboveAvg = 0;
            for (int i = 0; i < M.Length; i++)
            {
                if (M[i] < Avg(M))
                    Bcounter++;
                else
                    AboveAvg++;
            }
            return Bcounter;
        }

        public static int CheckNames(string[] Name, char C)
        {
            C = char.Parse(C.ToString().ToLower());
            int counter = 0;
            string Str;
            for (int i = 0; i < Name.Length - 1; i++)
            {
                Str = Name[i].ToLower();
                if (Str[0] == C || Str[Str.Length - 1] == C)
                    counter++;
            }
            return counter;
        }

        public static void WriteFile(string FileName, string[] Output)
        {
            FileStream FS = new FileStream(FileName, FileMode.OpenOrCreate, FileAccess.Write);
            StreamWriter SW = new StreamWriter(FS);
            for (int i = 0; i < Output.Length; i++)
                SW.WriteLine(Output[i]);
        }

        static void Main(string[] args)
        {
            int[] Mark;
            char[] Gender;
            string[] Name;
            string[] Output = new string[8];
            int Frequent, Frequency, AvgAll, MaleAvg, FemaleAvg, BelowAverage, AboveAverage, NamesCheck;
            Mark = ReadFile("c:\\IUST1.txt", out Name, out Gender);
            Frequent = MostFreq(Mark, out Frequency);
            AvgAll = Avg(Mark);
            MaleAvg = AvgCond(Gender, Mark, 'M');
            FemaleAvg = AvgCond(Gender, Mark, 'F');
            BelowAverage = BelowAvg(Mark, out AboveAverage);
            NamesCheck = CheckNames(Name, 'T');
            Output [0]= "Frequent Mark = " + Frequent.ToString();
            Output [1]= "Frequency = " + Frequency.ToString();
            Output [2]= "Average Of All = " + AvgAll.ToString();
            Output [3]= "Average Of Males = " + MaleAvg.ToString();
            Output [4]= "Average Of Females = " + FemaleAvg.ToString();
            Output [5]= "Below Average = " + BelowAverage.ToString();
            Output [6]= "Above Average = " + AboveAverage.ToString();
            Output [7]= "Names With \"T\" = " + NamesCheck.ToString();
            WriteFile("c:\\Output.txt", Output);
        }
    }
}
A: 

I think the fastest and least-code way would be to transform the txt to xml and then use Linq2Xml to select from it. Here's a link.

Edit: That might be more work than you'd like to do. Another option is to create a class called AcademicRecord that has properties for the persons name gender etc. Then when you read the file, add to a List for each line in the file. Then use a Sort predicate to sort the list; the highest record would then be the first one in the list. Here's a link.

jcollum
thats not what i want
Added a solution using Generics, which will be a really handy thing for you to learn. And will cut down on your code significantly.
jcollum
+2  A: 

Well, I like LINQ (update: excluded via comments) for querying, especially if I can do it without buffering the data (so I can process a huge file efficiently). For example below (update: removed LINQ); note the use of iterator blocks (yield return) makes this fully "lazy" - only one record is held in memory at a time.

This also shows separation of concerns: one method deals with reading a file line by line; one method deals with parsing a line into a typed data record; one (or more) method(s) work with those data record(s).

using System;
using System.Collections.Generic;
using System.IO;

enum Gender { Male, Female, Unknown }
class Record
{
    public string Name { get; set; }
    public Gender Gender { get; set; }
    public int Score { get; set; }
}
static class Program
{
    static IEnumerable<string> ReadLines(string path)
    {
        using (StreamReader reader = File.OpenText(path))
        {
            string line;
            while ((line = reader.ReadLine()) != null)
            {
                yield return line;
            }
        }
    }
    static IEnumerable<Record> Parse(string path)
    {
        foreach (string line in ReadLines(path))
        {
            string[] segments = line.Split(',');
            Gender gender;
            switch(segments[1]) {
                case "m": gender = Gender.Male; break;
                case "f": gender = Gender.Female; break;
                default: gender = Gender.Unknown; break;
            }
            yield return new Record
            {
                Name = segments[0],
                Gender = gender,
                Score = int.Parse(segments[2])
            };
        }
    }
    static void Main()
    {
        Record best = null;
        foreach (Record record in Parse("data.txt"))
        {
            if (record.Gender != Gender.Male) continue;
            if (best == null || record.Score > best.Score)
            {
                best = record;
            }
        }

        Console.WriteLine("{0}: {1}", best.Name, best.Score);
    }
}

The advantage of writing things as iterators is that you can easily use either streaming or buffering - for example, you can do:

List<Record> data = new List<Record>(Parse("data.txt"));

and then manipulate data all day long (assuming it isn't too large) - useful for multiple aggregates, mutating data, etc.

Marc Gravell
im not in Linq yet i dont know how to use it so can u keep it simple and basic as the code i wrote thanx marc
OK, I'll remove the LINQ bits - about 2 lines ;-p
Marc Gravell
ben: I would *strongly* encourage you to use this problem as an entry point into LINQ. It's the kind of thing LINQ excels at. Sure, use a non-LINQ way for the moment, but come back soon with a LINQ book to hand, and weep for the beauty of it all :)
Jon Skeet
+1  A: 

This question asks how to find a maximal element by a certain criterion. Combine that with Marc's LINQ part and you're away.

Jon Skeet
A: 

Your assignment might have different requirements, but if you only want to get "best male name and grade" from a file you described, a compact way is:

public String FindRecord()
{
    String[] lines = File.ReadAllLines("MyFile.csv");
    Array.Sort(lines, CompareByBestMaleName);
    return lines[0];
}

int SortByBestMaleName(String a, String b)
{
    String[] ap = a.Split();            
    String[] bp = b.Split();
     // Always rank male higher
    if (ap[1] == "m" && bp[1] == "f") { return 1; }
    if (ap[1] == "f" && bp[1] == "m") { return -1; }
    // Compare by score
    return int.Parse(ap[2]).CompareTo(int.Parse(bp[2]));
}

Note that this is neither fast nor robust.

dbkk
+1  A: 

In the real world, of course, these would be records in a database, and you would use one line of SQL to select the best record, ie:

SELECT Name, Score FROM Grades WHERE Score = MAX(Score)

(This returns more than one record where there's more than one best record, of course.) This is an example of the power of using the right tool for the job.