Read upper triangle of a matrix and print it on the screen

0

Hola buenas, would like to know why this little program does not work for me. I have to: Read the upper triangle of the matrix and print it on the screen.

At the moment I have this, but the problem is that at the time of printing / reading (I'm not sure where the problem is), the matrix is like I'm left in an infinite loop and does not do anything.

#include <stdio.h>
#define DIM 3

void leer_matriz_superior (float matriz [DIM][DIM]);
void imprimir_matriz_superior (float matriz [DIM][DIM]);

    void main () {

        float matriz [DIM][DIM];

        printf ("Introduce una matriz 3x3: ");
        leer_matriz_superior (matriz);
        imprimir_matriz_superior (matriz);
    }


    // Función 1.

    void leer_matriz_superior (float matriz [DIM][DIM]) {

        int i, j;

        for (i = 0; i = DIM -2; i ++) {
            for (j = i + 1; j = DIM - 1; j ++) {
                scanf ("%f", &matriz [i][j]);
            }
        }
    }

    // Función 2.

    void imprimir_matriz_superior (float matriz [DIM][DIM]) {

        int i, j;

        for (i = 0; i = DIM - 2; i ++) {
            for (j = i + 1; j = DIM - 1; j ++) {
                printf ("%6.0f", matriz [i][j]);
            }
            printf ("\n");
        }
    }

The compiler does not give an error, and I have reviewed the loop conditions and I think they are correct, any solution?

Greetings and thanks in advance.

Edited:

#include <stdio.h>
#define DIM 3

void leer_matriz_superior (float matriz [DIM][DIM]);
void imprimir_matriz_superior (float matriz [DIM][DIM]);

    void main () {

        float matriz [DIM][DIM];

        printf ("Introduce una matriz 3x3: ");
        leer_matriz_superior (matriz);
        imprimir_matriz_superior (matriz);
    }


    // Funció 1.

    void leer_matriz_superior (float matriz [DIM][DIM]) {

        int i, j;

        for (i = 0; i < DIM; i ++) {
            for (j = 0; j < DIM; j ++) {
                scanf ("%f", &matriz [i][j]);
            }
        }
    }

    // Funció 2.

    void imprimir_matriz_superior (float matriz [DIM][DIM]) {

        int i, j;

        for (i = 0; i = DIM - 2; i ++) {
            for (j = i + 1; j = DIM - 1; j ++) {
                printf ("%6.0f", matriz [i][j]);
            }
            printf ("\n");
        }
    }
    
asked by Alex Iglesias 30.03.2018 в 11:41
source

1 answer

1

In the conditions of the for loop, in the second parameter you are assigning i and j to DIM - 2 and DIM - 1 respectively, when it should be less than or equal to DIM - 1 .

That is, doing i = DIM - 2 or j = DIM -1 will always take that value and they will not leave the loop, since you make an assignment and do not have a stop condition, you should change it by i <= DIM - 2 and j <= DIM - 1

    
answered by 30.03.2018 / 11:53
source