This is the code I made go throw the spiral matrix, but it go throw the the matrix differently
2000021
1970022
1860123
1714131224
1600025
the code is it:
public class caracol {
public static void main(String[] args) {
int[][] matriz = new int[5][5];
int n=5;
int nlimite= n-1;
int inicio = 0;
int c=1;
while(c<=(n*n)) {
for (int i = inicio+2; i<=nlimite-1; i++) //baja
{
matriz[i][nlimite-1] = c++;
}
for (int i = inicio+2; i>=inicio+1; i--) ///izquierda
{
matriz[nlimite-1][i] = c++;
}
for (int i = nlimite-1; i>=inicio+1; i--) //sube
{
matriz[i][inicio+1] = c++;
}
for (int i = inicio+1; i>=nlimite-1; i++) //derecha
{
matriz[inicio+1][i] = c++;
}
for (int i = inicio+1; i<=nlimite; i++) //baja
{
matriz[i][nlimite] = c++;
}
for (int i = nlimite-1; i>=inicio; i--) ///izquierda
{
matriz[nlimite-1][i] = c++;
}
for (int i = nlimite; i>=inicio; i--) //sube
{
matriz[i][inicio] = c++;
}
for (int i = inicio; i>=nlimite; i++) //derecha
{
matriz[inicio][i] = c++;
}
for (int i = inicio; i<=nlimite; i++) //baja
{
matriz[i][nlimite] = c++;
}
nlimite=nlimite-1;
inicio = inicio+1;
}
for(int x=0;x<n;x++) { /*Mostrar la matriz en pantalla*/
System.out.println();
for(int y=0;y<n;y++) {
System.out.print(matriz[x][y]);
}
}
}
}
the resulting matrix is different compared to the matrix of the photo. you must start in matrix [2] [2], but it does in matrix [0] [0], as you change the position of the index of the row and column.
Try something like this:
Will output:
Edit: Added text as per
Me Myself
requested