Click here to Skip to main content
15,894,343 members
Please Sign up or sign in to vote.
2.33/5 (3 votes)
See more:
C#
double avg = 0;

double[] A = { 10, 20, 30, 40, 50, 60 };
double[] B = { 12, 70, 80, 40, 90, 34 };
double[] C = { 10, 20, 30, 40, 50, 60 };
double[] D = { 98, 20, 30, 40, 20, 60 };
double[] E = { 50, 20, 45, 56, 50, 87 };
double[] F = { 87, 20, 34, 40, 76, 60 };

for (int i = 0; i < 6; i++)
    avg = avg + A[i];
avg = avg / 6;
return avg;



This code will only give me the result for A. How to get the result of each one at a time?
Posted
Updated 19-Mar-12 7:38am
v3
Comments
ProEnggSoft 19-Mar-12 13:37pm    
Edit: pre tag for C# code added - PES

using linq it is more straight forward and no need for helper (named) variables

C#
double[] A = { 10, 20, 30, 40, 50, 60 };
double[] B = { 12, 70, 80, 40, 90, 34 };
double[] C = { 10, 20, 30, 40, 50, 60 };
double[] D = { 98, 20, 30, 40, 20, 60 };
double[] E = { 50, 20, 45, 56, 50, 87 };
double[] F = { 87, 20, 34, 40, 76, 60 };


IEnumerable<double> result = from arr in 
                                   new double [][] {A, B, C, D, E, F}
                             select arr.Average();
 
Share this answer
 
v2
Comments
ProEnggSoft 20-Mar-12 12:53pm    
Good solution. 5!
james_berry 21-Mar-12 2:01am    
Nice Answer, Even shorter

IEnumerable<double> result = new double[][] { A, B, C, D, E, F }.Select(arr => arr.Average()).ToList();
C#
double avg = 0;

            double[] A = { 10, 20, 30, 40, 50, 60 };
            double[] B = { 12, 70, 80, 40, 90, 34 };
            double[] C = { 10, 20, 30, 40, 50, 60 };
            double[] D = { 98, 20, 30, 40, 20, 60 };
            double[] E = { 50, 20, 45, 56, 50, 87 };
            double[] F = { 87, 20, 34, 40, 76, 60 };
            double[][] range = {A, B, C, D, E, F};
            double[] results = new double[6];
            {
                for (int j = 0; j < range.Length; j++)
                {
                    for (int i = 0; i < 6; i++)
                        avg = avg + range[j][i];
                    avg = avg / 6;
                    results[j] = avg;
                    
                }
              return results;
            }
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 19-Mar-12 14:08pm    
Exactly. A 5.
--SA
ProEnggSoft 19-Mar-12 14:25pm    
Good solution. +5

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900