#include <iostream.h>
#include <iomanip.h>
#include <stdio.h>
class matrix{
friend ostream& operator<<(ostream&, matrix&);
friend istream& operator>>(istream&, matrix&);
public:
void input_matrix();
int baris, kolom;
int M[10][10];
};
class vektor{
friend ostream& operator<<(ostream&, vektor&);
friend istream& operator>>(istream&, vektor&);
public:
vektor& matrix_kali_vektor(matrix&, vektor&);
vektor();
int banyak;
int elemen[10];
};
vektor::vektor(){
banyak=10;
for(int i=0; i<banyak; i++) elemen[i]=0;
}
void matrix::input_matrix(){
int i,j;
for(i=0; i<baris; i++)
for(j=0;j<kolom;j++){
cout<<"Data["<<i+1<<"]["<<j+1<<"] :";
cin>>M[i][j];
}
}
istream& operator>>(istream& in, matrix& input){
cout<<"Masukkan baris : ";
in>>input.baris;
cout<<"Masukkan kolom : ";
in>>input.kolom;
cout<<"Masukkan data matrix"<<endl;
input.input_matrix();
return in;
}
ostream& operator<<(ostream& out, matrix& output){
int i,j;
for(i=0;i<output.baris;i++){
for(j=0;j<output.kolom;j++){
cout <<setw(5)<<output.M[i][j]<<" ";
}
cout <<endl;
}
return out;
}
istream& operator>>(istream& in, vektor& input){
cout<<"Banyak elemen vektor : ";
in>>input.banyak;
cout<<"Masukkan data vektor\n";
for(int i=0;i<input.banyak;i++){
cout <<"Data["<<i+1<<"]: ";
cin>>input.elemen[i];
}
return in;
}
ostream& operator<<(ostream& out, vektor& output){
for(int i=0;i<output.banyak;i++){
cout <<setw(5)<<output.elemen[i]<<endl;
}
return out;
}
vektor& vektor::matrix_kali_vektor(matrix& Matriks, vektor& Vector){
int i,j;
banyak=Matriks.baris;
if(Matriks.kolom!=Vector.banyak){
cout<<"Tidak Bisa mengalikan matrix dengan vector"<<endl;
banyak=0;
return *this;
}
for(i=0;i<Matriks.baris;i++){
for(j=0;j<Matriks.kolom;j++){
elemen[i] += Matriks.M[i][j]*Vector.elemen[j];
}
}
return *this;
}
int main(){
matrix mtr;
vektor vek, kali;
cout<<"Masukkan data matrix"<<endl;
cin>>mtr;
cout << mtr;
cout<<"Masukkan data vektor"<<endl;
cin>>vek;
cout <<vek;
cout<<"Hasil perkalian : \n";
kali.matrix_kali_vektor(mtr,vek);
cout<<kali;
return 0;
}
0 komentar:
Post a Comment