codice:
#include <stdio.h>
#include <stdlib.h>
void trasposta(int **trasp, int **matrice, int righe, int colonne);
void moltiplicazione(int **ris, int **trasp, int **matrice, int righe,
int colonne);
int main(void) {
int righe, colonne, i, j;
int **matrice;
int **trasp;
int **ris;
printf(" Righe........: ");
fflush(stdout);
scanf("%d", &righe);
printf(" Colonne......: ");
fflush(stdout);
scanf("%d", &colonne);
puts("");
matrice = (int**) malloc((righe * sizeof(int*)));
trasp = (int**) malloc((colonne * sizeof(int*)));
ris = (int**) malloc((righe * sizeof(int*)));
for (i = 0; i < righe; i++) {
matrice[i] = (int*) malloc(colonne * sizeof(int));
}
for (i = 0; i < righe; i++) {
ris[i] = (int*) malloc(colonne * sizeof(int));
}
for (i = 0; i < colonne; i++) //poichè la matrice trasposta ha num righe e colonne invertire
{
trasp[i] = (int*) malloc(righe * sizeof(int));
}
for (i = 0; i < righe; ++i) {
for (j = 0; j < colonne; ++j) {
printf("Inserisci elemento riga %d e colonna %d: ", i, j);
fflush(stdout);
scanf("%d", &matrice[i][j]);
printf("\n");
fflush(stdout);
}
}
for (i = 0; i < colonne; ++i) {
for (j = 0; j < righe; ++j) {
trasp[i][j] = 0;
}
}
for (i = 0; i < righe; ++i) {
for (j = 0; j < colonne; ++j) {
ris[i][j] = 0;
}
}
trasposta(trasp, matrice, righe, colonne);
moltiplicazione(ris, matrice, trasp, righe, colonne);
for (i = 0; i < righe; ++i) {
for (j = 0; j < colonne; ++j) {
printf("matrice[%d][%d]= %d ", i, j, matrice[i][j]);
fflush(stdout);
}
printf("\n");
fflush(stdout);
}
for (i = 0; i < colonne; ++i) {
for (j = 0; j < righe; ++j) {
printf("trasp[%d][%d]= %d ", i, j, trasp[i][j]);
fflush(stdout);
}
printf("\n");
fflush(stdout);
}
for (i = 0; i < colonne; ++i) {
for (j = 0; j < righe; ++j) {
printf("ris[%d][%d]= %d ", i, j, ris[i][j]);
fflush(stdout);
}
printf("\n");
fflush(stdout);
}
return 0;
}
void trasposta(int **trasp, int **matrice, int righe, int colonne) {
int i, j; /*Dichiaro la variabile temporanea per lo scambio*/
for (i = 0; i < colonne; i++) {
for (j = 0; j < righe; j++) {
trasp[i][j] = matrice[j][i];
}
}
}
void moltiplicazione(int **ris, int **trasp, int **matrice, int righe,
int colonne) {
int i, j, k;
int tmp = 0;
for (i = 0; i < righe; i++) {
for (j = 0; j < colonne; j++) {
for (k = 0; k < righe; k++) {
tmp += matrice[i][k] * trasp[k][j];
printf("variabile temporanea %d", tmp);
fflush(stdout);
printf("\n");
fflush(stdout);
}
ris[i][j] = tmp;
//tmp=0;
}
}
}
Ecco il risultato stampato a video: