#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();
void matriks_jumlah(const matriks&, const matriks&);
void perkalian_matriks(const matriks&, const matriks&);
void cetak_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];
}
}
void matriks::matriks_jumlah(const matriks& matriks1, const matriks& matriks2){
int i,j;
baris=matriks1.baris;
kolom=matriks1.kolom;
for(i=0;i<baris;i++)
for(j=0;j<kolom;j++)
A[i][j]=matriks1.A[i][j]+matriks2.A[i][j];
//cetak_matriks();
cout <<endl;
}
void matriks::perkalian_matriks(const matriks& matriks1, const matriks& matriks2){
int i,j,k;
int barkol;
baris=matriks1.baris;
kolom=matriks1.kolom;
barkol=matriks1.kolom;
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];
}
// cetak_matriks();
}
void matriks::cetak_matriks(){
int i,j;
for(i=0;i<baris;i++){
for(j=0;j<kolom;j++)
cout<<setw(5)<<A[i][j]<<" ";
cout <<endl;
}
}
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();
cout<<"matriks yang dibuat adalah : \n";
A.cetak_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]<<" ";
}
return out;
}
main(){
matriks matriks1, matriks2;
matriks jumlah;
cout<< "Masukkan data matriks I\n";
cin>>matriks1;
cout<<"Masukkan data matriks II\n";
cin>>matriks2;
jumlah.matriks_jumlah(matriks1, matriks2);
cout<<"Hasil penjumlahan : \n";
jumlah.cetak_matriks();
jumlah.perkalian_matriks(matriks1,matriks2);
cout<<"Hasil perkalian : \n";
jumlah.cetak_matriks();
return 0;
};
0 komentar:
Post a Comment