[codes=c]#include "conio.h"
#include "stdio.h"
void nhap_ma_tran(int a[][10], int &d, int &c)
{
for (int i = 0; i<d; i++)
for (int j = 0; j<c; j++)
{
printf("Nhap cac phan tu: a[%d][%d]",i, j);
scanf("%d", &a[i][j]);
}
}
void xuat_ma_tran(int a[][10], int &d, int &c)
{
for (int i = 0; i<d; i++)
{
for (int j = 0; j<c; j++)
printf("%4d",a[i][j]);
printf("\n");
}
}
long tong_ma_tran(int a[][10], int &d, int &c)
{
long s = 0;
for (int i = 0; i<d; i++)
for (int j = 0; j<c; j++)
s = s + a[i][j];
return s;
}
long tich_ma_tran(int a[][10], int &d, int &c)
{
long t = 1;
for (int i = 0; i<d; i++)
for (int j = 0; j<c; j++)
t = t * a[i][j];
return t;
}
float trungbinhcong_ma_tran(int a[][10], int &d, int &c)
{
float trung_binh_cong, tong = 0;
int dem = 0;
for (int i = 0; i<d; i++)
for (int j = 0; j<c; j++)
{
if(a[i][j]>0)
{
tong = tong + a[i][j];
dem++;
}
}
if (dem>0)
{
trung_binh_cong = tong/dem;
}
else
trung_binh_cong = 0;
return trung_binh_cong;
}
void phan_tu_dong_chan(int a[][10], int d, int c)
{
for (int i = 0; i<d; i+=2)
{
for (int j = 0; j<c; j++)
if (i%2==0)
printf("%4d",a[i][j]);
printf("\n");
}
}
void phan_tu_dong_le(int a[][10], int d, int c)
{
for (int i = 0; i<d; i+=2)
{
for (int j = 0; j<c; j++)
if (j%2==0)
printf("%4d",a[i][j]);
printf("\n");
}
}
void main()
{
int a[10][10],d, c;
long tong = 0;
long tich = 1;
float tbc;
printf("NHap vao so dong: ");
scanf("%d", &d);
printf("NHap vao so cot: ");
scanf("%d", &c);
nhap_ma_tran(a, d, c);
printf("\n");
printf("Ma tran vua nhap co dang: ");
printf("\n");
xuat_ma_tran(a, d, c);
tong = tong_ma_tran(a, d, c);
tich = tich_ma_tran(a, d, c);
tbc = trungbinhcong_ma_tran(a, d, c);
printf("Tong la: %ld", tong);
printf("\n");
printf("Tich la: %ld", tich);
printf("\n");
printf("Trung binh cong la: %f", tbc);
printf("\n");
printf("Phan tu trong ma tran nam tren dong chan:");
printf("\n");
phan_tu_dong_chan(a, d, c);
printf("Phan tu trong ma tran nam tren dong le:");
printf("\n");
phan_tu_dong_le(a, d, c);
getch();
}
[/codes]
Last modified on March 31st, 2021 at 1:26 am
Nam Le
lequocnam
theo minh thay cai trung binh cong ban co the tinh gia tri dem = so dong x so cot thi se de lap trinh hon va bai toan sede hon va ngan hon [emot]grin[/emot][emot]ok[/emot]em chi gop y kien thui may bac chem nhe tay ti [emot]kill[/emot][emot]cry[/emot]
hi. lâu òi mới vào đọc lại, [emot]ok[/emot]