views:

469

answers:

3

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

+2  A: 

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".

Jon Skeet
great but can still do it by arrays
I got the PushLINQ example in yesterday ;-p
Marc Gravell
brandon: If you want an array, just call ToArray()
Jon Skeet
+1  A: 

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...

Marc Gravell
yesterday options are so advance I can't do it , but can you show me how to fix this
but what if i want to count the m2 and m3
thanx Marc you are the Best of the Best
A: 

I did Marc but what if i want to count the m2 and m3 how then i can do it ?

static void Main(string[] args)
        {
            string[] lines = File.ReadAllLines("C://iust.txt");
            string[][] rows = new string[lines.Length][];
            for (int i = 0; i < lines.Length; i++)
            {
                rows[i] = lines[i].Split(',');
            }

            int totalCounter = 0; 
            int totalSum = 0;
            int maleCount=0;
            int femaleCount=0;
            int maleSum=0;
            int femaleSum = 0;

            // etc
            foreach (string[] row in rows)
            {
                int m1 = int.Parse(row[3]);


                totalCounter++;
                totalSum += m1;
                switch (row[1])
                {
                    case "male":
                        maleCount++;
                        maleSum += m1;
                        break;
                    case "female":
                        femaleCount++;
                        femaleSum += m1;
                        break;
                }
            }


            Console.WriteLine("Type your request number:\n1-Get The Average for all M1\n2-Get The Average for Males M1\n3-Get The Average for Females M1");
            int order = Convert.ToInt32(Console.ReadLine());
            switch (order)
            {
                case 1:
            float avgm1;
            avgm1 = (float)totalSum / totalCounter;
            Console.WriteLine(avgm1);
            break;

                case 2:
                    float avgmm1;
                    avgmm1 = (float)maleSum/maleCount;
                    Console.WriteLine(avgmm1);
                    break;

                case 3:
                    float avgfm1;
                    avgfm1 = (float)femaleSum / femaleCount;
                    Console.WriteLine(avgfm1);
                    break;
            }



        }
    }