Skip to main content
adicionou 229 caracteres ao conteúdo
Fonte Link

Para que o programa respeite o exemplo mostrado, além de fazer a sanitização das entradas do usuário, pode-se fazer o programa da seguinte forma:

using System;
using System.Collections.Generic;
using System.Linq;

namespace ConsoleApp
{
    public class Program
    {
        private static class Vector
        {
            public static bool TryParse(string input, int columns, out IEnumerable<int> decodedValues)
            {
                var list = new List<int>();
                bool result = false;

                string[] sanity = input.Split(' ', StringSplitOptions.RemoveEmptyEntries);

                if (sanity.Length != columns)
                {
                    result = false;
                }
                else if (sanity.All(entry => int.TryParse(entry, out int dummy)))
                {
                    result = true;
                    list.AddRange(sanity.Select(entry => int.Parse(entry)));
                }

                decodedValues = list;
                return result;
            }
        }

        public static void Main(string[] args)
        {
            var (lines, columns) = GetLinesAndColumns();
            var matrix = GetMatrix(lines, columns).ToArray();
            var selectedValue = GetSelectedValue();

            DisplayNorthSouthEastWest(selectedValue, matrix, lines, columns);
        }

        public static (int, int) GetLinesAndColumns()
        {
            string input = null;
            int lines;
            int columns;

            do
            {
                Console.Write("Número de linhas: ");
                input = Console.ReadLine();
            } while (!int.TryParse(input, out lines));

            do
            {
                Console.Write("Número de colunas: ");
                input = Console.ReadLine();
            } while (!int.TryParse(input, out columns));

            Console.WriteLine();

            return (lines, columns);
        }

        public static IEnumerable<int> GetMatrix(int lines, int columns)
        {
            string input = null;
            IEnumerable<int> result = null;
            for (int i = 0; i < lines; ++i)
            { 
                do
                {
                    Console.Write($"    {i+1}: ");
                    input = Console.ReadLine();
                } while (!Vector.TryParse(input, columns, out result));

                foreach (var item in result)
                {
                    yield return item;
                }
            }

            Console.WriteLine();
        }

        public static int GetSelectedValue()
        {
            string input = null;
            int selectedValue;
            do
            {
                Console.Write("Número que deseja verificar: ");
                input = Console.ReadLine();
            } while (!int.TryParse(input, out selectedValue));

            Console.WriteLine();

            return selectedValue;
        }

