I would like to learn how to fill this 10x10 matrix but without repeating the numbers, 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[11, 11]; //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);
}
// 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();
You lack an algorithm.. the one that looks for the repeated elements of the array..
but first
is it 10x10?? then why do you define it as 11X11???
let's assume... we replace this...
for something like that...
and then... we call a control function
and that function... what it does... is go through the entire array and return,
true
if it is repeated andfalse
if it is not repeated.then... we would be missing only one thing... as long as that function returns
false
, we don't save anything in the array... and if it returnstrue
, we save it...You can use a HasSet that initially stores the 100 numbers randomly and later dumps them into the matrix, in this way your code is optimized by making a single tour of the matrix
Something like
In this way you do not have to do a validation function to know if a number has already been stored or not, since the HashSet does not store duplicates, later you go through the HashSet and store it in the array. The HashSet is an unordered collection that does not allow duplicates.
Another alternative solution can be