0% found this document useful (0 votes)
19 views

BT4

The document contains function definitions for operations on matrices including: - Input and output of matrices - Checking if a matrix is upper/lower triangular, identity, or symmetric - Computing the sum of elements in the upper triangle of a matrix - Finding the sum of prime numbers in a specified row - Inserting a row into a matrix - Sorting rows by descending row sum - Deleting the row and column of the maximum element

Uploaded by

quockhanh6310
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views

BT4

The document contains function definitions for operations on matrices including: - Input and output of matrices - Checking if a matrix is upper/lower triangular, identity, or symmetric - Computing the sum of elements in the upper triangle of a matrix - Finding the sum of prime numbers in a specified row - Inserting a row into a matrix - Sorting rows by descending row sum - Deleting the row and column of the maximum element

Uploaded by

quockhanh6310
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

#include<iostream>

#define COL 100

#include<cmath>

using namespace std;

void Input(int a[][COL],int &n);

void Output(int a[][COL],int n);

bool CheckMaTranTamGiacTren(int a[][COL],int n);

bool CheckMaTranTamGiacDuoi(int a[][COL],int n);

bool CheckMaTranDonVi(int a[][COL],int n);

bool CheckMTDoiXungQuaDuongCheoChinh(int a[][COL],int n);

int sum(int a[][COL],int n);

bool IsPrime(int n);

int SumRowPrime(int a[][COL], int n,int k);

void chenDayVaoDongK(int a[][COL], int n, int b[],int k);

void hoanViDong(float a[][COL], int i, int j);

void DeleteRCMAX(int a[][COL],int &n);

void sapXepTheoTongDongGiamDan(int a[][COL], int n);

int main()

int n;

int a[COL][COL];

Input(a,n);

Output(a,n);

if(CheckMaTranTamGiacTren(a,n))

cout<<"Ma tran tren la ma tran tam giac tren! "<<endl;

else

cout<<"Ma Tran tren khong la ma tran tam giac tren!"<<endl;

if(CheckMaTranTamGiacDuoi(a,n))

cout<<"Ma tran tren la ma tran tam giac duoi! "<<endl;

else

cout<<"Ma Tran tren khong la ma tran tam giac duoi!"<<endl;


if(CheckMaTranDonVi(a,n))

cout<<"Ma tran tren la ma tran don vi! "<<endl;

else

cout<<"Ma Tran tren khong la ma tran don vi!"<<endl;

if(CheckMTDoiXungQuaDuongCheoChinh(a,n))

cout<<"Ma tran tren doi xung qua duong cheo chinh!"<<endl;

else

cout<<"Ma Tran tren khong doi xung qua duong cheo chinh!"<<endl;

cout<<"Tong cua cac phan tu thuoc tam giac tren la: "<<sum(a,n)<<endl;

int k;

do{

cout<<"Xin moi nhap k: ";

cin>> k;

}while(k>=n||n<0);

cout<<"Tong cac so nguyen to o dong thu "<<k<<" la: "<<SumRowPrime(a,n,k)<<endl;

int b[COL];

cout<<"Nhap day can chen: ";

for(int i =0;i<n;i++)

cin>>b[i];

chenDayVaoDongK(a,n,b,k);

cout<<"Ma tran sau khi chen day tren vao dong k= "<<k<<" la: "<<endl;

Output(a,n);

cout<<"Sau khi hoan vi tong cac dong giam tu tren xuong: " <<endl;

sapXepTheoTongDongGiamDan(a,n);

Output(a,n);

DeleteRCMAX(a,n);

cout<<"Ma tran sau khi xoa cot va dong cua phan tu lon nhat la: "<<endl;

Output(a,n);

}
void Input(int a[][COL],int &n)
{

cout<<"Xin moi nhap cap n cua ma tran vuong: ";

cin>>n;

for(int i =0;i<n;i++)

for(int j=0;j<n;j++)

cout<<"Mang a["<<i<<"]["<<j<<"]= ";

cin>>a[i][j];

void Output(int a[][COL],int n)


{

for(int i =0;i<n;i++)

for(int j=0;j<n;j++)

cout<<a[i][j]<<"\t";

cout<<endl;

bool CheckMaTranTamGiacTren(int a[][COL],int n)


{

for(int i =1;i<n;i++)

for(int j=0;j<i;j++)
{

if(a[i][j]!=0)

return false ;

return true;

bool CheckMaTranTamGiacDuoi(int a[][COL],int n)


{

for(int j =1;j<n;j++)

for(int i=0;i<j;i++)

if(a[i][j]!=0)

return false ;

return true;

bool CheckMaTranDonVi(int a[][COL],int n)


{

if(CheckMaTranTamGiacDuoi(a,n)==true&&CheckMaTranTamGiacTren(a,n)==true)

for(int i =0;i<n;i++)

for(int j=0;j<n;j++)

if(i==j&&(a[i][j]==1))

return true;

}
}

return false;

bool CheckMTDoiXungQuaDuongCheoChinh(int a[][COL],int n)


{

for(int i =0;i<n;i++)

for(int j=0;j<n;j++)

if(a[i][j]!=a[j][i])

return false;

return true;

}
int sum(int a[][COL],int n)
{

int tong=0;

for(int i =0;i<n;i++)

for(int j=0;j<n;j++)

if(CheckMaTranTamGiacTren(a,n))

tong+=a[i][j];

return tong;

bool IsPrime(int n)
{

if(n<2)
return false;

for( int i=2;i<=sqrt(n);i++)

if(n%i==0)

return false;

return true;

int SumRowPrime(int a[][COL], int n,int k)


{

int sum=0;

for(int j=0;j<n;j++)

if(IsPrime(a[k][j]))

sum+=a[k][j];

return sum;

**void chenDayVaoDongK(int a[][COL], int n, int b[],int k)


{

for (int i = n - 1; i > k; i--)

for (int j = 0; j < n; j++)

a[i][j] = a[i - 1][j];

for (int j = 0; j < n; j++) {


a[k][j] = b[j % n];

void hoanViDong(int a[][COL], int i, int j)

for (int col = 0; col < COL; col++) {

float temp = a[i][col];

a[i][col] = a[j][col];

a[j][col] = temp;

void sapXepTheoTongDongGiamDan(int a[][COL], int n)


{

int tong[COL] = {0};

for (int i = 0; i < n; i++) {

for (int j = 0; j < n; j++) {

tong[i] += a[i][j];

for (int i = 0; i < n - 1; i++) {

for (int j = i + 1; j < n; j++) {

if (tong[i] < tong[j])

hoanViDong(a, i, j);

int temp = tong[i];

tong[i] = tong[j];

tong[j] = temp;

}
void DeleteRCMAX(int a[][COL],int &n)
{

int max=a[0][0];

int vt1 = -1;

int vt2 = -1;

for(int i =0;i<n;i++)

for(int j=0;j<n;j++)

if(max<a[i][j])

max=a[i][j];

vt1=i;

vt2=j;

for(int i =vt1;i<n-1;i++)

for(int j =0;j<n;j++)

a[i][j]=a[i+1][j];

for(int i=0;i<n-1;i++)

for(int j =vt2;j<n-1;j++)

a[i][j]=a[i][j+1];

}
}

n--;

You might also like