Question

I found this question in an old question in your website so i thought i can do it but I think I was mistaken :-)

the old post was here

I'm new at the jagged array thing so did the following code

StreamReader rows = new StreamReader("c:\\practice.txt");
            string line;
            int i;
            i=1;
            while ((line = rows.ReadLine()) != null)
            {
                String[][]rows = new String [i][]; ;
                rows = rows.ReadLine();
                String[][] rows = new string[S.Length][];
                i++;
            }
            for (int i; i < S.Length; i++)
            {

                row[i] = S[I].Split(',');

            }

            int totalCounter = 0, totalSum = 0;
            // etc
            foreach(string[] row in rows)
            {    
                int m1 = int.Parse(row[3]);
                totalCounter++;
                totalSum += m1;
                switch(row[2])
                {        
                    case "male":
                    maleCount++;            
                    maleSum += m1;            
                        break;        
                    case "female":            
                        femaleCount++;            
                        femaleSum += m1;            
                        break;    
                }
            }

I know i did major mistakes but at east i tried can any one help me to make it a workin code

Was it helpful?

Solution

You seem to be double-reading the lines, or maybe you've mixed up rows and cells - this bit in particular looks really odd:

        while ((line = rows.ReadLine()) != null)
        {
            String[][]rows = new String [i][]; ;
            rows = rows.ReadLine();
            String[][] rows = new string[S.Length][];
            i++;
        }

i.e. re-declaring rows, two calls to ReadLine per loop, etc. I suspect you mean string.Split? Either way, either use File.ReadAllLines, or look at some of the options presented yesterday. If you are desperate to use arrays, the core might look something like:

using System;
using System.IO;
static class Program
{
    static void Main()
    {
        string[] lines = File.ReadAllLines("foo.txt");
        string[][] grid = new string[lines.Length][];
        for (int i = 0; i < lines.Length; i++)
        {
            grid[i] = lines[i].Split(',');
        }

        int totalCount = 0, maleCount = 0, femaleCount = 0,
            m1Total = 0, m2Total = 0, m3Total = 0,
            m1MaleTotal = 0, m1FemaleTotal = 0;
        foreach (string[] line in grid)
        {
            totalCount++;
            int m1 = int.Parse(line[3]),
                m2 = int.Parse(line[4]),
                m3 = int.Parse(line[5]);
            m1Total += m1;
            m2Total += m2;
            m3Total += m3;
            switch (line[1].Trim())
            {
                case "male":
                    maleCount++;
                    m1MaleTotal += m1;
                    break;
                case "female":
                    femaleCount++;
                    m1FemaleTotal += m1;
                    break;
            }
        }
        Console.WriteLine("Rows: " + totalCount);
        Console.WriteLine("Total m1: " + m1Total);
        Console.WriteLine("Average m1: " + ((double)m1Total)/totalCount);
        Console.WriteLine("Male Average m1: " + ((double)m1MaleTotal) / maleCount);
        Console.WriteLine("Female Average m1: " + ((double)m1FemaleTotal) / femaleCount);
    }
}

Again - I cannot stress enough how much you should be doing this with LINQ instead of manual looping...

OTHER TIPS

Firstly, make sure you wrap unmanaged resources such as streams in using statements.

Personally I like my LineReader class which makes it easy to read lines of text from a file (or anything else, actually).

Next, I'd avoid using arrays unless you really have to. List<T> is generally a lot more pleasant to work with. Now if string.Split does what you want, you can easily have a List<String[]>. Alternatively, you can probably do a lot of the work using LINQ:

var query = from line in new LineReader("c:\\practice.txt")
            let parts = line.Split(',')
            select new { Gender=parts[2], Amount=int.Parse(parts[3]) };

Taking multiple aggregates from a single data stream is tricky in "normal" LINQ (which is why Marc Gravell and I developed Push LINQ). However, you can use a normal foreach statement:

int totalCounter = 0, totalSum = 0;
int maleCount = 0, maleSum = 0, femaleCount = 0, femaleSum = 0;
foreach (var row in query)
{
    totalCounter++;
    totalSum += row.Amount;
    switch (row.Gender)
    {
        case "male":
            maleCount++;
            maleSum += Amount;
            break;
        case "female":
            femaleCount++;
            femaleSum += Amount;
            break;
    }
}

If you grouped the rows by gender you might be able to make this even simpler, particularly if you know that the gender is always "male" or "female".

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top