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;
}