# Fill an n * m matrix diagonally

3

I have a problem that involves matrices, what I need to do is fill a matrix of N * M of dimension (that is, of a number n of columns and rows given by the user), with even numbers. example:

I have come up with the code, but I can not make the series increase diagonally, also when the matrix is square (4 * 4 example) the program runs normally), but otherwise, if it is n * m (What is requested, I have the display and an error.) Besides, the series must start from the top right (which already achieved), and end at the bottom right (but with the succession of pairs diagonally) How can I make the series of even numbers is generated diagonally exactly as in the previous example? My code:

``````    int val;
int lonv;
int fil;
int col;
int su=2;
int matriz[][];
System.out.println("Ingrese el nro de filas para la matriz: ");
System.out.println("Ingrese el nro de columnas para la matriz: ");
matriz=new int [fil][col];
for(int i=0; i<fil; i++)
{
for(int j=(col-1); j>=0; j-- )
{
if(matriz[i][j]<= 2)
{
matriz[i][j]= su;
su+=2;
}
else
{

System.out.println("");
matriz[i][j]= su;
su+=2;
}

}
}
for(int i=0; i<col; i++)
{
for(int j=0; j<col; j++)
{
System.out.print(matriz[i][j]+" ");
}
System.out.println("");
}
``````

I leave the code run for each case:

* Run of the program with square matrix (erroneous)

Run of the program with matrix of n m (erroneous)

asked by Axwell Duarte 22.11.2018 в 20:27
source

3

You have two errors.

One, I think you have not managed to conceptualize well the issue of how to traverse the matrix in diagonal , the way it should be done. I did a small exercise on paper (well in Excel, but it's the same) with a matrix of 2 rows and 3 columns and I could see that the order in which I need to go through the matrix is this:

``````1, 3
1, 2
2, 3
1, 1
2, 2
2, 1
``````

Sure it can be done with for, but it makes me more natural to use a `do/while` , so I've thought about my algorithm, which would be the following:

We start asking for the data, this part is common, then:

``````    int filas;
int columnas;
System.out.println("Ingrese el nro de filas para la matriz: ");
System.out.println("Ingrese el nro de columnas para la matriz: ");

//definimos las variables para almacenar la matriz y de control
int fila, columna, ultimaColumnaInicio, ultimaFilaInicio;
int matriz[][];
int valor = 2;
matriz = new int[filas][columnas];
``````

Long version, step by step:

``````    columna = columnas - 1;
fila = 0;
ultimaColumnaInicio = columna;
ultimaFilaInicio = fila;
do {
matriz[fila][columna] = valor;
if (columna == columnas - 1 && ultimaColumnaInicio > 0) {
fila = 0;
columna = ultimaColumnaInicio - 1;
ultimaColumnaInicio = columna;
} else if (fila == filas - 1 || columna == columnas - 1) {
fila = ultimaFilaInicio + 1;
columna = 0;
ultimaFilaInicio = fila;
} else {
fila++;
columna++;
}
valor = valor + 2;
} while ((fila != filas));
``````

A more condensed version (fewer lines of code) of the algorithm that fills the matrix would be like this:

``````    ultimaColumnaInicio = columna = columnas - 1;
ultimaFilaInicio = fila = 0;
do {
matriz[fila++][columna++] = valor;
if (columna == columnas && ultimaColumnaInicio > 0) {
fila = 0;
columna = --ultimaColumnaInicio;
} else if (fila == filas || columna == columnas) {
fila = ++ultimaFilaInicio;
columna = 0;
}
valor = valor + 2;
} while ((fila != filas));
``````

Finally, you have an error when printing the matrix, since you use the variable `col` for both the rows iterator and the columns iterator, so it raises an exception when the matrix is not square. The correct version would be something like:

``````    for(int i=0; i < filas; i++) {
for(int j=0; j < columnas; j++) {
System.out.print(String.format("%3d", matriz[i][j]) +" ");
}
System.out.println("");
}
``````

This produces an output like this:

4x5 matrix

`````` 22  14   8   4   2
30  24  16  10   6
36  32  26  18  12
40  38  34  28  20
``````

7x5 matrix

`````` 22  14   8   4   2
32  24  16  10   6
42  34  26  18  12
52  44  36  28  20
60  54  46  38  30
66  62  56  48  40
70  68  64  58  50
``````

0

Well, the way I see the error, you have it on the way to show the matrix, as I show you next:

``````//Comienzas por las filas y después las columnas
for(int i=0; i<fil; i++)
{
for(int j=0; j<col; j++)
{
System.out.print(matriz[i][j]+" ");
}
System.out.println("");
}
``````

You have to go through row, column. I hope you find it