Bài 340 : Đếm số lượng số âm trên một cột trong ma trận các số thực
Tải code tại đây
#include<stdio.h>
#include<conio.h>
#define MAX 100
void nhapmatran(float a[MAX][MAX],int &n,int &m)
{
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<1||n>MAX)
printf("So cot ban nhap vao khong hop le!Xin vui long nhap lai!\n");
else
break;
}while(n<1||n>MAX);
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 xuatmatran(float a[MAX][MAX],int n,int m)
{
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
{
printf("%16f",a[i][j]);
}
printf("\n");
}
}
void demgiatriamtrencot(float a[MAX][MAX],int n,int m)
{
int b[MAX]; // mảng b lúc này ta sẽ để dạng số nguyên bởi vì nó sẽ gán giá trị dem vào !
for(int j=0;j<n;j++)
{
int dem=0;
for(int i=0;i<m;i++)
{
if(a[i][j]<0)
{
dem++;
b[j]=dem;
}
}
}
for(int j=0;j<n;j++)
printf("\nSo luong so am co tren cot %d la:%d",j,b[j]);
}
void main()
{
float a[MAX][MAX];
int n,m,tieptuc;
quaylai:nhapmatran(a,n,m);
printf("\n>>>>>>>>>>>>>>>MA TRAN VUA NHAP LA:<<<<<<<<<<<<<<<<<<<<<\n");
xuatmatran(a,n,m);
demgiatriamtrencot(a,n,m);
printf("\nBan co muon tiep tuc chay chuong trinh 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