Console.WriteLine("Enter n:"); int n=Convert.ToInt32(Console.ReadLine()); Console.WriteLine("Enter m:"); int m = Convert.ToInt32(Console.ReadLine()); int [,]arr=new int [n, m]; Random ran = new Random(); int sum = 0; float middle = 0; for (int i=0; i < n; i++) { for(int j=0; j<m; j++) { arr[i, j] = ran.Next(0, 100); sum += arr[i, j]; middle = sum /( m*n); Console.Write(arr[i, j] + "\t"); } Console.WriteLine(); } Console.WriteLine("middle=" + middle); Console.ReadLine(); 

In the two-dimensional array, find the arithmetic average of the first column and the number of elements in each of the following columns exceeding the arithmetic average of the preliminary column. I could find only the arithmetic average of the whole array

  • What exactly do you fail? - Ares
  • Calculate the arithmetic average of individual columns, rather than the whole array - Helpless
  • array [i] [column], where i is a counter, column is the column number. - Wootiae
  • @Helpless, added in response to what you can’t do - Ares
  • one
    @Ares, thank you) - Helpless

2 answers 2

I can write in C ++.

 for(int i = 0;i < n;++i){ int sum = 0; for(int j = 0;j < m;++j){ sum = sum + array[j][i]; } int avr = sum / n; cout << avr << endl; } 

Like the person asked to translate C # although I’m not in it

 Console.WriteLine("Enter n:"); int n = Convert.ToInt32(Console.ReadLine()); Console.WriteLine("Enter m:"); int m = Convert.ToInt32(Console.ReadLine()); int [,]arr=new int [n, m]; int []sum=new int [m]; Random ran = new Random(); for (int i=0; i<n; i++) { for(int j=0; j<m; j++) { arr[i, j] = ran.Next(0, 100); Console.Write(arr[i, j] + "\t"); } Console.WriteLine(); } for (int i=0; i<m; i++) { for(int j=0; j<n; j++) { sum[i] += arr[j, i]; } } for (int i=0; i<m; i++) { Console.Write(float(sum[i] / n) + " "); } Console.ReadLine(); 

We fix the column and pass through all the rows of columns. Finding the sum of each column can be found and the arithmetic mean

  • The question says "Sharpe C". To answer the question, and not to any other. - AK

Find the arithmetic average of the columns:

We write the extension-method:

 public static class ArrExt { public static double GetAverageForColumn(this int[,] arr, int i) => Enumerable.Range(0, arr.GetLength(0)).Select(j => arr[j, i]).Average(); } 

We use (column numbering starts from 0):

 var array = new int[,] { { 1, 2, 3 }, { 1, 12, 5 }, { 1, 5, 3 } }; var average = array.GetAverageForColumn(1); Console.WriteLine(average);