Hello! Please help with the task:

Given an integer rectangular matrix. Define:

  1. The product of elements located above the main and secondary diagonal.
  2. The number of the first of the lines that do not contain any positive elements.

I know that the 1st is in (i < j) && (i + j < n + 1)
But I don’t know how to write all this correctly and how to show the matrix in the form.
There is also such code for creating a matrix:

 int[,] a = new int[8, 8]; Random randomizer = new Random(); for (int i = 0; i < 8; i++) { for (int j = 0; j < 8; j++) { a[i, j] = randomizer.Next(-9, 9); } } 

    2 answers 2

    You can solve the problem this way:

     ///////// int number; for (int i = 0; i < a.GetLength(0); i++) { var isHavePositive = false; for (int j = 0; j < a.GetLength(1); j++) { if (a[i, j] > 0) { isHavePositive = true; } } if (!isHavePositive) { number = i; break; } } ////////////// int result = 1; int skiper = 1; int ii = 0; while (a.GetLength(1) > skiper * 2) { for (int j = 0; j < a.GetLength(1) - 2 * skiper; j++) { result *= a[ii, j]; } ii++; skiper++; } 

    After this number will be equal to the line number without positive elements, and result to the product of elements on the diagonal

    • Even he has something wrong with me ... - loving_evil
    • there are subqueries if (a[i, j] >= 0) and just below number = i + 1; - loving_evil
    • thanks for the corrections :) - Vladimir Paliukhovich
    • I haven’t figured out the work yet) thinks wrong - loving_evil

    Working option:

     textBox1.Clear(); int n = 6; //размерность int[,] a = new int[n, n]; // создание массива Random randomizer = new Random(); for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { a[i, j] = randomizer.Next(-5, 5); if (a[i, j] < 0) { textBox1.Text += (" " + a[i, j]); } else textBox1.Text += (" " + a[i, j]); } textBox1.Text += (Environment.NewLine); } int number = 0; for (int i = 0; i < a.GetLength(0); i++) { var isHavePositive = false; for (int j = 0; j < a.GetLength(1); j++) { if (a[i, j] >= 0) { isHavePositive = true; } } if (!isHavePositive) { number = i + 1; break; } } textBox2.Text = ("Номер строки в которой все элементы отрицательные : " + number) + (Environment.NewLine); int x = 1; for (int i = 0; i < a.GetLength(0); i++) { for (int j = 0; j < a.GetLength(1); j++) { if (i < j && i + j < n - 1) x *= a[i, j]; } } textBox2.Text += ("Произведение элементов над диагоналями: " + x);