0

我用 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;
}
4

2 回答 2

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;
}

我在您的数组传递中没有看到任何错误。

于 2016-06-04T06:25:48.430 回答
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);
}
于 2016-06-04T06:29:03.227 回答