怎么将求随机数组(个数2-10)的平均数,方差,最大值
如何将求随机数组(个数2-10)的平均数,方差,最大值
小白遇上一个问题
现在需要自动生成一个随机数组,个数范围是2-10个。我用了
time_t t;
int a,i,n;
srand((unsigned)time(&t));
a = rand() % 9 + 2;
来输出数组数字个数。
然后要求每个数字的范围是0.001-1000000,这里我这样写的
for(i = 0; i < a ; ++i)
{
num = rand() % 1000000 + (float)(rand() % 1001) * 0.001f ;
printf("%.3f Ω\n",num);
}
现在的问题是,数字得到了,怎么用返回的这些数字来计算他们的均值和方差呢?
现在的代码
------解决思路----------------------
随机数放到数组里,计算均值,方差就是数学问题了
------解决思路----------------------
小白遇上一个问题
现在需要自动生成一个随机数组,个数范围是2-10个。我用了
time_t t;
int a,i,n;
srand((unsigned)time(&t));
a = rand() % 9 + 2;
来输出数组数字个数。
然后要求每个数字的范围是0.001-1000000,这里我这样写的
for(i = 0; i < a ; ++i)
{
num = rand() % 1000000 + (float)(rand() % 1001) * 0.001f ;
printf("%.3f Ω\n",num);
}
现在的问题是,数字得到了,怎么用返回的这些数字来计算他们的均值和方差呢?
现在的代码
#include "stdafx.h"
#include<stdlib.h>
#include <time.h>
int _tmain(int argc, _TCHAR* argv[])
{
time_t t;
int a,i,n;
float num, max;
float arr[10];
srand((unsigned)time(&t));
a = rand() % 9 + 2;
printf("The registor values are:\n");
for(i = 0; i < a ; ++i)
{
num = rand() % 1000000 + (float)(rand() % 1001) * 0.001f ;
printf("%.3f Ω\n",num);
arr[10]=num;
}
printf("Please select the calculation of the following:\n");
printf("(1).Calculate & display mean value of the resistor values \n");
printf("(2).Calculate & display the sample standard deviation of the resistor values \n");
printf("(3).Calculate & display the largest value of the resistor values \n");
printf("(4). Quit!\n");
puts("Your choice is :");
scanf_s("%d",&n);
switch(n){
case 1 :
//;
break;
case 2 :
//;
break;
case 3 :
//;
break;
case 4 :puts("Goodbye!");
default:
puts("Error\n");
break;
getchar();
}
return 0;
}
------解决思路----------------------
随机数放到数组里,计算均值,方差就是数学问题了
------解决思路----------------------
int main()
{
time_t t;
int a,i;
char c;
float num, max, sum=0.0, ave,s,ab,sumab=0.0;
srand((unsigned)time(&t));
a = rand() % 9 + 2; //get 2-10 values
printf("The registor values are:\n");
for(i = 0; i < a ; ++i)
{
arr[i] = rand() % 1000000 + (float)(rand() % 1001) * 0.001f; //get the values from 0.001-1000000
printf("%.3f Ω\n",arr[i]);
}
while(1)
{
printf("Please select the calculation of the following:\n");
printf("(1).Calculate & display mean value of the resistor values \n");
printf("(2).Calculate & display the sample standard deviation of the resistor values \n");
printf("(3).Calculate & display the largest value of the resistor values \n");
printf("(4). Quit!\n");
puts("Your choice is :");
scanf("%s",&c);
switch(c)
{
case '1' : //mean value
for (i=0;i<a;i++){
sum=sum + arr[i];
}
ave=sum/a;
printf("The mean value is %.3f Ω\n\n",ave);
break;
case '2' : //sample standard deviation
for (i=0;i<a;i++){
sum=sum + arr[i];
}
ave=sum/a;
for (i=0;i<a;i++){
ab=abs(arr[i]-ave);
sumab=(pow(sumab,ab))/a;
}
printf ( "The sample standard deviation is %.3f Ω\n\n", sumab );
break;
case '3' : //largest value
max=arr[0];
for (i=0;i<a;i++){
if(arr[i]>max)
max=arr[i];
}
printf("The largest value is %.3f Ω\n\n",max);
break;
case '4' :
puts("Goodbye!\n");
exit(0);
default:
puts("Error\n\n");
break;
}
}
return 0;
}