[Functions]D. Liang 5.22 Financial application: computing the mean and standard deviation.c

Description

In business applications, you are often asked to compute the mean and standard deviation of data.
The mean is simply the average of the numbers. The standard deviation is a statist that tells you how tightly all the various data are clustered around the mean in a set of data.
For example, what is the average age of the students in a class? How close are the ages? If all the students are the same age, the deviation is 0.
Write a program that reads data set from input and computes the mean and
standard deviations of these numbers using the following formula:

sum(n) = x1 + x2 + ... + xn
mean(n) = sum(n)/n 
squaresum(n) = x1^2 + x2^2 + ... + xn^2
deviation(n) = sqrt( (squaresum(n) - sum(n)*sum(n)/n) / (n-1) ) 

Input

The first line is a positive number t (0<t<=20). It mean that there are t test cases.
Each test case include two lines. The first line is a positive number n for data numbers in this test case, and the second line is n numbers seperated by one blank.

Output

For each test case, output the mean and standard deviation in seperated by one blank in one line.
You should set the precision of floating-point numbers to 2 and fixed.

Sample Input

2
3
1 2 3
4
4 2 2 4

Sample Output

2.00 1.00
3.00 1.15

The idea:It’s is easy to solve with array.

//   Date:2020/4/3
//   Author:xiezhg5
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main(void) {
	int n;
	scanf("%d",&n);
	int i=1;
	int d;
	for(i=1; i<=n; i++) {
		float sum=0,square=0;
		int j;
		float m[100]= {0};     //定义一个静态数组并初始化
		scanf("%d",&d);
		for(j=0; j<d; j++) {
			scanf("%f",&m[j]); //将用户输入的值存放到数组中
			sum=sum+m[j];      //对输入的数字求和
			square=square+m[j]*m[j];
		}
		float mean=(float)(sum)/(j*1.0);   //计算平均值
		float Devia;
		Devia=(float)sqrt((double)(square-sum*sum/j)/(j-1)); //代入题目给的公式注意类型的转换
		printf("%.2f %.2f\n",mean,Devia);
	}
	return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值