Why do you round my decimals? Problem with two-dimensional arrangement C

2

I am starting in the art of programming, and I am working on an exercise to show the inverse of a matrix. The inverse matrix is defined as a double array, and I am using the algorithm for cofactors. The problem is when I show the matrix on the screen the results come out rounded, for example:

If it is 1/6 instead of showing 0.1666 shows 0.000 If it is 5/2 instead of showing 2.5 shows 2,000.

I paste the code so you can help me. Greetings

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

int **crear_matint();
double **crear_matflot();
void ingresar_matriz( int **mat);
int cof_ij(int a, int b, int **mat);
int determinante( int **mat);
double **inversa(int ** mat);



int main (){

int i,j,a,b;
int **mat;
double **inv;
double k;

    mat=crear_matint();

    ingresar_matriz(mat);

     inv= inversa(mat);


a= cof_ij(1,2,mat);
b= determinante(mat);



printf("\n\n\n\n");

for(i=0;i<3;i++){
    for(j=0;j<3;j++){
        printf("%i ",mat[i][j]);

        }
        printf("\n");
        }



      printf("\n\n\n\n");

for(i=0;i<3;i++){
    for(j=0;j<3;j++){
        printf("%.5f \t",inv[i][j]);

        }
        printf("\n");
        }
k=1/6;
printf(" %i %i  %.1f",a,b,k);

return 0;}




int **crear_matint(int **a){
int i;
int **mat;

mat=(int **)calloc(3,sizeof(int *));

for(i=0;i<3;i++){
    mat[i]=(int *)calloc(3,sizeof(int ));}

    return mat;
    }




double **crear_matflot(){
int i;
double **mat;

mat=(double **)calloc(3,sizeof(double *));

for(i=0;i<3;i++){
    mat[i]=(double *)calloc(3,sizeof(double ));}

    return mat;
    }


void ingresar_matriz( int **mat){

    int i,j;

    for(i=0;i<3;i++){
        for(j=0;j<3;j++){
            printf("Ingresa el elemento (%i,%i)",(i+1),(j+1));
            scanf("%i",&mat[i][j]);
            }}}




int determinante(int **mat){
    int i,det=0;

    for(i=0;i<3;i++){
    det+=(cof_ij(0,i,mat))*(mat[0][i]);}
    return det;
    }




int cof_ij(int a, int b, int **mat){

    int c_ij;
    int i,j,k=0;
    int mat2[4];

    for(i=0;i<3;i++){
        for(j=0;j<3;j++){
            if(i!=a && j!=b){
            mat2[k]=mat[i][j];
            k++;
            }}}
    c_ij= mat2[0]*mat2[3]- mat2[1]*mat2[2];
    c_ij*=pow((-1),(a+b));

    return c_ij;
    }



double **inversa(int ** mat){
    int det,i,j;
    double **inv;

    inv=crear_matflot();

    det=determinante(mat);

    for(i=0;i<3;i++){
        for(j=0;j<3;j++){

      inv[j][i] = (cof_ij(i,j,mat))/det;}}

      return inv;

    }
    
asked by Román Castillo 30.07.2018 в 00:24
source

1 answer

3

In C when you divide integers the result obtained is an integer. That's why when you do 5/2 the result is rounded. This is solved by casting a float on some element, be it the numerator or denominator.

For example: if you have 5.0/2 or 5/2.0 the result will be 2.500000 . If your variables are x e y would be x/(float) y; or (float)x/y;

I hope it serves you.

    
answered by 30.07.2018 / 01:14
source