我用 C++ 为 Kolmogorov Smrinov 测试编写了一个程序,但它不起作用。它没有返回 d+(dp), d-(dn) 一个正确的值。我在程序中找不到任何错误。请大侠们快点帮忙。我认为在函数 dplus 和 dminus 的函数中作为 arg 传递的数组存在一些错误。注意:- 此程序仅适用于 alpha 值为 0.05 且列表值为 0.565 的分布
#include <stdio.h>
#include <math.h>
float dplus(float num[], int n);
float dminus(float num[], int n);
float larges(float data[], int n);
int main()
{
printf("Kolmogorov Test\n");
int n;
float dvalue1;
printf("Enter number of elements to compute for tets: \t");
scanf("%d", &n);
float num[n];
float dp, dn;
for(int i=0; i<n; i++)
{
scanf("%f", &num[i]);
}
//sorting in ascending order
for(int i=0; i<n; i++)
{
for(int j=i+1; j<n; j++)
{
if(num[i]>num[j])
{
float temp = num[i];
num[i] = num[j];
num[j] = temp;
}
}
}
printf("\nNumbers in ascending order is: \t");
for(int i=0; i<n; i++)
{
printf("%0.2f\t",num[i]);
}
dp = dplus(num, n);
dn = dminus(num, n);
printf("\ndp = %f",dp);
printf("\ndp = %f",dn);
if(dp>dn)
{
dvalue1 = dp;
}
else
{
dvalue1 = dn;
}
float dvalue = 0.565; //for alpha = 0.05
printf("\nCalculated D = %0.2f",dvalue1);
if(dvalue1 < dvalue)
{
printf("\n Since D is less tha Dalpha so the data is uniformily distributed.");
}
else
{
printf("\nSince D is greater than Dalpha so the data is not uniformily distributed.");
}
return 0;
}
float dplus(float num[], int n)
{
float data[n];
int count=1;
for(int i=0; i<n; i++)
{
while(count<=n)
{
data[i] = ((count/n)-num[i]);
count++;
}
}
float lar = larges(data, n);
return lar;
}
float dminus(float num[], int n)
{
float data[n];
int count=1;
for(int i=0; i<n; i++)
{
while(count<=n)
{
data[i] = (num[i]-((count-1)/n));
count++;
}
}
float lar = larges(data, n);
return lar;
}
float larges(float data[], int n)
{
for(int i=1; i<n; i++)
{
if(data[0]<data[i])
data[0] = data[i];
}
float lar = data[0];
return lar;
}