我用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;
}
答案 0 :(得分:0)
count
和dplus()
例程中的dminus()
变量及其随附的while()
循环似乎没有意义。此外,您应该注意表达式是执行整数还是浮动除法。下面是对代码的简化修改 - 看看它是否更接近您的意图:
#include <stdio.h>
#include <math.h>
float dplus(float num[], int n);
float dminus(float num[], int n);
float largest(float data[], int n);
int main()
{
printf("Kolmogorov Test\n");
int n;
printf("Enter number of elements to compute for test: ");
scanf("%d", &n);
float num[n];
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("Numbers in ascending order are: \t");
for (int i = 0; i < n; i++)
{
printf("%0.2f\t", num[i]);
}
printf("\n");
float dp = dplus(num, n);
float dn = dminus(num, n);
printf("dp = %f\n", dp);
printf("dn = %f\n", dn);
float dvalue = dn;
if (dp > dn)
{
dvalue = dp;
}
printf("Calculated D = %0.2f\n", dvalue);
float dalpha = 0.565; // for alpha = 0.05
if (dalpha > dvalue)
{
printf("Since D is less than Dalpha, the data is uniformly distributed.\n");
}
else
{
printf("Since D is greater than Dalpha, the data is not uniformly distributed.\n");
}
return 0;
}
float dplus(float num[], int n)
{
float data[n];
for (int i = 0; i < n; i++)
{
data[i] = (i + 1) / ((float) n) - num[i];
}
return largest(data, n);
}
float dminus(float num[], int n)
{
float data[n];
for (int i = 0; i < n; i++)
{
data[i] = num[i] - i / ((float) n);
}
return largest(data, n);
}
float largest(float data[], int n)
{
float large = data[0];
for (int i = 1; i < n; i++)
{
if (large < data[i])
large = data[i];
}
return large;
}
我没有看到你的数组传递中的任何错误。
答案 1 :(得分:0)
这个C代码应该给你正确的D +和D值,但我不确定关键值。
#include <stdio.h>
double dplus(double num[], double d1[], int length, double n) {
for (int i = 0; i < length; i++) {
d1[i] = ((i + 1) / n) - num[i];
}
double d1max = d1[0];
for (int i = 0; i < length; i++) {
if (d1max <= d1[i]) {
d1max = d1[i];
}
}
printf("D+ = %f\n", d1max);
return d1max;
}
double dminus(double num[], double d2[], int length, double n) {
for (int i = 0; i < length; i++) {
d2[i] = (num[i] - (i) / n);
}
double d2max = d2[0];
for (int i = 0; i < length; i++) {
if (d2max <= d2[i]) {
d2max = d2[i];
}
}
printf("D- = %f\n", d2max);
return d2max;
}
void kst(double num[], int length) {
for (int i = 0; i < length; i++) {
for (int j = i + 1; j < length; j++) {
if (num[i] > num[j]) {
double temp;
temp = num[i];
num[i] = num[j];
num[j] = temp;
}
}
}
double d1[length];
double d2[length];
double n = (double) length;
double d1max = dplus(num, d1, length, n);
double d2max = dminus(num, d2, length, n);
double dplus = d1max;
double dminus = d2max;
double d;
if (dplus > dminus) {
d = dplus;
printf("D = %f\n", d);
} else {
d = dminus;
printf("D = %f\n", d);
}
}
int main() {
printf("Kolmogorov Test\n");
int n;
double dvalue1;
printf("Enter number of elements to compute for tets: \t");
scanf("%d", &n);
double num[n];
double dp, dn;
int i;
for (i = 0; i < n; i++) {
scanf("%lf", &num[i]);
}
kst(num, i);
}