ho segnato con molti asterischi la parte di codice che secondo me da problemi:
se la parte di codice commentata da //traspongo la metto prima di //visualizza matrici il programma non fa nessun printf e si chiude...
se la metto dopo si esegue e va tutto bene![]()
mi sapreste aiutare??
grazie
codice:#include <stdio.h> #include <stdlib.h> struct matrice { int r; int c; double ** m; }; struct matrice crea_matrice(int r, int c); // r = righe, c = colonne int moltiplica_matrici(struct matrice * c, struct matrice a, struct matrice b); int trasponi_matrice(struct matrice * c, struct matrice a); int stampa_matrice(struct matrice a); int main (){ struct matrice x, xt, v, xtv; struct matrice varianza; x = crea_matrice(2,1); xt = crea_matrice(x.c,x.r); v = crea_matrice(x.r,x.r); varianza = crea_matrice(1,1); xtv = crea_matrice(xt.r,v.c); //dati matrici x.m[0][0] = 0.5; x.m[1][0] = 0.5; v.m[0][0] = 2; v.m[0][1] = 0.5; v.m[1][0] = 0.5; v.m[1][1] = 3; //traspongo ********************************************** if(!trasponi_matrice(&xt,x)) printf("errore..."); //visualizza matrici ****************************************** printf("\nx:\n"); stampa_matrice(x); printf("\nxt:\n"); stampa_matrice(xt); printf("\nv:\n"); stampa_matrice(v); //xt*V*x if(!moltiplica_matrici(&xtv,xt,v)) printf("errore..."); if(!moltiplica_matrici(&varianza,xtv,x)) printf("errore..."); printf("varianza %f",varianza.m[0][0]); getchar(); return 1; } struct matrice crea_matrice(int r, int c){ // r = righe, c = colonne int x,y; struct matrice matrice; matrice.r = r; matrice.c = c; matrice.m = (double **) malloc(r * sizeof(double *)); for(x=0;x<r;x++) matrice.m[x] = (double *) malloc(c * sizeof(double)); for(x=0;x<r;x++) for(y=0;y<c;y++) matrice.m[x][y] = 0; return matrice; } int moltiplica_matrici(struct matrice * c, struct matrice a, struct matrice b){ int x,y,i; if(a.c != b.r) return 0; if(c->r != a.r || c->c != b.c) return 0; for(x=0;x<c->r;x++) for(y=0;y<c->c;y++) for(i=0;i<a.c;i++) c->m[x][y] = c->m[x][y] + a.m[x][i]*b.m[i][y]; return 1; } int trasponi_matrice(struct matrice * c, struct matrice a){ int x,y; if(c->r != a.c || c->c != a.r) return 0; for(x=0;x<c->r;x++) for(y=0;y<c->c;y++) c->m[y][x] = a.m[x][y]; return 1; } int stampa_matrice(struct matrice a){ int x,y; for(x=0;x<a.r;x++){ for(y=0;y<a.c;y++) printf("%f\x09",a.m[x][y]); printf("\n"); } return 1; }![]()

Rispondi quotando
