how to find average of each column of a datatable using c#
<p>I have a .csv file containing names, roll, subjects correspondingly.I parsed it into a datatable and I calculated the highest mark of each subject. All i want to calculate is the average of each Subject. Can anyone help me with this !!!!!</p>

<p>This was my output.</p>

<p>Highest mark for ComputerScience:</p>

<pre><code>    Name : Manoj
    Roll Number : 1212334556
    Mark : 94
</code></pre>

<p>Highest Mark for Biology:</p>

<pre><code>    Name : Sandeep
    Roll Number : 1223456477
    Mark : 90
</code></pre>

<p>Highest Mark for Commerce:</p>

<pre><code>    Name : BarathRam
    Roll Number : 1212345664
    Mark : 97
</code></pre>

<p>And csv file contains Names,Rollno, Computer, Biology, Commerce.</p>

<p>Now all i need to get is the average of each subject</p>

<p>My code:</p>

<pre><code>    static DataTable table;
    static void Main(string[] args)
    {
        StreamReader r = new StreamReader(@"C:\Users\GOPINATH\Desktop\stud1.csv");
        string line = r.ReadLine(); //reads first line - column header
        string[] part = line.Split(','); //splits the line by comma
        createDataTable(part);
        //copy from CSV to DataTable&lt;String,String,int,int,int&gt;
        while ((line = r.ReadLine()) != null)
        {
            try
            {
                part = line.Split(',');
                table.Rows.Add(part[0], part[1], Convert.ToInt32(part[2]), Convert.ToInt32(part[3]), Convert.ToInt32(part[4]));
            }
            catch(Exception e)
            {
                Console.WriteLine(e.Message);   
            }
        }
        r.Close();

        int mark1_index = 0, mark2_index = 0, mark3_index = 0; //initailize index value 0 for highest marks 

        //finding the index of the highest mark for each subject
        for(int i=0 ; i&lt;table.Rows.Count ; i++)
        {
            if (Convert.ToInt32(table.Rows[i][2]) &gt; Convert.ToInt32(table.Rows[mark1_index][2])) //subject1
            {
                mark1_index = i;
            }

            if (Convert.ToInt32(table.Rows[i][3]) &gt; Convert.ToInt32(table.Rows[mark2_index][3])) //subject2
            {
                mark2_index = i;
            }

            if (Convert.ToInt32(table.Rows[i][4]) &gt; Convert.ToInt32(table.Rows[mark3_index][4])) //subject3
            {
                mark3_index = i;
            }
        }

        printmark(table,mark1_index, 2);
        printmark(table,mark2_index, 3);
        printmark(table,mark3_index, 4);

        Console.Read();

    }
    public static void createDataTable(string[] columnName)
    {
        //create DataTable&lt;String,String,int,int,int&gt;
        table = new DataTable();
        table.Columns.Add(columnName[0], typeof(String));
        table.Columns.Add(columnName[1], typeof(String));
        table.Columns.Add(columnName[2], typeof(int));
        table.Columns.Add(columnName[3], typeof(int));
        table.Columns.Add(columnName[4], typeof(int));     
    }
    public static void printmark(DataTable t, int rowIndex, int columnIndex)
    {
        Console.WriteLine("Highest mark for " + t.Columns[columnIndex].ColumnName + ":");
        Console.WriteLine("\tName: " + (string)t.Rows[rowIndex][0]);
        Console.WriteLine("\tRole Number: " + (string)t.Rows[rowIndex][1]);
        Console.WriteLine("\tMark: " + (int)t.Rows[rowIndex][columnIndex]);

    }
}
</code></pre>

<p>}</p>