Ciao a tutti!!
Stavo facendo un esercizio in c++, ho completato tutto, però vorrei un vostro parere.
Questo è il codice:
Interfaccia
Implementazionecodice:#ifndef MATRICE_H #define MATRICE_H typedef double tipoElem; class Matrice { private: int righe; int colonne; tipoElem **elementi; public: Matrice(int r, int c); Matrice(const Matrice& mat); int GetRighe(); int GetColonne(); void StampaMatrice(); void AzzeraMatrice(); tipoElem LeggiMatrice(int r, int c); void ScriviMatrice(int r, int c, tipoElem val); void ProdottoScalare(double scalare); Matrice Trasposta(); Matrice ProdottoMatriciale(Matrice m2); }; #endif
Ora il mio dubbio sorge perché nei metodi Trasposta e ProdottoMatriciale faccio restituire la matrice che ottengo eseguendo i vari calcoli, per come ho fatto però mi viene da pensare che in realtà stia restituendo la matrice che verrà distrutta alla fine del metodo stesso.codice:#include "Matrice.h" #include <iostream> using namespace std; Matrice::Matrice(int r, int c) { colonne = c; righe = r; int i; elementi = new tipoElem* [righe]; for (i = 0; i < righe; i++) elementi[i] = new tipoElem[colonne]; AzzeraMatrice(); } Matrice::Matrice(const Matrice& matCopy) { righe = matCopy.righe; colonne = matCopy.colonne; elementi = new double*[righe]; for(int i = 0; i < righe; i++) { elementi[i] = new double[colonne]; } for(int i = 0; i < righe; i++) { for(int j = 0; j < colonne; j++) { elementi[i][j] = matCopy.elementi[i][j]; } } } int Matrice::GetRighe() { return righe; } int Matrice::GetColonne() { return colonne; } void Matrice::StampaMatrice() { for(int i = 0; i < righe; i++) { for(int j = 0; j < colonne; j++) { cout << LeggiMatrice(i, j) << " "; } cout << endl; } } void Matrice::AzzeraMatrice() { for(int i = 0; i < righe; i++) { for(int j = 0; j < colonne; j++) { ScriviMatrice(i, j, 0); } } } tipoElem Matrice::LeggiMatrice(int r, int c) { try { if(r >= 0 && r < righe && c >= 0 && c < colonne) { return elementi[r][c]; } else { throw "Indici errati."; } } catch(char *string) { cout << endl << string << endl; } } void Matrice::ScriviMatrice(int r, int c, tipoElem val) { try { if(r >= 0 && r < righe && c >= 0 && c < colonne) { elementi[r][c] = val; } else { throw "Indici errati."; } } catch(char *string) { cout << endl << string << endl; } } void Matrice::ProdottoScalare(double scalare) { for(int i = 0; i < righe; i++) { for(int j = 0; j < colonne; j++) { ScriviMatrice(i, j, LeggiMatrice(i, j) * scalare); } } } Matrice Matrice::Trasposta() { Matrice trasposta(righe, colonne); for(int i = 0; i < righe; i++) { for(int j = 0; j < colonne; j++) { trasposta.ScriviMatrice(i, j, LeggiMatrice(j, i)); } } return trasposta; } Matrice Matrice::ProdottoMatriciale(Matrice m2) { Matrice matProd(righe, m2.GetColonne()); for(int i = 0; i < righe; i++) { for(int j = 0; j < m2.GetColonne(); j++) { matProd.ScriviMatrice(i, j, 0); for(int k = 0; k < righe; k++) { matProd.ScriviMatrice(i, j, matProd.LeggiMatrice(i, j) + (LeggiMatrice(i, k) * m2.LeggiMatrice(k, j))); } } } return matProd; }
Quindi mi chiedevo se in realtà il metodo corretto era questo:
Grazie a tutti anticipatamentecodice:... ... Matrice* Matrice::Trasposta() { Matrice *trasposta = new Matrice(righe, colonne); for(int i = 0; i < righe; i++) { for(int j = 0; j < colonne; j++) { trasposta->ScriviMatrice(i, j, LeggiMatrice(j, i)); } } return trasposta; } Matrice* Matrice::ProdottoMatriciale(Matrice m2) { Matrice *matProd = new Matrice(righe, m2.GetColonne()); for(int i = 0; i < righe; i++) { for(int j = 0; j < m2.GetColonne(); j++) { matProd->ScriviMatrice(i, j, 0); for(int k = 0; k < righe; k++) { matProd->ScriviMatrice(i, j, matProd->LeggiMatrice(i, j) + (LeggiMatrice(i, k) * m2.LeggiMatrice(k, j))); } } } return matProd; }![]()

Rispondi quotando