1.从键盘输入八个实数,并保存到数组中,计算并输出算术平均数vc++
发布网友
发布时间:2022-04-29 21:01
我来回答
共1个回答
热心网友
时间:2023-10-09 03:30
全部任务写在一个程序里了。
#include <stdio.h>
void task1(){
double a[8],s=0; int i;
printf("input 8 values\n");
for (i=0;i<8;i++){ scanf("%lf",&a[i]);s=s+a[i];};
s = s/8;
printf("mean=%lf\n",s);
}
int task2(int x){ if (x>=0) return x; else return -x;}
int task3(int n){int i,sum=0; for (i=1;i<=n;i++) sum=sum+i; return sum;}
int main(){
int x1=123,x2=-123,n;
int a[10],i,ib=0,is=0,i0=0;
printf("\n---1---\n"); task1();
printf("\n---2---\n"); printf("abs x1=%d, abs x2=%d\n",task2(x1),task2(x2));
printf("\n---3---\n"); printf("input n\n");scanf("%d",&n); printf("sum=%d\n",task3(n));
printf("\n---4---\n"); printf("input 10 int data\n");
for (i=0;i<10;i++) scanf("%d",&a[i]);
for (i=0;i<10;i++) {if (a[i]>0)ib++; else if (a[i]<0) is++; else i0++;};
printf("positive=%d negtive=%d zero=%d\n",ib,is,i0);
printf("\n---5---\n");
printf("input 10 int data\n"); for (i=0;i<10;i++) scanf("%d",&a[i]);
ib=0; for (i=0;i<10;i++)if (a[i]>a[ib]) ib=i;
for (i=0;i<10;i++) printf("%d ",a[i]);
printf("max value at a[%d],value=%d\n",ib,a[ib]);
return 0;
}