All pastes #762929 Raw Edit

Mine

public text v1 · immutable
·published 2007-11-06 00:29 UTC
rendered paste body
#include <iostream>
using namespace std;
struct Taille {
 unsigned int Col;
 unsigned int Lin;
};

struct Matrices {
 long ** M1;
 long ** M2;
 Taille T1;
 Taille T2;
};
Matrices multiply (Matrices T);
Matrices add (Matrices T);
Matrices init ();
Matrices define (Matrices K);
int print (Matrices T,int print = 0);

int main () {
 cout << "Addition matricielle" << endl << endl;
 Matrices K = init();
 Matrices T = define(K);
 cout <<endl<<endl;
 print(T,1);
 cout <<endl<<endl;
 print(T,2);
 Matrices B = multiply(T);
 print(B,3);
}

Matrices init () {
 // initialisation des dimensions des matrices
 cout << "Entrez à présent les dimensions de vos 2 matrices" << endl; 
 cout << "Matrice 1 : Lignes" << endl;
 Matrices K;
 K.T1.Col = 0, K.T1.Lin = 0, K.T2.Col = 0, K.T2.Lin = 0;
 cin >> K.T1.Lin;
 cout << "Matrice 1 : Colonnes" << endl;
 cin >> K.T1.Col;
 cout << "Matrice 2 : Lignes" << endl; 
 cin >> K.T2.Lin;
 cout << "Matrice 2 : Colonnes" << endl;
 cin >> K.T2.Col;
 // Initialisation des tableaux dynamiques
 K.M1 = NULL;
 K.M2 = NULL;
 K.M1 = new long*[K.T1.Col], K.M2 = new long*[K.T2.Col];
 for (int i = 0; i < K.T1.Col; i++)  K.M1[i] = new long[K.T1.Lin];
 for (int i = 0; i < K.T2.Col; i++)  K.M2[i] = new long[K.T2.Lin];
 cout << "Matrices initialisés" << endl;
 cout << "Taille Matrice 1 : Il y a " << K.T1.Lin;
 cout << " lignes et "<<K.T1.Col<< " colonnes"<<endl;
 cout << "Taille Matrice 2 : Il y a " << K.T2.Lin;
 cout << " lignes et "<<K.T2.Col<< " colonnes"<<endl;
 return K;
}

Matrices define (Matrices K) {
//Initialisation des valeurs dans les matrices
 for (int i = 0; i < K.T1.Col; i++) {
  for (int t = 0; t < K.T1.Lin; t++) {
   cout << "Entrez le "<<i+1<<" chiffre de la "<<t+1<< " ligne (Matrice 1) " << endl;
   cin >> K.M1[i][t];
  }
 }
 for (int i = 0; i < K.T2.Col; i++) {
  for (int t = 0; t < K.T2.Lin; t++) {
   cout << "Entrez le " <<i+1<<" chiffre de la "<<t+1<< " ligne (Matrice2)" << endl;
   cin >> K.M2[i][t];
  }
 }
 return K;
}
 
int print (Matrices T, int print) {
//affichage des matrices
 if (print == 1) {
  cout << "Matrice n°" << print << endl;
  for ( int i = 0; i < T.T1.Col; i++ ) {
   for ( int k = 0; k < T.T1.Lin; k++) {
    cout << T.M1[i][k] <<" ";
   }
  cout << endl;
  }
 }
 if (print == 2) {
  cout <<"Matrice n°" << print << endl;
  for (int i = 0; i < T.T2.Col; i++) {
   for (int k = 0; k < T.T2.Lin; k++) {
    cout << T.M2[i][k] << " ";
   }
   cout << endl;
  }
 }
 if ( print == 3) {
  cout <<"Matrice 1 + Matrice 2 =" << endl ;
  for (int i = 0; i < T.T1.Col; i++) {
   for (int k = 0; k < T.T1.Lin; k++) {
    cout << T.M1[i][k] << " ";
   }
   cout <<endl;
  }
 }
 return 0;
}

Matrices add ( Matrices T ) {
 Matrices B;
 B.T1.Col = T.T1.Col;
 B.T1.Lin = T.T1.Lin;
 B.M2 = NULL;
 B.M1 = new long*[B.T1.Col];
 for (int i = 0; i < B.T1.Col; i++) B.M1[i] = new long[B.T1.Lin];
  if ((T.T1.Col == T.T2.Col) && (T.T1.Lin == T.T2.Lin)) {
   for (int i = 0; i < T.T1.Col;i++) {
    for (int k = 0; k < T.T1.Lin; k++) {
     B.M1[i][k] = T.M1[i][k] + T.M2[i][k];
    }
   }
  }
 return B;
}
Matrices multiply ( Matrices K ) {
 Matrices R ;
 R.T1.Col = K.T2.Col;
 R.T1.Lin = K.T1.Lin;
 R.M2 = NULL;
 R.M1 = new long *[R.T1.Col];
 for (int i = 0; i < R.T1.Col; i++) R.M1[i] = new long [R.T1.Lin];
 if (K.T1.Col == K.T2.Lin) {
  for (int i = 0; i < R.T1.Col; i++) {
   for (int j = 0; j < R.T1.Lin; j++) {
    for (int k = 0; k < R.T1.Lin; k++) {
     R.M1[i][j] += K.M1[i][k] * K.M2[k][j];
     }
    }
   }
   return R;
  }
  else {
   cout << "Vous ne pouvez pas multiplier ces 2 matrices : dim error" << endl;
   return K;
  }
}