#include<stdio.h> #define MAX 50 //Chuong trinh tao ma tran vuong bac n //Tinh tong cac phan tu tren duong cheo chinh //Kiem tra ma tran tam giac //Tinh dinh thuc cua ma tran main() { int n,i,j; int A[MAX][MAX]; int sum; char tamGiacTren; int det; do { printf("Bac cua ma tran vuong A:\n"); scanf("%d",&n); } while(n<=0||n>MAX); /*==============NHAP MA TRAN==============*/ printf("\nNhap ma tran vuong A:\n"); for(i=0;i<n;i++) for(j=0;j<n;j++) { printf("A(%d,%d)=",i,j); scanf("%d",&A[i][j]); } /*==============XUAT MA TRAN==============*/ printf("\nMA TRAN A VUA NHAP: \n"); for(i=0;i<n;i++) { for(j=0;j<n;j++) printf("%d\t",A[i][j]); printf("\n"); } /*========TINH TONG CAC PHAN TU TREN DUONG CHEO CHINH========*/ sum = 0; for(i=0;i<n;i++) { sum += A[i][i]; } printf("\nTong cac phan tu tren duong cheo chinh: %d\n",sum); /*========KIEM TRA MA TRAN TAM GIAC TREN========*/ tamGiacTren = 1; //duyet tung phan tu duoi duong cheo chinh //neu phat hien co phan tu khac 0 thi gan 0 cho bien tamGiacTren //sau do thoat khoi vong lap for(i=0;i<n;i++) { for(j=0;j<i;j++) if(A[i][j] !=0) { tamGiacTren = 0; break; //thoat khoi vong lap j } if(tamGiacTren == 0) break; //thoat khoi vong lap i } /*========TINH DINH THUC CUA MA TRAN TAM GIAC TREN========*/ if(tamGiacTren) { printf("\nMa tran A la ma tran tam giac tren"); det = 1; for(i=0;i<n;i++) { det *= A[i][i]; } printf("\nDinh thuc cua ma tran A: %d",det); } else printf("\nMa tran A khong phai la ma tran tam giac tren"); getch(); }
Tags:
lập trình c