T
T
teilzteilzteilzteilz2021-12-23 21:51:07
C++ / C#
teilzteilzteilzteilz, 2021-12-23 21:51:07

When compiling a matrix along the left diagonal, it gives an error, how to fix it?

Good afternoon! Help with a problem. According to the task that I was given, the matrix should be displayed diagonally from left to right. I started debugging for bugs. When debugging, it gives an error in the section "Filling the matrix with variables" when converting m[i, j] = Convert.ToInt32(arr[j]);. How can I rewrite the code so that the matrix is ​​diagonal? Please help with this issue.

static void Main(string[] args)
        {
            // Ввод значений
            int i, n, j;
            n = Convert.ToInt32(Console.ReadLine());
            int[,] m = new int[n, n];
            string s;


            // Заполнение матрицы переменными
            for (i = 0; i < n; i++)
            {
                s = Console.ReadLine();
                string[] arr = s.Split();

                for (j = 0; j < n; j++)
                {
                    m[i, j] = Convert.ToInt32(arr[j]);
                }
            }

            // Конечный Вывод матрицы
            for (i = 0; i < n; i++)
            for (j = 0; j < n; j++)
            {
               if (i == j)
               {
                  Console.Write(m[i, i] + " ");
               }
            }
            Console.ReadKey();
        }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
N
Nik Faraday, 2022-01-18
@NikFaraday

What error does it give?
Why are you using a string for initialization?
Why are you splitting it on an empty value?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question