#include<stdio.h>
#include<conio.h>
#define MAX 100
int i,j;
void NhapMaTran(int a[][MAX], int n)
{
for(int i = 0; i < n; i++)
{
for(int j = 0; j < n; j++)
{
printf("\nNhap vao a[%d][%d] = ", i, j);
scanf("%d", &a[i][j]);
}
}
}
void XuatMaTran(int a[][MAX], int n)
{
for(int i = 0; i < n; i++)
{
for(int j = 0; j < n; j++)
{
printf("%4d",a[i][j]);
}
printf("\n\n");
}
}
void DuyetCacPhanTuTrenDuongCheoChinh(int a[][MAX], int n)
{
for(int i = 0; i < n; i++)
{
printf("%4d",a[i][i]);
}
}
void DuyetCacPhanTuTamGiacTrenDuongCheoChinh(int a[][MAX], int n)
{
for(int i = 0; i < n - 1; i++)
{
for(int j = i + 1; j < n; j++)
{
printf("%4d",a[i][j]);
}
}
}
void DuyetCacPhanTuTamGiacDuoiDuongCheoChinh(int a[][MAX], int n)
{
/*for(int i = 1; i < n; i++)
{
for(int j = 0; j < i; j++)
{
printf("%4d",a[i][j]);
}
}*/
for(int i = 1; i < n; i++)
{
for(int j = i - 1; j >= 0; j--)
{
printf("%4d",a[i][j]);
}
}
}
int DemSoChanTamGiacDuoi(int a[][MAX], int n)
{
int dem = 0;
for(int i = 1; i < n; i++)
{
for(int j = 0; j < i; j++)
{
if(a[i][j] % 2 == 0)
{
dem++;
}
}
}
return dem;
}
void DuyetCacPhanTuTrenDuongCheoPhu(int a[][MAX], int n)
{
for(int i = 0; i < n; i++)
{
printf("%4d",a[i][n - 1 - i]);
}
}
void DuyetCacPhanTuTamGiacTrenDuongCheoPhu(int a[][MAX], int n)
{
for(int i = 0; i < n - 1; i++)
{
for(int j = 0; j < n - 1 - i; j++)
{
printf("%4d",a[i][j]);
}
}
}
void DuyetCacPhanTuTamGiacDuoiDuongCheoPhu(int a[][MAX], int n)
{
for(int i = 1; i < n; i++)
{
for(int j = n - i; j < n; j++)
{
printf("%4d",a[i][j]);
}
}
}
int main()
{
int a[MAX][MAX], n;
do
{
printf("\nNhap vao so dong = so canh: ");
scanf("%d", &n);
if(n < 1 || n > MAX)
{
printf("\nChi so nhap vao khong hop le. Xin kiem tra lai !");
}
}while (n < 1 || n > MAX);
NhapMaTran(a,n);
XuatMaTran(a,n);
printf("\nCac phan tu tren duong cheo chinh la: ");
DuyetCacPhanTuTrenDuongCheoChinh(a,n);
printf("\nCac phan tu tam giac tren duong cheo chinh la: ");
DuyetCacPhanTuTamGiacTrenDuongCheoChinh(a,n);
printf("\nCac phan tu tam giac duoi duong cheo chinh la: ");
DuyetCacPhanTuTamGiacDuoiDuongCheoChinh(a,n);
printf("\nCac phan tu tren duong cheo phu la: ");
DuyetCacPhanTuTrenDuongCheoPhu(a,n);
printf("\nCac phan tu tam giac tren duong cheo phu la: ");
DuyetCacPhanTuTamGiacTrenDuongCheoPhu(a,n);
printf("\nCac phan tu tam giac duoi duong cheo phu la: ");
DuyetCacPhanTuTamGiacDuoiDuongCheoPhu(a,n);
int dem = DemSoChanTamGiacDuoi(a,n);
printf("\nSo luong phan tu chan tam giac duoi duong cheo chinh la %d",dem);
getch();
return 0;
}