ma trận xoán ốc
#include <stdio.h>
#include <conio.h>
void NhapCotDong(int &cot,int &dong);
void NhapMangThayMaTran(int Mang[],int cot,int dong);
void XuatMangThayMaTran(int Mang[],int cot,int dong);
void SapXepMang1Chieu(int Mang[], int cot, int dong);
void ThietLapMaTranXoanOc(int Mang[],int MaTranXoanOc[][10], int cot, int dong);
void XuatMaTran(int a[][10], int cot,int dong);
void main()
{
int Mang[100];
int cot;
int dong;
NhapCotDong(cot,dong);
NhapMangThayMaTran(Mang,cot,dong); //Nh?p m?ng thay cho ma tr?n
printf("\n\tMa tran vua nhap vao: \n");
XuatMangThayMaTran(Mang,cot,dong); //Xu?t m?ng theo ma tr?n
SapXepMang1Chieu(Mang,cot,dong);
//Dò t?ng v? trí trong ma tr?n theo d?ng xo?n ?c
int MaTranXoanOc[10][10];
ThietLapMaTranXoanOc(Mang,MaTranXoanOc,cot,dong);
printf("\n\tMa tran xoan oc:\n");
XuatMaTran(MaTranXoanOc,cot,dong);
getch();
}
void NhapCotDong(int &cot, int &dong)
{
printf(“Nhap so cot: ”); scanf(“%d”, &cot);
printf(“Nhap so dong: ”); scanf(“%d”, &dong);
}
void NhapMangThayMaTran(int Mang[],int cot,int dong)
{
for (int i=0, stt = 0; i<dong; i++ )
{
for (int j=0; j<cot; j++)
{
printf("\t\t%d/ [%d][%d] = ",stt+1, i, j);
scanf("%d", &Mang[stt]);
stt++;
}
}
}
void XuatMangThayMaTran(int Mang[],int cot,int dong)
{
printf("\t------------------------\n");
for (int i=0,stt = 0; i<dong; i++)
{
printf("\t");
for (int j=0; j<cot; j++)
{
printf("%4d", Mang[stt]);
stt++;
}
printf("\n");
}
printf("\t------------------------\n");
}
void SapXepMang1Chieu(int Mang[], int cot, int dong)
{
int temp;
for (int i=0;i <(cot*dong)-1; i++)
{
for (int j=i+1; j<(cot*dong); j++)
{
if(Mang[i]>Mang[j])
{
temp = Mang[i];
Mang[i]=Mang[j];
Mang[j]=temp;
}
}
}
}
void ThietLapMaTranXoanOc(int Mang[],int MaTranXoanOc[][10], int cot, int dong)
{
int dem = 0;
int i=0, j=0;
int DauTien=0;
int Tru=0;
do
{
i=DauTien;
for (j=i; j< cot - Tru; j++)
{
MaTranXoanOc[i][j] = Mang[dem];
dem++;
}
j--;
for (i++; i< dong - Tru; i++ )
{
MaTranXoanOc[i][j] = Mang[dem];
dem++;
}
i--;
for (j--; j>=Tru; j--)
{
MaTranXoanOc[i][j] = Mang[dem];
dem++;
}
Tru++;
j++;
for (i--; i>=Tru; i--)
{
MaTranXoanOc[i][j] = Mang[dem];
dem++;
}
DauTien++;
} while (dem!=cot*dong);
}
void XuatMaTran(int a[][10], int cot,int dong)
{
printf("\t------------------------\n");
for (int i=0; i<dong; i++)
{
printf("\t");
for (int j=0; j<cot; j++)
{
printf("%4d", a[i][j]);
}
printf("\n");
}
printf("\t------------------------\n");
}