Bài 439 : Tính tổng các phần tử thuộc ma trận tam giác dưới (không tính đường chéo) trong ma trận vuông các số thực
#include<stdio.h>
#include<conio.h>
#define MAX 100
void nhapmatranvuong(float a[MAX][MAX],int &m,int &n)
{
do{
printf("Nhap vao so dong cua ma tran:m=");
scanf("%d",&m);
if(m<1||m>MAX)
printf("So dong ban nhap vao khong hop le!Xin vui long nhap lai!\n");
else
break;
}while(m<1||m>MAX);
do{
printf("Nhap vao so cot cua ma tran:n=");
scanf("%d",&n);
if(n!=m)
printf("So cot ban nhap vao khong thoa tinh chat la ma tran vuong (n==m).Xin vui long nhap lai!\n");
else
break;
}while(n!=m);
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
{
printf("Nhap vao a[%d][%d]=",i,j);
scanf("%f",&a[i][j]);
}
}
}
void xuatmatranvuong(float a[MAX][MAX],int m,int n)
{
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
{
printf("%16f",a[i][j]);
}
printf("\n");
}
}
void tongduoiduongcheochinh(float a[MAX][MAX],int m,int n)
{
float tong=0;
for(int i=1;i<m;i++)
{
for(int j=0;j<=i-1;j++)
{
tong+=a[i][j];
}
}
printf("\nTong cac phan tu thuoc ma tran tam giac duoi duong cheo chinh la:%f",tong);
}
void tongduoiduongcheophu(float a[MAX][MAX],int m,int n)
{
float tong=0;
for(int i=1;i<m;i++)
{
for(int j=n-1;j>=n-i;j--)
{
tong+=a[i][j];
}
}
printf("\nTong cac phan tu thuoc ma tran tam giac duoi duong cheo phu la:%f",tong);
}
void main()
{
float a[MAX][MAX];
int n,m,tieptuc;
quaylai:nhapmatranvuong(a,m,n);
printf("\n>>>>>>>>>>>>>>>>>>>>MA TRAN VUA NHAP:<<<<<<<<<<<<<<<<<<<<<\n");
xuatmatranvuong(a,m,n);
tongduoiduongcheochinh(a,m,n);
tongduoiduongcheophu(a,m,n);
printf("\nBan co muon tiep tuc chay chuong trinh hay khong ? Neu co bam phim C,nguoc lai bam bat ky 1 phim nao khac de ket thuc\n");
tieptuc=getch();
if(tieptuc=='c'||tieptuc=='C')
goto quaylai;
}
|
Nhận xét
Đăng nhận xét