We are providing online training of realtime Live project on Asp.Net MVC with Angular and Web API. For more information click here. If you have any query then drop the messase in CONTACT FORM

Saturday, June 13, 2015

Adjacent matrix

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

            Console.WriteLine("Enter the rows of the matrix");
            int x = int.Parse(Console.ReadLine());
            Console.WriteLine("Enter the column of the matrix");
            int y = int.Parse(Console.ReadLine());
            int[,] arr = new int[x, y];

            Console.WriteLine("Enter the {0} element in the matrix respectively", x * y);
            for (int row = 0; row < x; row++)
            {

                for (int col = 0; col < y; col++)
                {

                    arr[row, col] = int.Parse(Console.ReadLine());
                }
            }

            Console.WriteLine("Enter the {0} element in the matrix respectively", x * y);
            for (int row = 0; row < x; row++)
            {

                for (int col = 0; col < y; col++)
                {

                    Console.Write(arr[row, col] + " ");
                }

                Console.WriteLine();
            }

            Console.WriteLine();

            Console.WriteLine("Enter the value");
            int val = int.Parse(Console.ReadLine());
            for (int row = 0; row < x; row++)
            {

                for (int col = 0; col < y; col++)
                {

                    if (arr[row, col] == val)
                    {

                        if (row == 0)
                        {

                            Console.WriteLine("No available top element");
                            Console.WriteLine("Bottom element = {0}", arr[row + 1, col]);
                        }
                        else if (row == x - 1)
                        {

                            Console.WriteLine("No available Bottom element");
                            Console.WriteLine("Top element = {0}", arr[row - 1, col]);

                        }
                        else
                        {

                            Console.WriteLine("Top element = {0}", arr[row - 1, col]);
                            Console.WriteLine("Bottom element = {0}", arr[row + 1, col]);
                        }
                        if (col == 0)
                        {

                            Console.WriteLine("No available Left element");
                            Console.WriteLine("Right element = {0}", arr[row, col + 1]);

                        }

                        else if (col == y - 1)
                        {

                            Console.WriteLine("No available Right element");
                            Console.WriteLine("Left element = {0}", arr[row, col - 1]);
                        }
                        else
                        {

                            Console.WriteLine("Right element = {0}", arr[row, col + 1]);
                            Console.WriteLine("Left element = {0}", arr[row, col - 1]);

                        }

                    }

                }
            }

            Console.ReadLine();
        }


    }

 
OUTPUT:

No comments: