Good day. Help please solve the problem:

Given an integer rectangular matrix A of size nxm. Find the first row in the matrix, all elements of which are zero. Multiply all elements of the column with the same number by 2.

Tell me a method for solving this problem.

class Program { static void Main(string[] args) { int[,] array2D = new int[,] { { 1, 0, 4, 6, 2 }, // Создаем { 3, 4, 1, 7, 4 }, // Массив { 0, 0, 0, 0, 0 }, // размером { 7, 8, 2, 4, 1 } }; // nxm Console.WriteLine("Исходный массив:"); for (int i = 0; i < array2D.GetLength(0); i++ ) { for (int j = 0; j < array2D.GetLength(1); j++) { if (array2D[i, j] == 0) Console.Write(" " + array2D[i, j]); } Console.WriteLine(" "); } Console.ReadKey(); } } 

In this matrix in row number 3, all elements are equal to 0, it must be found, and column 3 multiplied by 2. How to do this?

    1 answer 1

     int[,] array2D = new int[,] { { 1, 0, 4, 6, 2 }, // Создаем { 3, 4, 1, 7, 4 }, // Массив { 0, 0, 0, 0, 0 }, // размером { 7, 8, 2, 4, 1 } }; // nxm int result=-1;; List<int> tmp = new List<int>(); for(int i=0; i<4; i++) { for(int j=0; j<5; j++) tmp.Add(array2D[i,j]); if(tmp.All(num=>num==0))// альтернатива: !tmp.Any(num=>num!=0) { result = i; break; } tmp.Clear(); } if(result!=-1)// проверка на случай, если необходимой строки нету for(int i=0; i<4; i++) array2D[i,result]*=2; 
    • Thank! Is it possible to solve this problem by any method? - amYrBY
    • You can not create an auxiliary tmp list, but simply use a boolean (bool) variable to determine if the string is null. - alexlz
    • Can you give an example? Thanks in advance) - amYrBY
    • one
      Well, for example: for (int i = 0; i <n; i ++) {bool allZeroes = true; for (int j = 0; j <m; j ++) allZeroes & = array2D [i, j] == 0; // Since multiplication does not affect zeros, then ... if (allZeroes && i <m) // all zeros and the column exists for (int i1 = 0; i1 <array2D.GetLength (0); i1 ++) array2D [i1, i] * = 2; } - alexlz
    • and I like it, especially with the &= operator - Specter