15 May 2012

Program kali matrik skalar

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

class matrix{
    friend ostream& operator<<(ostream&, matrix&);
    friend istream& operator>>(istream&, matrix&);
public :
    void input_matrix();
    void kali_matrix_skalar(const matrix&, int);
private:
    int M[20][20];
    int baris, kolom;
};
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];
        }
    }
}
void matrix::kali_matrix_skalar(const matrix& matrixA, int skalar){
    int i,j;
    baris=matrixA.baris;
    kolom=matrixA.kolom;
    for(i=0;i<baris;i++){
        for(j=0;j<kolom;j++){
            M[i][j]=skalar*matrixA.M[i][j];
           
        }
    }

}
istream& operator>>(istream& in, matrix& input){
    cout<<"Masukkan baris = ";
    in>>input.baris;
    cout<<"Masukkan kolom = ";
    in>>input.kolom;
    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;
}
main(){
    matrix matrixA, kali;
    int skalar;
    cout<<"Masukkan Data Matrix"<<endl;
    cin>>matrixA;
    cout<<"Masukkan Skalar = ";
    cin>>skalar;
    kali.kali_matrix_skalar(matrixA, skalar);   
    cout<<kali;
getch();
return 0;
}

0 komentar:

Post a Comment