I want to create a matrix thats N by N and that N is recieved as an input and when it prints its printed in a spiral. For example if the input is a 4 that means the matrix will be 4 by 4 and it will look like this when printed:
1 2 3 4
12 13 14 5
11 16 15 6
10 9 8 7
I know how to create the matrix and make it N by N what I do not know is how can I make it look like this. Is there any guide,tutorial or anything I can watch/read to get how to make more 2d array exercises?
def llenar_matriz(n):
# Fills the matrix
for r in range(n):
fila = []
for c in range(n):
fila.append(0)
matriz.append(fila)
return matriz
def imprimir_matriz(matriz):
# Prints the matrix
filas = len(matriz)
columnas = len(matriz[0])
for f in range(filas):
for c in range(columnas):
print ("%3d" %matriz[f][c], end="")
print()
# Main program
lado = int(input("Ingrese el tamaño de la matriz: "))
while lado < 1:
print("Tamaño inválido. Debe ser mayor que 0")
lado = int(input("Ingrese el tamaño de la matriz: "))
matriz = []
llenar_matriz(lado)
imprimir_matriz(matriz)
This is my code atm and all it does is create a matrix of N by N, fills it with 0's and prints it