15 May 2012

Program matrix_tranpose

#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& transpose(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::transpose(const matriks& matrik){
    int i,j;
    baris=matrik.kolom;
    kolom=matrik.baris;
    for(i=0;i<baris;i++)
        for(j=0;j<kolom;j++){
            A[i][j]=matrik.A[j][i];
        }
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 matrik, T;
cout<< "Masukkan data matriks \n"; cin>>matrik;
cout<<"Data Matrik  :\n"; cout <<matrik;
cout<<"Hasil Transpose : \n";
T.transpose(matrik);
cout << T;
return 0;
}

0 komentar:

Post a Comment