        public static void DisplayNorthSouthEastWest(int selectedValue, int[] matrix, int lines, int columns)
        {
            var vector = matrix.ToArray();

            int selectedIndex = Array.FindIndex(vector, item => item == selectedValue);

            if (selectedIndex < 0)
            {
                Console.WriteLine($"Número que deseja verificar, {selectedValue}, não está na matriz.");
            }
            else
            { 
                int temp;
                int north = selectedIndex - columns;
                int south = selectedIndex + columns;
                int west  = (temp = selectedIndex) % columns == 0       ? -1 : temp - 1;
                int east  = (temp = (selectedIndex + 1)) % columns == 0 ? -1 : temp;

                string northString = north < 0 ?              "Não há" : vector[north].ToString();
                string southString = south >= vector.Length ? "Não há" : vector[south].ToString();
                string westString  = west < 0 ?               "Não há" : vector[west].ToString();
                string eastString  = east < 0 ?               "Não há" : vector[east].ToString();

                Console.WriteLine($"Esquerda : {westString}");
                Console.WriteLine($"Direita  : {eastString}");
                Console.WriteLine($"Acima    : {northString}");
                Console.WriteLine($"Abaixo   : {southString}");
            }
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;

namespace ConsoleApp
{
    public class Program
    {
        private static class Vector
        {
            public static bool TryParse(string input, int columns, out IEnumerable<int> decodedValues)
            {
                var list = new List<int>();
                bool result = false;

                string[] sanity = input.Split(' ', StringSplitOptions.RemoveEmptyEntries);

                if (sanity.Length != columns)
                {
                    result = false;
                }
                else if (sanity.All(entry => int.TryParse(entry, out int dummy)))
                {
                    result = true;
                    list.AddRange(sanity.Select(entry => int.Parse(entry)));
                }

                decodedValues = list;
                return result;
            }
        }

        public static void Main(string[] args)
        {
            var (lines, columns) = GetLinesAndColumns();
            var matrix = GetMatrix(lines, columns).ToArray();
            var selectedValue = GetSelectedValue();

            DisplayNorthSouthEastWest(selectedValue, matrix, lines, columns);
        }

        public static (int, int) GetLinesAndColumns()
        {
            string input = null;
            int lines;
            int columns;

            do
            {
                Console.Write("Número de linhas: ");
                input = Console.ReadLine();
            } while (!int.TryParse(input, out lines));

            do
            {
                Console.Write("Número de colunas: ");
                input = Console.ReadLine();
            } while (!int.TryParse(input, out columns));

            Console.WriteLine();

            return (lines, columns);
        }

        public static IEnumerable<int> GetMatrix(int lines, int columns)
        {
            string input = null;
            IEnumerable<int> result = null;
            for (int i = 0; i < lines; ++i)
            { 
                do
                {
                    Console.Write($"    {i+1}: ");
                    input = Console.ReadLine();
                } while (!Vector.TryParse(input, columns, out result));

                foreach (var item in result)
                {
                    yield return item;
                }
            }
        }

        public static int GetSelectedValue()
        {
            string input = null;
            int selectedValue;
            do
            {
                Console.Write("Número que deseja verificar: ");
                input = Console.ReadLine();
            } while (!int.TryParse(input, out selectedValue));

            return selectedValue;
        }

        public static void DisplayNorthSouthEastWest(int selectedValue, int[] matrix, int lines, int columns)
        {
            var vector = matrix.ToArray();

            int selectedIndex = Array.FindIndex(vector, item => item == selectedValue);

            if (selectedIndex < 0)
            {
                Console.WriteLine($"Número que deseja verificar, {selectedValue}, não está na matriz.");
            }
            else
            { 
                int temp;
                int north = selectedIndex - columns;
                int south = selectedIndex + columns;
                int west  = (temp = selectedIndex) % columns == 0       ? -1 : temp - 1;
                int east  = (temp = (selectedIndex + 1)) % columns == 0 ? -1 : temp;

                string northString = north < 0 ?              "Não há" : vector[north].ToString();
                string southString = south >= vector.Length ? "Não há" : vector[south].ToString();
                string westString  = west < 0 ?               "Não há" : vector[west].ToString();
                string eastString  = east < 0 ?               "Não há" : vector[east].ToString();

                Console.WriteLine($"Esquerda : {westString}");
                Console.WriteLine($"Direita  : {eastString}");
                Console.WriteLine($"Acima    : {northString}");
                Console.WriteLine($"Abaixo   : {southString}");
            }
        }
    }
}

Para que o programa respeite o exemplo mostrado, além de fazer a sanitização das entradas do usuário, pode-se fazer o programa da seguinte forma:

using System;
using System.Collections.Generic;
using System.Linq;

namespace ConsoleApp
{
    public class Program
    {
        private static class Vector
        {
            public static bool TryParse(string input, int columns, out IEnumerable<int> decodedValues)
            {
                var list = new List<int>();
                bool result = false;

                string[] sanity = input.Split(' ', StringSplitOptions.RemoveEmptyEntries);

                if (sanity.Length != columns)
                {
                    result = false;
                }
                else if (sanity.All(entry => int.TryParse(entry, out int dummy)))
                {
                    result = true;
                    list.AddRange(sanity.Select(entry => int.Parse(entry)));
                }

                decodedValues = list;
                return result;
            }
        }

        public static void Main(string[] args)
        {
            var (lines, columns) = GetLinesAndColumns();
            var matrix = GetMatrix(lines, columns).ToArray();
            var selectedValue = GetSelectedValue();

            DisplayNorthSouthEastWest(selectedValue, matrix, lines, columns);
        }

        public static (int, int) GetLinesAndColumns()
        {
            string input = null;
            int lines;
            int columns;

            do
            {
                Console.Write("Número de linhas: ");
                input = Console.ReadLine();
            } while (!int.TryParse(input, out lines));

            do
            {
                Console.Write("Número de colunas: ");
                input = Console.ReadLine();
            } while (!int.TryParse(input, out columns));

            Console.WriteLine();

            return (lines, columns);
        }

        public static IEnumerable<int> GetMatrix(int lines, int columns)
        {
            string input = null;
            IEnumerable<int> result = null;
            for (int i = 0; i < lines; ++i)
            { 
                do
                {
                    Console.Write($"    {i+1}: ");
                    input = Console.ReadLine();
                } while (!Vector.TryParse(input, columns, out result));

                foreach (var item in result)
                {
                    yield return item;
                }
            }

            Console.WriteLine();
        }

        public static int GetSelectedValue()
        {
            string input = null;
            int selectedValue;
            do
            {
                Console.Write("Número que deseja verificar: ");
                input = Console.ReadLine();
            } while (!int.TryParse(input, out selectedValue));

            Console.WriteLine();

            return selectedValue;
        }

        public static void DisplayNorthSouthEastWest(int selectedValue, int[] matrix, int lines, int columns)
        {
            var vector = matrix.ToArray();

            int selectedIndex = Array.FindIndex(vector, item => item == selectedValue);

            if (selectedIndex < 0)
            {
                Console.WriteLine($"Número que deseja verificar, {selectedValue}, não está na matriz.");
            }
            else
            { 
                int temp;
                int north = selectedIndex - columns;
                int south = selectedIndex + columns;
                int west  = (temp = selectedIndex) % columns == 0       ? -1 : temp - 1;
                int east  = (temp = (selectedIndex + 1)) % columns == 0 ? -1 : temp;

                string northString = north < 0 ?              "Não há" : vector[north].ToString();
                string southString = south >= vector.Length ? "Não há" : vector[south].ToString();
                string westString  = west < 0 ?               "Não há" : vector[west].ToString();
                string eastString  = east < 0 ?               "Não há" : vector[east].ToString();

                Console.WriteLine($"Esquerda : {westString}");
                Console.WriteLine($"Direita  : {eastString}");
                Console.WriteLine($"Acima    : {northString}");
                Console.WriteLine($"Abaixo   : {southString}");
            }
        }
    }
}
Fonte Link

using System;
using System.Collections.Generic;
using System.Linq;

namespace ConsoleApp
{
    public class Program
    {
        private static class Vector
        {
            public static bool TryParse(string input, int columns, out IEnumerable<int> decodedValues)
            {
                var list = new List<int>();
                bool result = false;

                string[] sanity = input.Split(' ', StringSplitOptions.RemoveEmptyEntries);

                if (sanity.Length != columns)
                {
                    result = false;
                }
                else if (sanity.All(entry => int.TryParse(entry, out int dummy)))
                {
                    result = true;
                    list.AddRange(sanity.Select(entry => int.Parse(entry)));
                }

                decodedValues = list;
                return result;
            }
        }

        public static void Main(string[] args)
        {
            var (lines, columns) = GetLinesAndColumns();
            var matrix = GetMatrix(lines, columns).ToArray();
            var selectedValue = GetSelectedValue();

            DisplayNorthSouthEastWest(selectedValue, matrix, lines, columns);
        }

        public static (int, int) GetLinesAndColumns()
        {
            string input = null;
            int lines;
            int columns;

            do
            {
                Console.Write("Número de linhas: ");
                input = Console.ReadLine();
            } while (!int.TryParse(input, out lines));

            do
            {
                Console.Write("Número de colunas: ");
                input = Console.ReadLine();
            } while (!int.TryParse(input, out columns));

            Console.WriteLine();

            return (lines, columns);
        }

        public static IEnumerable<int> GetMatrix(int lines, int columns)
        {
            string input = null;
            IEnumerable<int> result = null;
            for (int i = 0; i < lines; ++i)
            { 
                do
                {
                    Console.Write($"    {i+1}: ");
                    input = Console.ReadLine();
                } while (!Vector.TryParse(input, columns, out result));

                foreach (var item in result)
                {
                    yield return item;
                }
            }
        }

        public static int GetSelectedValue()
        {
            string input = null;
            int selectedValue;
            do
            {
                Console.Write("Número que deseja verificar: ");
                input = Console.ReadLine();
            } while (!int.TryParse(input, out selectedValue));

            return selectedValue;
        }

        public static void DisplayNorthSouthEastWest(int selectedValue, int[] matrix, int lines, int columns)
        {
            var vector = matrix.ToArray();

            int selectedIndex = Array.FindIndex(vector, item => item == selectedValue);

            if (selectedIndex < 0)
            {
                Console.WriteLine($"Número que deseja verificar, {selectedValue}, não está na matriz.");
            }
            else
            { 
                int temp;
                int north = selectedIndex - columns;
                int south = selectedIndex + columns;
                int west  = (temp = selectedIndex) % columns == 0       ? -1 : temp - 1;
                int east  = (temp = (selectedIndex + 1)) % columns == 0 ? -1 : temp;

                string northString = north < 0 ?              "Não há" : vector[north].ToString();
                string southString = south >= vector.Length ? "Não há" : vector[south].ToString();
                string westString  = west < 0 ?               "Não há" : vector[west].ToString();
                string eastString  = east < 0 ?               "Não há" : vector[east].ToString();

                Console.WriteLine($"Esquerda : {westString}");
                Console.WriteLine($"Direita  : {eastString}");
                Console.WriteLine($"Acima    : {northString}");
                Console.WriteLine($"Abaixo   : {southString}");
            }
        }
    }
}