#include<conio.h>
#include<stdio.h>
#define MAX 100
// Ma trận zic zac ngang
void XuatMaTran(int a[][MAX], int d, int c)
{
for (int i = 0; i < d ; i++)
{
for (int j = 0; j < c; j++)
{
printf("%4d", a[i][j]);
}
printf("\n\n");
}
}
void TaoMaTranZicZacNgang(int a[][MAX],int dong, int cot)
{
int dem=1;
for(int i = 0; i < dong; i++)
{
if(i % 2 == 0)
for(int j = 0; j < cot; j++)
a[i][j] = dem++;
else
for(int j = cot-1; j >= 0; j--)
a[i][j] = dem++;
}
}
int main()
{
int a[MAX][MAX];
int dong, cot;
do
{
printf("\nNhap vao so dong: ");
scanf("%d", &dong);
if (dong < 0 || dong > MAX)
{
printf("\nChi so dong khong hop le. Xin kiem tra lai !");
}
} while (dong < 0 || dong > MAX);
do
{
printf("\nNhap vao so cot: ");
scanf("%d", &cot);
if (cot < 0 || cot > MAX)
{
printf("\nChi so cot khong hop le. Xin kiem tra lai !");
}
} while (cot < 0 || cot > MAX);
printf("\nMa tran xoan oc: \n");
TaoMaTranZicZacNgang(a,dong,cot);
XuatMaTran(a,dong,cot);
getch();
return 0;
}