Transposed matrix that takes non-integer and fractional values

Asked

Viewed 30 times

0

I’m solving an exercise where I type values for a matrix 3x3, the print and also display its transpose. The problem is that I am not able to receive fractional values, and comma numbers.

The code I was able to develop is this:

#include <stdio.h>
#include <stdlib.h>

void imprime_matriz(int matriz[3][3])
{
    int i,j;
    for(i=0;i<3;i++)
    {
        for(j=0;j<3;j++)
        {
          printf("  %2d ",matriz[i][j]);
        }
        printf("\n");
    }
}

void preenche_matriz(int matriz[3][3])
{
    int i,j;

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

    }

}

void transposta(int matriz[3][3])
{
    int i,j;
    int aux[3][3];

     for(i=0;i<3;i++)
    {
        for(j=0;j<3;j++)
        {
            aux[i][j] = matriz[j][i];

        }
    }

    for(i=0;i<3;i++)
    {
        for(j=0;j<3;j++)
        {
            matriz [i][j] = aux[i][j];
        }
    }
}

int main()
{   int matriz[3][3];

    printf("\nDigite os elementos da Matriz:\n\n");
    preenche_matriz(matriz);

    imprime_matriz(matriz);

    transposta(matriz);
        printf("\n");
    imprime_matriz(matriz);

    system("pause");
    return 0;
}
  • 1

    Understanding "fractional values" as real numbers then is because you have declared your matrix to be integer numbers. Try declaring your matrix as float or double and not int.

  • Also note that the transpose of a matrix x[M][N] is a matrix xt[M][N] with any M and N, may or may not be equal, and therefore you can only overwrite the original matrix with the transpose in the particular case of M equal to N.

1 answer

0

Declare your matrix as float or double! To print a double you must use "%lf" and to print a float, "%f". I hope I’ve helped!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.