[Lập trình C] Tổng hợp mảng 2 chiều bằng hàm
#include"stdio.h"
#define SIZE 10
void nhapMang2chieu(int a[SIZE][SIZE],int m,int n);
void xuatMang2chieu(int a[SIZE][SIZE],int m,int n);
int tinhTong2chieu(int a[SIZE][SIZE],int m, int n);
int tinhTongchan(int a[SIZE][SIZE],int m,int n);
int tinhTongle(int a[SIZE][SIZE],int m,int n);
int tinhTongam(int a[SIZE][SIZE],int m,int n);
int tinhTongduong(int a[SIZE][SIZE],int m,int n);
int tinhTongSNT(int a[SIZE][SIZE],int m,int n);
int demsolanxuathien(int a[SIZE][SIZE],int m,int n);
int kiemtraSNT(int a[SIZE][SIZE],int m, int n);
int demSNT(int a[SIZE][SIZE],int m, int n);
int main()
{
int b[10][10];
int m=3,n=3,x=3;
int kq;
nhapMang2chieu(b,m,n);
printf("Mang vua nhap la:");
xuatMang2chieu(b,m,n);
kq=tinhTong2chieu(b,m,n);
printf("\nTong la: %d",kq);
kq=tinhTongchan(b,m,n);
printf("\nTong chan la: %d",kq);
kq=tinhTongle(b,m,n);
printf("\nTong le la: %d",kq);
kq=tinhTongam(b,m,n);
printf("\nTong am la: %d",kq);
kq=tinhTongduong(b,m,n);
printf("\nTong duong la: %d",kq);
kq=tinhTongSNT(b,m,n);
printf("\nTong SNT la: %d",kq);
kq=demsolanxuathien(b,m,n);
printf("\nSo %d xuat hien %d lan",x,kq);
kq=kiemtraSNT(b,m,n);
if(kq==1)
{
printf("\nTrong mang ton tai so nguyen to",kq);
}
else
{
printf("\nTrong mang khong ton tai so nguyen to",kq);
}
kq=demSNT(b,m,n);
if(kq==0)
{
printf("Khong ton tai",kq);
}
else
{
printf("\nCo %d SNT",kq);
}
return 0;
}
//Xuat
void xuatMang2chieu(int a[SIZE][SIZE],int m,int n)
{
int i=0;
while(i<=n-1)
{
int j=0;
while(j<=n-1)
{
printf("%d ",a[i][j]);
j++;
}
i++;
}
}
//Nhap
void nhapMang2chieu(int a[SIZE][SIZE],int m,int n)
{
int i=0;
while(i<=m-1)
{
int j=0;
while(j<=n-1)
{
printf ("Nhap mang a[%d][%d]: ",i,j);
scanf("%d",&a[i][j]);
j++;
}
i++;
}
}
//Tinh tong
int tinhTong2chieu(int a[SIZE][SIZE],int m,int n)
{
int i=0,tong=0;
while(i<=m-1)
{
int j=0;
while(j<=n-1)
{
tong=tong+a[i][j];
j++;
}
i++;
}
return tong;
}
//Tinh tong chan
int tinhTongchan(int a[SIZE][SIZE],int m,int n)
{
int i=0,tong=0;
while(i<=m-1)
{
int j=0;
while(j<=n-1)
{
if(a[i][j]%2==0)
tong=tong+a[i][j];
j++;
}
i++;
}
return tong;
}
//Tinh tong le
int tinhTongle(int a[SIZE][SIZE],int m,int n)
{
int i=0,tong=0;
while(i<=m-1)
{
int j=0;
while(j<=n-1)
{
if(a[i][j]%2!=0)
tong=tong+a[i][j];
j++;
}
i++;
}
return tong;
}
//Tinh tong am
int tinhTongam(int a[SIZE][SIZE],int m,int n)
{
int i=0,tong=0;
while(i<=m-1)
{
int j=0;
while(j<=n-1)
{
if(a[i][j]<0)
tong=tong+a[i][j];
j++;
}
i++;
}
return tong;
}
//Tinh duong
int tinhTongduong(int a[SIZE][SIZE],int m,int n)
{
int i=0,tong=0;
while(i<=m-1)
{
int j=0;
while(j<=n-1)
{
if(a[i][j]>0)
tong=tong+a[i][j];
j++;
}
i++;
}
return tong;
}
//Tinh tong SNT
int tinhTongSNT(int a[SIZE][SIZE],int m,int n)
{
int i=0,tong=0;
while(i<=m-1)
{
int j=0;
while(j<=n-1)
{
int k=1,dem=0;
while(k<=a[i][j])
{
if(a[i][j]%k==0)
{
dem++;
}
k++;
}
if(dem==2)
{
tong=tong+a[i][j];
}
j++;
}
i++;
}
return tong;
}
//Dem so lan xuat hien
int demsolanxuathien(int a[SIZE][SIZE],int m,int n)
{
int i=0,lan=0,x=3;
while(i<=m-1)
{
int j=0;
while(j<=n-1)
{
if(a[i][j]==x)
{
lan++;
}
j++;
}
i++;
}
return lan;
}
//Kiem tra SNT
int kiemtraSNT(int a[SIZE][SIZE],int m, int n)
{
int i=0;
while(i<=m-1)
{
int j=0;
while(j<=n-1)
{
int k=1,dem=0;
while(k<=a[i][j])
{
if(a[i][j]%k==0)
{
dem++;
}
k++;
}
if(dem==2)
{
return 1;
}
j++;
}
i++;
}
return 0;
}
//Dem so nguyen to
int demSNT(int a[SIZE][SIZE],int m, int n)
{
int i=0,dem=0;
while(i<=m-1)
{
int j=0;
while(j<=n-1)
{
int k=1,d=0;
while(k<=a[i][j])
{
if(a[i][j]%k==0)
{
d++;
}
k++;
}
if(d==2)
{
dem++;
}
j++;
}
i++;
}
return dem;
}
Tut By: Thanh Liêm
#define SIZE 10
void nhapMang2chieu(int a[SIZE][SIZE],int m,int n);
void xuatMang2chieu(int a[SIZE][SIZE],int m,int n);
int tinhTong2chieu(int a[SIZE][SIZE],int m, int n);
int tinhTongchan(int a[SIZE][SIZE],int m,int n);
int tinhTongle(int a[SIZE][SIZE],int m,int n);
int tinhTongam(int a[SIZE][SIZE],int m,int n);
int tinhTongduong(int a[SIZE][SIZE],int m,int n);
int tinhTongSNT(int a[SIZE][SIZE],int m,int n);
int demsolanxuathien(int a[SIZE][SIZE],int m,int n);
int kiemtraSNT(int a[SIZE][SIZE],int m, int n);
int demSNT(int a[SIZE][SIZE],int m, int n);
int main()
{
int b[10][10];
int m=3,n=3,x=3;
int kq;
nhapMang2chieu(b,m,n);
printf("Mang vua nhap la:");
xuatMang2chieu(b,m,n);
kq=tinhTong2chieu(b,m,n);
printf("\nTong la: %d",kq);
kq=tinhTongchan(b,m,n);
printf("\nTong chan la: %d",kq);
kq=tinhTongle(b,m,n);
printf("\nTong le la: %d",kq);
kq=tinhTongam(b,m,n);
printf("\nTong am la: %d",kq);
kq=tinhTongduong(b,m,n);
printf("\nTong duong la: %d",kq);
kq=tinhTongSNT(b,m,n);
printf("\nTong SNT la: %d",kq);
kq=demsolanxuathien(b,m,n);
printf("\nSo %d xuat hien %d lan",x,kq);
kq=kiemtraSNT(b,m,n);
if(kq==1)
{
printf("\nTrong mang ton tai so nguyen to",kq);
}
else
{
printf("\nTrong mang khong ton tai so nguyen to",kq);
}
kq=demSNT(b,m,n);
if(kq==0)
{
printf("Khong ton tai",kq);
}
else
{
printf("\nCo %d SNT",kq);
}
return 0;
}
//Xuat
void xuatMang2chieu(int a[SIZE][SIZE],int m,int n)
{
int i=0;
while(i<=n-1)
{
int j=0;
while(j<=n-1)
{
printf("%d ",a[i][j]);
j++;
}
i++;
}
}
//Nhap
void nhapMang2chieu(int a[SIZE][SIZE],int m,int n)
{
int i=0;
while(i<=m-1)
{
int j=0;
while(j<=n-1)
{
printf ("Nhap mang a[%d][%d]: ",i,j);
scanf("%d",&a[i][j]);
j++;
}
i++;
}
}
//Tinh tong
int tinhTong2chieu(int a[SIZE][SIZE],int m,int n)
{
int i=0,tong=0;
while(i<=m-1)
{
int j=0;
while(j<=n-1)
{
tong=tong+a[i][j];
j++;
}
i++;
}
return tong;
}
//Tinh tong chan
int tinhTongchan(int a[SIZE][SIZE],int m,int n)
{
int i=0,tong=0;
while(i<=m-1)
{
int j=0;
while(j<=n-1)
{
if(a[i][j]%2==0)
tong=tong+a[i][j];
j++;
}
i++;
}
return tong;
}
//Tinh tong le
int tinhTongle(int a[SIZE][SIZE],int m,int n)
{
int i=0,tong=0;
while(i<=m-1)
{
int j=0;
while(j<=n-1)
{
if(a[i][j]%2!=0)
tong=tong+a[i][j];
j++;
}
i++;
}
return tong;
}
//Tinh tong am
int tinhTongam(int a[SIZE][SIZE],int m,int n)
{
int i=0,tong=0;
while(i<=m-1)
{
int j=0;
while(j<=n-1)
{
if(a[i][j]<0)
tong=tong+a[i][j];
j++;
}
i++;
}
return tong;
}
//Tinh duong
int tinhTongduong(int a[SIZE][SIZE],int m,int n)
{
int i=0,tong=0;
while(i<=m-1)
{
int j=0;
while(j<=n-1)
{
if(a[i][j]>0)
tong=tong+a[i][j];
j++;
}
i++;
}
return tong;
}
//Tinh tong SNT
int tinhTongSNT(int a[SIZE][SIZE],int m,int n)
{
int i=0,tong=0;
while(i<=m-1)
{
int j=0;
while(j<=n-1)
{
int k=1,dem=0;
while(k<=a[i][j])
{
if(a[i][j]%k==0)
{
dem++;
}
k++;
}
if(dem==2)
{
tong=tong+a[i][j];
}
j++;
}
i++;
}
return tong;
}
//Dem so lan xuat hien
int demsolanxuathien(int a[SIZE][SIZE],int m,int n)
{
int i=0,lan=0,x=3;
while(i<=m-1)
{
int j=0;
while(j<=n-1)
{
if(a[i][j]==x)
{
lan++;
}
j++;
}
i++;
}
return lan;
}
//Kiem tra SNT
int kiemtraSNT(int a[SIZE][SIZE],int m, int n)
{
int i=0;
while(i<=m-1)
{
int j=0;
while(j<=n-1)
{
int k=1,dem=0;
while(k<=a[i][j])
{
if(a[i][j]%k==0)
{
dem++;
}
k++;
}
if(dem==2)
{
return 1;
}
j++;
}
i++;
}
return 0;
}
//Dem so nguyen to
int demSNT(int a[SIZE][SIZE],int m, int n)
{
int i=0,dem=0;
while(i<=m-1)
{
int j=0;
while(j<=n-1)
{
int k=1,d=0;
while(k<=a[i][j])
{
if(a[i][j]%k==0)
{
d++;
}
k++;
}
if(d==2)
{
dem++;
}
j++;
}
i++;
}
return dem;
}
Tut By: Thanh Liêm
THE END
Đánh giá hôm nay: {[['']]}
[Lập trình C] Tổng hợp mảng 2 chiều bằng hàm
Reviewed by
ADMIN
on
January 05, 2020
Rating:
No comments :
Một số lưu ý khi bình luận
Mọi bình luận sai nội quy sẽ bị xóa mà không cần báo trước (xem nội quy)
Bấm Thông báo cho tôi bên dưới khung bình luận để nhận thông báo khi admin trả lời
Để bình luận một đoạn code, hãy mã hóa code trước nhé