So, I have this school asignement and I just can't get it right. I'm supposed to make a rectangular array, size defined by the user then I have to print all the values within the array. It ought to look like this:
This is my code so far (on github):
static void Main(string[] args)
{
Console.WriteLine("Ingrese un número entero: ");
int userValue = int.Parse(Console.ReadLine()); //el número ingresado por el usuario es guardado en una variable
if (userValue <= 10 && userValue > 0) //valida que el número ingresado sea igual o menor a 10 y mayor que 0
{
int[,] rectArray = new int[userValue,userValue]; //la variable que registra el número dado por el usuario es usada para definir el tamaño del arreglo
Console.WriteLine("Los valores del arreglo son:");
for (int file = 0; file < rectArray.GetLength(0); file )
{
if (file > 0) {Console.WriteLine(" " file);}; //Se utiliza un condicional para evitar que se imprima la primer línea con valor de 0
for (int column = 0; column < rectArray.GetLength(1); column )
{
int colValue = column file 1;
Console.Write(" " colValue );
};
};
Console.ReadKey();
}
else
{
Console.WriteLine("El valor necesita ser igual o menor a 10.");
Console.ReadKey();
};
}
CodePudding user response:
I would advice to separate the population logic from printing.
The population logic is almost good. You just don't need the if
statement:
int[,] rectArray = new int[userValue, userValue];
for (int row = 0; row < userValue; row )
{
for (int column = 0; column < userValue; column )
{
rectArray[row, column] = column row 1;
}
}
The printing is almost the same but instead of assigning values to each element of the array, you need to retrieve them:
for (int row = 0; row < rectArray.GetLength(0); row )
{
for (int column = 0; column < rectArray.GetLength(1); column )
{
Console.Write(" " rectArray[row, column]);
}
Console.WriteLine();
}