Mi spiego meglio, vorrei che il programma mi chiedesse quante colonne e quante righe vorrei inserire invece di averle già prefissate.
Grazie in anticipo, il programma è il seguente.
- Codice:
#include <iostream>
using namespace std;
const int N=3, M=3;
void leggiMatrice(int matrice[N][M]);
void stampaMatrice(int matrice[N][M]);
void calcolaTrasposta(int m[N][M], int t[M][N]);
void calcolaProdotto(int m1[N][M],int m2[M][N], int prodotto[N][N]);
int main(){
int matrice[N][M], trasposta[M][N], prodotto[N][N];
leggiMatrice(matrice);
stampaMatrice(matrice);
calcolaTrasposta(matrice,trasposta);
calcolaProdotto(matrice,trasposta,prodotto);
system("PAUSE");
}
void leggiMatrice(int matrice[N][M]){
cout << "inserisci la matrice " << N << " x " << M << ": " <<endl;
for (int i=0; i<N; i++)
for(int j=0; j<M; j++)
cin >> matrice[i][j];
return;
}
void stampaMatrice(int matrice[N][M]){
cout << "La matrice immessa e': " <<endl;
for (int i=0; i<N; i++){
for(int j=0; j<M; j++){
cout << matrice[i][j] << "\t";
}
cout <<endl;
}
return;
}
void calcolaTrasposta(int m[N][M], int t[M][N]){
cout << "La matrice trasposta e': " <<endl;
int temp;
for (int i=0; i<N; i++){
for(int j=0; j<M; j++){
t[j][i]=m[i][j]; //non avevo bisogno di "bloccare" la variabile col temp
}
}
for(int i=0; i<M; i++){
for(int j=0; j<N; j++){
cout << t[i][j] << "\t";
}
cout <<endl;
}
return;
}
void calcolaProdotto(int m1[N][M],int m2[M][N], int prodotto[N][N]){
int k;
for(int i=0; i<N; i++){
for(int j=0; j<N; j++){ // il problema era qui.Iinizialmente avevamo j<M, ma la matrice che si viene a formare é NxN, per cui andava sostituito con j<N.
prodotto[i][j] = 0;
for(k=0; k<M; k++){ // infine qui, k<M al posto di k<N... ma non ho ben capito.
prodotto[i][j] += m1[i][k] * m2[k][j];
}
}
}
cout << "La matrice prodotto delle due e': " <<endl;
for (int i=0; i<N; i++){
for(int j=0; j<N; j++){
cout << prodotto[i][j] << "\t";
}
cout <<endl;
}
return;
}