#include <conio.h>
#include <stdlib.h>
int main()
{
int chon,n,i,j;
float *p;
float tb,temp,tong,max,min;
do {
printf("\n\t----MENU----");
printf("\n1. NHAP");
printf("\n2. XAP XEP TANG DAN VA HIEN THI");
printf("\n3. TIM TONG");
printf("\n4. TIM TRUNG BINH");
printf("\n5. TIM MIN,MAX");
printf("\n6. THOAT");
printf("\nmoi ban chon tu 1-6: ");
scanf("%d",&chon);
switch(chon)
{
case 1:
printf("\nNhap so phan tu: ");
scanf("%d",&n);
p=(float*)malloc(n*sizeof(float));
for(i=0; i<n; i++)
{
printf("\nNhap phan tu %d: ",i+1);
scanf("%f",(p+i));
}
getch();
break;
case 2:
for(i=0;i<n-1;i++)
for(j=i+1;j<n;j++)
if(*(p+i)>*(p+j))
{
temp=*(p+i);
*(p+i)=*(p+j);
*(p+j)=temp;
}
printf("\nxap xep theo thu tu tang dan: ");
for(i=0;i<n;i++)
{
printf("%5.2f",*(p+i));
}
getch();
break;
case 3:
tong=0;
for(i=0;i<n;i++)
{
tong=tong+p[i];
}
printf("\ntong=%.2f ",tong);
getch();
break;
case 4:
tong=0;
for(i=0;i<n;i++)
{
tong+=p[i];
}
tb=(float)tong/n;
printf("\ngia tri trung binh la: %.2f",tb);
getch();
break;
case 5:
min=p[0];
for (i=1;i<n;i++)
{
if (min>p[i])
min=p[i];
}
printf("\ngia tri min: %.2f",min);
printf("\n");
max=p[0];
for (i=1;i<n;i++)
{
if (max<p[i])
max=p[i];
}
printf("\ngia tri max: %.2f",max);
getch();
break;
case 6:
break;
default:
printf("\nBan chon SAI,moi chon lai");
getch();
break;
}
} while(chon!=6);
}
123
Trả lờiXóa