Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Ex 20 multiplication de matrices
- #include <iostream>
- #include <vector>
- using namespace std;
- typedef vector <vector<int> > Matrice;
- Matrice lire_matrice();
- Matrice multiplication (const Matrice& M1,const Matrice& M2);
- void affiche_matrice (const Matrice& M);
- int main(){
- Matrice M1,M2;
- M1=lire_matrice();
- M2=lire_matrice();
- //M1={{1,2,3},{4,5,6}};//Test
- //M2={{1,2,3,4},{5,6,7,8},{9,0,1,2}};//Test
- //resultat test ...
- // 38 14 20 26
- // 83 38 53 68
- if (M2.size()==M1[0].size()){//nombre lignes M2 identique colonnes M1
- affiche_matrice(multiplication ( M1, M2));
- }else {
- cout<<"Multiplication de matrices impossible !"<<endl;
- }
- return 0;
- }
- Matrice lire_matrice(){
- size_t lignes(0),max_lignes(0),colonnes(0),max_colonnes(0);
- int valeur(0);
- Matrice tmp;
- cout <<"Saissie d'une matrice :"<<endl<<"Nombre de lignes : ";
- cin>>max_lignes;
- cout <<"Nombre de colonnes : ";
- cin >>max_colonnes;
- for (lignes=0;lignes< max_lignes;lignes++){
- tmp.push_back(vector<int> ());
- for (colonnes=0;colonnes<max_colonnes;colonnes++){
- cout<<"M["<<lignes<<","<<colonnes<<"]=";
- cin>> valeur;
- tmp[lignes].push_back(valeur);
- }
- }
- return tmp;
- };
- void affiche_matrice (const Matrice& M){
- for (auto ligne:M){
- cout<<endl;
- for (auto elem:ligne){
- cout << elem << " ";};
- }
- cout <<endl;
- };
- Matrice multiplication (const Matrice& M1,const Matrice& M2){
- size_t N=M1.size();
- size_t M=M1[0].size();
- size_t X=M2.size();
- size_t i,j,k;
- Matrice c( M1.size(),std::vector <int>(M2[0].size()));
- for (i=0; i<N; i++)
- for (j=0; j<=M; j++) {
- c[i][j] = 0;
- for (k=0; k<X; k++)
- c[i][j] += M1[i][k] * M2[k][j];
- }
- return c;
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement