[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;
}
发布了165 篇原创文章 · 获赞 124 · 访问量 5625

猜你喜欢

转载自blog.csdn.net/qq_45645641/article/details/105288560
今日推荐