Chào các bạn! Vì nhiều lý do từ nay Truyen2U chính thức đổi tên là Truyen247.Pro. Mong các bạn tiếp tục ủng hộ truy cập tên miền mới này nhé! Mãi yêu... ♥

bai4_matran

#include "stdio.h"

#include "conio.h"

#include "iostream.h"

#include "iomanip.h"

#include "math.h"

class matran

    {

    private:

             int a[50][50];

             int n,m;

        public:

             void nhap();

             void xuat();

             int tongchinh();

             int tongphu();

             void sx_tangcot();

             void sx_chinh();

             friend void tongmt(matran mt1,matran mt2,matran &S,int &kt);

             friend void tichmt(matran mt1,matran mt2,matran &T,int &kt);

    };

void matran::nhap()

    {

        int i,j;

        cout<<"

Nhap so hang n=";cin>>n;

        cout<<"

Nhap so cot m=";cin>>m;

        for(i=1;i<=n;i++)

            for(j=1;j<=m;j++)

                {

                     cout<<"a["<<i<<"]["<<j<<"]=";

                     cin>>a[i][j];

                }

    }

void matran::xuat()

    {

        int i,j;

        for(i=1;i<=n;i++)

        {

            cout<<"

";

            for(j=1;j<=m;j++)

            cout<<setw(4)<<a[i][j];

        }

    }

int matran::tongchinh()

    {

        int i,S;

        S=0;

        for(i=1;i<=n;i++)

            S+=a[i][i];

        return S;

    }

int matran::tongphu()

    {

        int i,S;

        S=0;

        for(i=1;i<=n;i++)

            S+=a[i][n-i+1];

        return S;

    }

void matran::sx_tangcot()

    {

        int i,j,k,tg;

        for(j=1;j<=m;j++)

            for(i=1;i<n;i++)

                for(k=i+1;k<=n;k++)

                    if(a[i][j]>a[k][j])

                           {tg=a[i][j];a[i][j]=a[k][j];a[k][j]=tg;}

    }

void matran::sx_chinh()

    {

        int i,j,tg;

        for(i=1;i<n;i++)

            for(j=i+1;j<=n;j++)

                if(a[i][i]>a[j][j])

                {tg=a[i][i];a[i][i]=a[j][j];a[j][j]=tg;}

    }

void tongmt(matran mt1,matran mt2,matran &S,int &kt)

    {

        int i,j;

        if(mt1.n==mt2.n&&mt1.m==mt2.m)

            {

                kt=1;

                for(i=1;i<=mt1.n;i++)

                    for(j=1;j<=mt1.m;j++)

                        S.a[i][j]=mt1.a[i][j]+mt2.a[i][j];

                S.n=mt1.n;S.m=mt1.m;

            }

        else

            kt=0;

    }

void tichmt(matran mt1,matran mt2,matran &T,int &kt)

    {

        int i,j,k;

        if(mt1.m==mt2.n)

           {

               kt=1;

                for(i=1;i<=mt1.n;i++)

                    for(j=1;j<=mt2.m;j++)

                    {

                T.a[i][j]=0;

                        for(k=1;k<=mt1.m;k++)

                            T.a[i][j]+=mt1.a[i][k]*mt2.a[k][j];

                    }

                T.n=mt1.n;T.m=mt2.m;

           }

        else

            kt=0;

    }

void main()

    {

        matran A,B,T,S;

        int kt,kt1;

        clrscr();

        cout<<"

Nhap ma tran A:

";

        A.nhap();

        cout<<"

Ma tran A la:

";

        A.xuat();

        cout<<"

Tong cac phan tu tren duong cheo chinh S="<<A.tongchinh();

        cout<<"

Tong cac phan tu tren phu cheo chinh S="<<A.tongphu();

        A.sx_tangcot();

        cout<<"

Sap xep ma tran tang dan theo cot:

";

        A.xuat();

        cout<<"

Nhap ma tran B:

";

        B.nhap();

        cout<<"

Ma tran B la:

";

        B.xuat();

        B.sx_chinh();

        cout<<"

Sap xep ma tran B tang dan tren duong cheo chinh:

";

        B.xuat();

        tongmt(A,B,S,kt);

        if(kt==1)

            {

                cout<<"

Tong hai ma tran la:

";

                S.xuat();

            }

        else

            cout<<"

Khong tinh duoc tong:

";

        tichmt(A,B,T,kt1);

        if(kt1==1)

            {

                cout<<"

Tich hai ma tran la:

";

                T.xuat();

            }

        else

            cout<<"

Khong tinh duoc tich:

";

        getch();

    }

Bạn đang đọc truyện trên: Truyen247.Pro

Tags: #123