Bài 133 : Viết hàm liệt kê các vị trí mà giá trị tại đó là giá trị âm trong mảng một chiều các số thực
Tải code về máy
#include<stdio.h>
#include<conio.h>
#define MAX 100
#define bool
void nhapmang(float a[MAX],int &n)
{
do{
printf("Nhap vao so phan tu cua mang:n=");
scanf("%d",&n);
if(n<1||n>MAX)
printf("So ban nhap vao khong hop le!Xin ban vui long nhap lai!\n");
else
break;
}while(n<1||n>MAX);
for(int i=0;i<n;i++)
{
printf("Nhap vao a[%d]=",i);
scanf("%f",&a[i]);
}
}
void xuatmang(float a[MAX],int n)
{
printf("\n>>>>>>>>>>>>>>MANG VUA NHAP LA:<<<<<<<<<<<<<<\n");
for(int i=0;i<n;i++)
{
printf("%16f",a[i]);
}
printf("\n");
}
void xulydulieu(float a[MAX],int n)
{
int Co,dem=0,vitri=0;
bool Co=false;
printf("\nTrong mang co cac gia tri am la:");
for(int i=0;i<n;i++)
{
if(a[i]<0)
{
vitri=i;
printf("\n%16f tai vi tri:%d",a[i],vitri);
dem++;
Co=true;
}
}
if(Co==true)
printf("\nTrong mang co %d gia tri am",dem);
else
printf("\nTrong mang khong ton tai gia tri am");
}
void main()
{
float a[MAX];
int n,tieptuc;
quaylai:nhapmang(a,n);
xuatmang(a,n);
xulydulieu(a,n);
printf("\nBan co muon tiep tuc chay chuong trinh khong ? Neu co bam phim C,nguoc lai bam bat ky 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