Untitled 2019-12-10 12:24:22
#include <iostream>
#include <stdio.h>
#include <math.h>
using namespace std;
// khai bai bien
int m[9][9],n;
// Ham nhap
void Nhap(){
cout<<"\n 1. Nhap ma tran: ";
cout<<"\n - Nhap kich thuoc ma tran: ";
do{
cout<<"\n n= "; cin>>n;
if(n<=3||n>=10)
cout<<"\n Nhap lai n ! ";
}while (n<=3||n>=10);
cout<<"\n - Nhap ma tran: ";
int i=0,j;
while(i<n){
j=0;
while(j<n){
cout<<"\n m["<<i<<"]["<<j<<"]=";
cin>>m[i][j];
j++;
}
i++;
}
}
// In ma tran
void InMaTran(){
cout<<"\n 2. In ma tran: \n ";
int i=0,j;
while(i<n){
j=0;
while(j<n){
cout<<m[i][j]<<" ";
j++;
}
cout<<"\n ";
i++;
}
}
// Tinh tong
long Tong(){
long t=0;
for(int i=0;i<n;i++)
for(int j=0;j<n;j++)
t+=m[i][j];
return t;
}
// Tim max
int Max_MT(){
int max=m[0][0];
for(int i=1;i<n;i++)
for(int j=1;j<n;j++)
if (max<m[i][j]) max=m[i][j];
return max;
}
// Dem so chan
int Dem_so_chan(int a[9][9]){
int d=0;
for(int i=1;i<n;i++)
for(int j=1;j<n;j++)
if (a[i][j]%2==0) d++;
return d;
}
// Tinh tong tung dong
void Tong_dong(){
int s[9];
// khai tao s
for(int i=0;i<n;i++)
s[i]=0;
// tinh tong tung dong
for(int i=0;i<n;i++)
for(int j=0;j<n;j++)
s[i]=s[i]+m[i][j];
// in tong dong
cout<<"\n 6. Tong tung dong: ";
for(int i=0;i<n;i++)
cout<<"\n - Tong dong thu "<<i<<": "<<s[i];
}
// Tong duong cheo chinh
int Tong_duong_cheo_chinh(){
int t=0;
for(int i=0;i<n;i++)
t=t+m[i][i];
return t;
}
// Kiem tra so nguyen to
int TestSNT(int a)
{
if(a==0||a==1)
return 0;
if(a==3||a==5||a==7)
return 1;
for(int i=2; i<=(int)sqrt(a);i++)
if(a%i==0)
return 0;
return 1;
}
// in vi tri so nguyen to
void In_vi_tri_SNT(){
cout<<"\n 8. In vi tri so nguyen to: ";
for(int i=0;i<n;i++)
for(int j=0;j<n;j++)
if(TestSNT(m[i][j])==1)
cout<<"\n ["<<i<<"]["<<j<<"], ";
}
// ham main
int main() {
Nhap();
InMaTran();
cout<<"\n 3. Tong cac phan tu: "<<Tong();
cout<<"\n 4. Max: "<<Max_MT();
cout<<"\n 5. Dem so chan: "<<Dem_so_chan(m);
Tong_dong();
cout<<"\n 7. Tong duong cheo chinh: "<<Tong_duong_cheo_chinh();
In_vi_tri_SNT();
}
Public Last Update: 2019-12-10 12:24:24