CLR: .Net / Mono / Boo / Otros CLR > C#

 Matriz/Array sin datos repetidos en C#

(1/1)

joshernandez:
Hola, que tal buenas tardes.
Disculpen, alguien me podria ayudar a realizar un bucle/ciclo e if, que no me permita almacenar el mismo valor en algun campo de la matriz.
En este codigo me permite asignar el tamaño de la matriz y luego ingresar los valores, pero no se que codigo necesito ingresar para que al insertar un numero y si este ya esta ingresado, no me lo permita y asi hasta colocar un numero distino y rellanar todos los campos de la matriz. Gracias


--- Código: ---using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Matriz_bidimensional
{
    class Program
    {
        static void Main(string[] args)
        {
            bool dato = false;
            double valor = 0;
            valores v = new valores();

            //bucle o ciclo do que repite el codigo de la clase el cual almacena la primer peticion del tamaño de la matriz
            do
            {
                var result = v.valor_inicial();
                dato = result.isValid;
                valor = result.valor;
            }
            while (dato == false);

            Console.WriteLine("El valor ingresado fue: {0}", valor);






            //codigo para la creación de la matriz
            Console.WriteLine();
            int[,] matriz = new int[Convert.ToInt32(valor), Convert.ToInt32(valor)];
            for (int a = 0; a < valor; a++)
            {
                for (int b = 0; b < valor; b++)
                {
                    Console.Write("Ingrese el número: ");
                    double numero = Convert.ToDouble(Console.ReadLine());
                    matriz[a, b] = Convert.ToInt32(numero);
                }
            }







            //Codigo que manda a imprimir la matriz
            Console.WriteLine();
            Console.WriteLine("Matriz primer orden");
            for (int a = 0; a < valor; a++)
            {
                for (int b = 0; b < valor; b++)
                {
                    Console.Write("[{0}]", matriz[a, b]);
                    Console.Write(" ");
                }
                Console.WriteLine();

            }

            Console.WriteLine();
            Console.WriteLine("Matriz segundo orden");
            //Codigo que manda a imprimir la matriz al revez
            for (int a = 0; a < valor; a++)
            {
                for (int b = 0; b < valor; b++)
                {
                    Console.Write("[{0}]", matriz[b, a]);
                    Console.Write(" ");
                }
                Console.WriteLine(" ");
            }

            Console.ReadKey();
        }





        //Clase para poder ingresar el primer valor positivo, entero y mayor a 1
        class valores
        {
            public Result valor_inicial()
            {
                Result result = new Result();

                Console.WriteLine("Ingresa el número de la matriz");
                double valor = Convert.ToDouble(Console.ReadLine());

                if (valor >= 0 && valor % 1 == 0 && valor > 1)
                {
                    result.isValid = true;
                    result.valor = valor;
                }
                else
                {
                    Console.WriteLine("Ingresa un número entero, positivo y mayor a 1.");
                    Console.WriteLine();
                    result.isValid = false;
                }
                return result;
            }
        }


        //se define una propiedad autoimplementada (con esto declarado, pudemos acceder a los valores ya sea para obtener un valor o asignarle uno).
        public class Result
        {
            public bool isValid { get; set; }
            public double valor { get; set; }
        }
    }
}
--- Fin del código ---

Ayuda por favor!!

cesar.queb:
Cuando desees realizar este tipo de tareas, lo ideal es utilizar un objeto de tipo Diccionario.


--- Código: C# ---// C# program to illustrate how// to  check the given key or // value present in the dictionary// or notusing System;using System.Collections.Generic;   class GFG {         // Main Method    static public void Main () {                          // Creating a dictionary        // using Dictionary<TKey,TValue> class        Dictionary<int, string> My_dict =                        new Dictionary<int, string>();                      // Adding key/value pairs in the           // Dictionary Using Add() method          My_dict.Add(1123, "Welcome");          My_dict.Add(1124, "to");          My_dict.Add(1125, "GeeksforGeeks");                     // Using ContainsKey() method to check          // the specified key is present or not          if (My_dict.ContainsKey(1122)==true)          {              Console.WriteLine("Key is found...!!");          }           else          {               Console.WriteLine("Key is not found...!!");          }                     // Using ContainsValue() method to check          // the specified value is present or not          if (My_dict.ContainsValue("GeeksforGeeks")==true)          {              Console.WriteLine("Value is found...!!");          }           else          {               Console.WriteLine("Value is not found...!!");          }    }} 
Saludos.

Navegación

[0] Índice de Mensajes

Ir a la versión completa