ma trận đẹp
#include <stdio.h>
#include <conio.h>
#include <math.h>
#define MAXN 51
int m,n;
int a[MAXN][MAXN];
void nhap(int a[][MAXN],int &m,int &n)
{
int i,j;
printf("Nhap so nguyen duong m: ");
do
{
scanf("%d",&m);
if ((m<2)||(m>50)) printf("Nhap lai: ");
}
while ((m<2)||(m>50));
printf("Nhap so nguyen duong n: ");
do
{
scanf("%d",&n);
if ((n<2)||(n>50)) printf("Nhap lai: ");
}
while ((n<2)||(n>50));
for (i=1;i<=m;i++)
for (j=1;j<=n;j++)
{
printf("Nhap phan tu a[%d][%d]: ",i,j);
do
{
scanf("%d",&a[i][j]);
if ((a[i][j]<1)||(a[i][j]>30000)) printf("Nhap lai: ");
}
while ((a[i][j]<1)||(a[i][j]>30000));
}
}
void in(int a[][MAXN],int m,int n)
{
int i,j;
for (i=1;i<=m;i++)
{
for (j=1;j<=n;j++) printf("%7d",a[i][j]);
printf("
");
}
}
void sapxepdong(int a[][MAXN],int m,int n)
{
int i,j,k;
int tmp;
for (i=1;i<=m;i++)
for (j=1;j<n;j++)
for (k=j+1;k<=n;k++)
if (a[i][j]>a[i][k])
{
tmp=a[i][j];
a[i][j]=a[i][k];
a[i][k]=tmp;
}
}
void sapxepcot(int a[][MAXN],int m,int n)
{
int i,j,k;
int tmp;
for (j=1;j<=n;j++)
for (i=1;i<m;i++)
for (k=i+1;k<=m;k++)
if (a[i][j]<a[k][j])
{
tmp=a[i][j];
a[i][j]=a[k][j];
a[k][j]=tmp;
}
}
main()
{
nhap(a,m,n);
printf("
Ma tran sau khi nhap la:
");
in(a,m,n);
sapxepdong(a,m,n);
sapxepcot(a,m,n);
printf("
Ma tran dep co duoc la:
");
in(a,m,n);
getch();
}
Bạn đang đọc truyện trên: TruyenTop.Vip