Bài Tập lớn Môn C/C++ phần Ma Trận

HuanBuiThanh
Ma trận trong C/C++
#include <stdio.h>
#include <conio.h>
#include <math.h>
void nhapmt(int a[9][9],int m , int n)
{ for ( int i=0;i<m;i++)
 for (int j=0;j<n;j++)
      { printf("\ nhap a[%d]:[%d]",i,j);
 scanf("%d,%d",&a[i][j]);
 }
}
void xuat_mt(int a[9][9],int m , int n)
{ for (int i=0;i<m;i++)
{printf("\n");
 for (int j=0;j<n;j++)
 { printf("%d  ",a[i][j]);
    // printf("\n");
 }
  }
}
// max tren toan ma tran
void maxmatran(int a[9][9],int m , int n)
 {        int max;
 max= a[0][0];
  for (int i=0;i<m;i++)
  {
   for (int j=0;j<n;j++)
    {
    if( max < a[i][j]  )
    max=a[i][j];
   }
  }
 printf("\n phan tu lon nhat tren ma tran la %d : ",max);
 }
//max theo tung hang
void max_hang(int a[9][9],int m , int n)
{
 int maxhang,max;
  for (int i=0;i<m;i++)
  { max=a[i][0];
   for (int j=0;j<n;j++)
    {
    if( max < a[i][j]  )
    max=a[i][j];
   }
  printf(" max cua hang a[%d][0] la : ",i);
  printf("%d",max);
  printf("\n");
  }

}
//max theo tung cot
void max_cot(int a[9][9],int m , int n)
{
 int maxcot,max;
  for (int j=0;j<m;j++)
  { max=a[0][j];
   for (int i=0;i<n;i++)
    {
    if( max < a[i][j]  )
    max=a[i][j];
   }
  printf(" max cua cot a[%d][0] la : ",i);
  printf("%d",max);
  printf("\n");
  }

}
//chuyen vi ma tran
void chuyen_vi(int a[9][9],int e[9][9],int m , int n)
 {
    int i,j;   
    for (i=0;i<n;i++) 
        for (j=0;j<m;j++)
            {a[i][j]=e[j][i];
   }
}
// tim so nguyen to tren toan ma tran
int snt(int a)
{
    if (a<=1) return 0;
    for (int i=2;i<a;i++)
        if (a%i==0) return 0;
 else
    return 1;   
}
//tich ma tran
void tich_mt(int c[9][9],int a[9][9],int b[9][9],int m , int n)
{ int k;
for(int i=0;i<m;i++)
 for(int j=0;j<n;j++)
 {
 c[i][j]=0;
 for (k=0;k<m;k++)
  c[i][j]=c[i][j]+a[i][k]*b[k][j];
 }
}
// cong 2 ma tran
void cong_mt(int d[9][9],int a[9][9],int b[9][9],int m , int n)
{
 int i, j;
 for (i=1;i<=m;i++)
  for (j=1;j<=n;j++)
  {
  d[i][j]=0;
  d[i][j]=a[i][j]+b[i][j];
  }
}
//so hoan hao
/*bool SoHoanHao(int n)
{
    int temp=0;
    for (int i=1;i<n;i++)
  for (int j=0;j<m;j++)
        if (a[i][j]%i==0)
            temp=temp+i;
    if (temp==n)
        return true;
    else
        return false;
}
*/
//chuong trinh chinh


void main()
{
clrscr();
int a[9][9];
int a2[9][9];
int c[9][9];
int d[9][9];
int e[9][9];
int m,n,m2,n2;
 int i,j;
 int tong=0;
printf("\n nhap so hang : ");
scanf("%d",&m);
printf("\n nhap so cot : ");
scanf("%d",&n);
nhapmt(a,m,n);
printf("\n da nhap xong , bam enter de xem ket qua ");
getch();
printf("\n ma tran ban vua nhap la : ");
xuat_mt(a,m,n);
printf("\n");
maxmatran(a,m,n);
printf("\n");
max_hang(a,m,n);
max_cot(a,m,n);
//in ra cac so nguyen to trong ma tran
printf("\n \n cac so nguyen to trong mang va vi tri cua chung \n");
    for (i=1;i<=n;i++)
 for (j=1;j<=m;j++)
     if (snt(a[i][j])==1)
  printf("\n %d - a[%d][%d]",a[i][j],i,j);
printf("\n bam enter de sap xep");
getch();
// sap xep lai ma tran
 //sap xep theo hang
    for(i=0;i<m;i++)
        for(j=0;j<n-1;j++)
            for(int k=j+1;k<n;k++)
                if (a[i][j]>a[i][k])
                {
                    int tam=a[i][j];
                    a[i][j]=a[i][k];
                    a[i][k]=tam;
                }
        // sap xep theo cot
    for(i=0;i<n;i++)
        for(j=0;j<m-1;j++)
            for( k=j+1;k<m;k++)
                if (a[j][i]>a[k][i])
                {
                    int tam=a[j][i];
                    a[j][i]=a[k][i];
                    a[k][i]=tam;
                }
    printf("\n ma tran sau khi sap xep tang dan la :\n");
    for(i=0;i<m;i++)
    {
        for(j=0;j<n;j++)
            printf("%d\t",a[i][j]);
        printf("\n");
    }
printf("\n ma tran chuyen vi");
//ma tran chuyen vi
chuyen_vi(a,e,m,n);
xuat_mt(e,m,n);
printf("\n bam enter de xem tong cac phan tu tren duong cheo chinh ");
getch();
//tinh tong duong cheo chinh
for (i=0;i<n;i++)
tong=tong+a[i][i];
 printf("\n Tong cac phan tu tren duong cheo la: %d",tong);
 getch();
 printf("\n nhap them ma tran 2 de tinh tiep :");
//nhap mt a2
printf("\n nhap so hang ma tran 2 : ");
scanf("%d",&m2);
printf("\n nhap so cot ma tran 2 : ");
scanf("%d",&n2);
nhapmt(a2,m2,n2);
printf("\n ma tran ban vua nhap la : ");
xuat_mt(a2,m2,n2);
getch();
printf("\n Tong cua 2 ma tran ");
//cong 2 ma tran
cong_mt(d,a,a2,m,n);
xuat_mt(d,m,n);
printf("\n bam enter de tinh tich 2 ma tran");
getch();
//tich 2 ma tran
tich_mt(c,a,a2,m,n);
xuat_mt(c,m,n);//xuat ra ma tran c
printf("\n bam enter de ket thuc chuong trinh");
getch();
//ket thuc chuowng trinh
getch();

Getting Info...

Đăng nhận xét

Cảm ơn bạn đã quan tâm và gửi nhận xét tại
http://huanbuithanh.blogspot.com
hãy ghé thăm blog hàng ngày để được cập nhật những thủ thuật mới nhất nhé . thân ái !
Cookie Consent
We serve cookies on this site to analyze traffic, remember your preferences, and optimize your experience.
Oops!
It seems there is something wrong with your internet connection. Please connect to the internet and start browsing again.
AdBlock Detected!
We have detected that you are using adblocking plugin in your browser.
The revenue we earn by the advertisements is used to manage this website, we request you to whitelist our website in your adblocking plugin.
Site is Blocked
Sorry! This site is not available in your country.