15 May 2012

Program Matrik kali Matrik

#include <stdio.h>
#include <iostream.h>
#include <iomanip.h>

class matriks{
    friend istream& operator>>(istream&, matriks&);
    friend ostream& operator<<(ostream&, matriks&);
public:
    void baca_matriks();
    matriks& perkalian_matriks(const matriks&, const matriks&);
private:
    int baris, kolom;
    int A[10][10];
};

void matriks::baca_matriks(){
    int i,j;
    for(i=0; i<baris; i++)
        for(j=0;j<kolom;j++){
            cout<<"Data["<<i+1<<"],["<<j+1<<"] :";
            cin>>A[i][j];
        }
}
matriks& matriks::perkalian_matriks(const matriks& matriks1, const matriks& matriks2){
    int i,j,k;
    int barkol;
    baris=matriks1.baris;
    kolom=matriks2.kolom;
    barkol=matriks2.baris;
    for(i=0;i<baris;i++)
        for(j=0;j<kolom;j++){
            A[i][j]=0;
            for(k=0;k<barkol;k++){
                A[i][j]+=matriks1.A[i][k]*matriks2.A[k][j];
            }
        }
    return* this;
}
istream& operator>>(istream& in, matriks& A){
    cout<<"Banyak baris : ";
    in>>A.baris;
    cout<<"Banyak kolom : ";
    in>>A.kolom;
    cout<<"Masukkan data matriks\n";
    A.baca_matriks();
return in;
}

ostream& operator<<(ostream& out, matriks& A){
    int i,j;
    for(i=0;i<A.baris;i++)
    {
    for(j=0;j<A.kolom;j++)
        cout <<setw(5)<<A.A[i][j]<<" ";
        cout <<endl;
    }
    return out;
}

int main(){
matriks matriks1, matriks2, kali;
cout<< "Masukkan data matriks I\n"; cin>>matriks1;
cout<<"Data Matrik I :\n"; cout <<matriks1;
cout<<"Masukkan data matriks II\n"; cin>>matriks2;
cout<<"Data Matrik II :\n"; cout <<matriks2;
kali.perkalian_matriks(matriks1,matriks2);
cout<<"Hasil perkalian : \n";
cout << kali;
return 0;
}

0 komentar:

Post a Comment