В каждом столбце найти среднее арифметическое отрицательных элементов - C#
Формулировка задачи:
В каждом столбце найти среднее арифметическое отрицательных элементов .
Решение задачи: «В каждом столбце найти среднее арифметическое отрицательных элементов»
textual
Листинг программы
int[,] Matrix = new int[7, 7];
Random gen = new Random();
string resultat="";
int count=0;
double sum = 0;
for (int i = 0; i < Matrix.GetLength(0); i++)
for (int j = 0; j < Matrix.GetLength(1); j++)
Matrix[i, j] = gen.Next(-30,30);
// Matrix=new int[7,7]{{-1,1,1,1,1,1,1},{-40,1,1,1,1,1,1},{1,1,1,1,1,1,1},{1,1,1,1,1,1,1},{1,1,1,1,1,1,1},{1,1,1,1,1,1,1},{1,1,1,1,1,1,1}};
for (int i = 0; i < Matrix.GetLength(0); i++)
{
resultat += "\n";
sum=0;
count=0;
for (int j = 0; j < Matrix.GetLength(1); j++)
{
resultat += Matrix[i, j] + "\t";
}
}
Console.WriteLine(resultat);
resultat="";
bool first = true;
for (int i = 0; i < Matrix.GetLength(1); i++)
{
if (!first)
resultat += "\t";
sum=0;
count=0;
first = false;
for (int j = 0; j < Matrix.GetLength(0); j++)
if (Matrix[j, i] < 0)
{
sum += Matrix[j, i];
count++;
}
if (count != 0)
resultat += Math.Round((sum / count),2);
if (count == 0)
resultat += "-";
}
Console.WriteLine("<br>");
Console.WriteLine(resultat);