Home > Mobile >  To check if matrix row is an palindrome (array in array)
To check if matrix row is an palindrome (array in array)

Time:09-16

The task is I need to check if the array in array (2d array) is palindromic The problem is I get an "Out of Range" exception at LINE 40 and can't figure out how to tell computer to do proper calculating there: if (arr[i, j] != arr[arr.GetLength(1) - i, j - 1])

I need to interpret if (arr[i] != arr[n - i - 1]) for 2d array. This is my code so far: ```

class Program
    {
        static void Main(string[] args)
        {

            Random rnd = new Random();
            int n;
            n = Convert.ToInt32(Console.ReadLine());
            int[,] arr = new int[n, n];


            
            for (int i = 0; i < arr.GetLength(0); i  )
            {
                for(int j = 0; j < arr.GetLength(1); j  )
                {
                    arr[i, j] = rnd.Next(1, 15);
                }
            }

            for (int i = 0; i < arr.GetLength(0); i  )
            {
                for (int j = 0; j < arr.GetLength(1); j  )
                {
                    Console.WriteLine(arr[i, j]);
                }
            }


            int flag = 0;
            for (int i = 0; i < arr.GetLength(0); i  )
            {

                for (int j = 0; j <= arr.GetLength(1) / 2 && n != 0; j  )
                {
                    if (arr[i, j] != arr[arr.GetLength(1) - i, j - 1])
                    {
                        Console.WriteLine(i);
                        flag = 1;
                        break;
                    }

                }
            }
            if (flag == 1)
            {
                Console.WriteLine("pali");
            }
            else
            {
                Console.WriteLine("not pali");
            }
        }
    }

CodePudding user response:

Are looking for something like this ?

namespace ConsoleApp
{
    class Program
    {
        static void Main(string[] args)
        {
            int[,] arr = new int[,]
            {
                { 1, 2, 3, 4, 5, 6 },
                { 1, 2, 3, 4, 5, 6 },
                { 1, 2, 3, 3, 2, 1 },      // Palindrome
                { 1, 2, 3, 4, 5, 6 },
                { 1, 2, 3, 4, 5, 6 }
            };
            PrintArray(arr);
            CheckForPalindrome(arr);
        }

        static void PrintArray(int[,] arr)
        {
            for (int i = 0; i < arr.GetLength(0); i  )
            {
                for (int j = 0; j < arr.GetLength(1); j  )
                {
                    Console.Write($"{arr[i, j]} ");
                }
                Console.WriteLine();
            }
        }

        static void CheckForPalindrome(int[,] arr)
        {
            for (int i = 0; i < arr.GetLength(0); i  )
            {
                var equals = true;
                for (int j = 0; j < arr.GetLength(1) / 2; j  )
                {
                    equals = arr[i, j] == arr[i, arr.GetLength(1) - j - 1];
                    if (equals == false)
                        break;
                }
                if (equals == true)
                    Console.WriteLine($"Row {i   1} is palindrome");
            }
        }
    }
}
  • Related