-1

I wrote this piece of code that makes a simple 6x6 matrix (after taking input from the user) and I have to add a code that will scan the matrix and count for palindromes (if any) and will show their exact location (for example "First palindrome is at row 6 and column 2 \n Second palindrome is at row 2 column 4... etc ) but for that I have to use a "method" and that is where things gets weird for me as I have no experience in doing so. I really hope to get some help here. Thankyou so much.

Here's my matrix code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ArrayMatrix
{
    class Program
    {
        static void Main(string[] args)
        {
            int i, j;
            int[,] arr1 = new int[6, 6];

            Console.Write("Input elements in the matrix :\n");
            for (i = 0; i < 6; i++)
            {
                for (j = 0; j < 6; j++)
                {
                    Console.Write("element - [{0},{1}] : ", i, j);
                    arr1[i, j] = Convert.ToInt32(Console.ReadLine());
                }
            }

            Console.Write("\nThe matrix is : \n");
            for (i = 0; i < 6; i++)
            {
                Console.Write("\n");
                for (j = 0; j < 6; j++)
                    Console.Write("{0}\t", arr1[i, j]);
            }
            Console.Write("\n\n");
            Console.ReadKey();
        }
    }
}
stuartd
  • 70,509
  • 14
  • 132
  • 163
sarim77
  • 13
  • 5
  • _First palindrome is at row 6 and column 2 - Second palindrome is at row 2 column 4._ How are you defining a 'palindrome' within your matrix? Do you mean if the matrix entry is, for example, '12321'? What about '1' or '11', do they count? – stuartd Apr 05 '21 at 18:45
  • Like a simple Palindrome value, like the one that is the same if reversed. So 1 isn't, as 01 reversed is 10 while 11 is a palindrome as 11 reversed is still 11. – sarim77 Apr 05 '21 at 18:48
  • Yes, 12321 will count as a Palindrome – sarim77 Apr 05 '21 at 18:49
  • Sounds like you have enough information to write a `bool IsPalindrome(int value)` method? The [method docs](https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/methods) should help you get started. – stuartd Apr 05 '21 at 19:21
  • sadly enough I have seen this guide and even then I have not been able to find a way to implement that into my code :( – sarim77 Apr 05 '21 at 19:39

1 Answers1

1

So, for a number to be a palindrome by your rules:

  • It must be greater than 9
  • The sequence of digits must be same the same when reversed.

Here’s an example using Linq:

public static bool IsPalindrome(int value)
{
    if (value < 10)
    {
        return false;
    }

    var characters = value.ToString().ToCharArray();

    return characters.SequenceEqual(characters.Reverse());
}

Docs:

SequenceEqual

Reverse

ToCharArray

stuartd
  • 70,509
  • 14
  • 132
  • 163
  • Yes sir I get that, but how do I make it show the output from my matrix? I am very confused, – sarim77 Apr 05 '21 at 21:23
  • Like how do I make it search for Palindrome values from my matrix, and then show its location as well (in correspondence to rows and columns) – sarim77 Apr 05 '21 at 21:24
  • Well, for every element in the matrix - rows and columns - you call this method. That tells you which entries are palindromic, and from that you can construct your output. – stuartd Apr 05 '21 at 22:25
  • I did try that but it seems to get stuck at execution and says "System.IndexOutOfRangeException: 'Index was outside the bounds of the array." I don't seem to figure out how to fix that – sarim77 Apr 05 '21 at 23:41
  • See this question: [What is an IndexOutOfRangeException / ArgumentOutOfRangeException and how do I fix it?](https://stackoverflow.com/questions/20940979/what-is-an-indexoutofrangeexception-argumentoutofrangeexception-and-how-do-i-f) – stuartd Apr 05 '21 at 23:50