Prevent the repetition of numbers in a matrix [duplicated]

0

I have an algorithm where I fill a matrix of 10 x 10. I wish that at the time of printing the matrix, no number is repeated.

This is my code:

int posi = 0, posj = 0, mayor = 0;//declaracion de variables para la posicion y el numero mayor

            Console.WriteLine("Matriz de 10*10\n");

            int[,] matriz = new int[10, 10];  //Creacion de la matriz de 10*10 (10filas, 10 columnas)
            Random aleatorio = new Random();//declaracion del random para llenar la matriz 

            //se recorre la matriz para asignar los valores

            for (int i = 0; i < 10; i++)
               for (int j = 0; j < 10; j++)
                {

                       matriz[i,j]=aleatorio.Next(0, 100);

                    //matriz[i, j] = aleatorio.Next(0, 100);
                }


            // se recorre nuevamente para leer sus valores y validar cual es mayor y capturar su respectiva posicion
                for (int i = 0; i < 10; i++)
                {
                    for (int j = 0; j < 10; j++)
                    {
                        if (matriz[i, j] > mayor)//si matriz en la posicion i,j es > mayor
                        {
                            mayor = matriz[i, j]; // a mayor -> el valor que hay en la posicion i, j
                            posi = i; // a la posicion i -> i
                            posj = j;// a la posicion j -> j
                        }
                        //imprimir matriz  
                        Console.Write(matriz[i, j].ToString() + " ");
                    }
                Console.WriteLine();
            }
            //impresion del numero mayor y su posicion.
            Console.WriteLine("El numero mayor es:{0} y esta en la posicion {1},{2}", mayor.ToString(), posi.ToString(), posj.ToString());
            Console.ReadLine();
            Console.ReadLine();
        }
    }
}
    
asked by jorge 10.08.2017 в 14:13
source

2 answers

0

If you are going to fill a matrix with values from 0 to 99 you can do it like that (I have done it in a 5x5). You only do the loop to fill the matrix.

int [,] matriz = new int[5,5];
int [] arrayValores = new int[]{0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24};

List<int> valores = arrayValores.ToList();

Random r = new Random();

for(int i=0; i<5; i++)
{
    for(int j=0; j<5; j++)
    {
        int indValor = r.Next(0,valores.Count);
        matriz[i,j] = valores.ElementAt(indValor);

        valores.RemoveAt(indValor);
    }
}

If you know what values you are going to enter, to get the index of the maximum value with an if when you "take out" the value from the list, you already have it.

    
answered by 10.08.2017 / 15:05
source
1

A very efficient way to avoid duplicates is to generate a HashSet first (which already deals with duplicates) and then fill your matrix with the values taken from there.

Then you can use Buffer.BlockCopy to give it the form of a matrix.

and finally you can search for the maximum as you are doing already

// aqui modifique para tomar m y n de variables.
int m = 10;
int n = 10;

// generar random values, count = 10x10 (columnas x filas) = 100
HashSet<int> candidates = new HashSet<int>();
while (candidates.Count < (m*n)) 
{
    // no permite dups
    candidates.Add(aleatorio.Next(0, 100));
}

// luego le das la forma de matrix
int[,] matriz = new int[m, n];
Buffer.BlockCopy(candidates, 0, matriz, 0, num.Length * sizeof(int));

// resto del programa aqui
    
answered by 10.08.2017 в 14:42