// Calcolo del prodotto tra matrici #include #include #include using namespace std; #define NR 2 #define NC 3 double A[NR][NC]; double B[NR][NC]; double C[NR][NC]; void LeggiMatrice( fstream f1 ){ for (int i = 0; i < NR; i++) { for (int j = 0; j < NC; j++) { f1 >> A[i][j]; } } } void ScriviMatrice( fstream f2 ){ f2 << "La matrice A e`:" << endl; for (int i = 0; i < NR; i++) { for (int j = 0; j < NC; j++) { f2 << A[i][j] << " "; } f2 << endl; } } int main() { // double A[NR][NC]; // double B[NR][NC]; //double C[NR][NC]; //fstream f1; //fstream f2; // Lettura delle matrici da file f1.open("matrice1.dat",ios::in); for (int i = 0; i < NR; i++) { for (int j = 0; j < NC; j++) { f1 >> A[i][j]; } } if ( f1.fail() ) { cout << "Errore nell'apertura di 'matrice1.dat' ==> Esco" << endl; return -1; } f1.clear(); f1.close(); f2.open("matrice2.dat",ios::in); for (int i = 0; i < NR; i++) { for (int j = 0; j < NC; j++) { f2 >> B[i][j]; } } if ( f2.fail() ) { cout << "Errore nell'apertura di 'matrice2.dat' ==> Esco" << endl; return -1; } f2.clear(); f2.close(); // Scrittura delle matrici cout << "La matrice A e`:" << endl; for (int i = 0; i < NR; i++) { for (int j = 0; j < NC; j++) { cout << A[i][j] << " "; } cout << endl; } cout << "La matrice B e`:" << endl; for (int i = 0; i < NR; i++) { for (int j = 0; j < NC; j++) { cout << B[i][j] << " "; } cout << endl; } // Calcolo della matrice somma e sua scrittura for (int i = 0; i < NR; i++) { for (int j = 0; j < NC; j++) { C[i][j] = A[i][j]+B[i][j]; } } cout << "La matrice somma e`:" << endl; for (int i = 0; i < NR; i++) { for (int j = 0; j < NC; j++) { cout << C[i][j] << " "; } cout << endl; } f2.open("matrice1.dat",ios::out); ScriviMatrice( f2 ); f2.close(); return 0; }