-3

Here is my. I think my code is correct but it gets stuck after i give input. But if i remove other code except sorting and printing it in ascending order it works. But if not it doesn't work. It stuck here

stuck

#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("No. of elements should not be greater than 20.\n");
    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], n);
    dn = dminus(&num[n], n);

    if(dp>dn)
    {

        dvalue1 = dp;
     }

     else
     {
         dvalue1 = dn;
     }

     //float dalphas = 0.05;
     float dvalue = 0.565;  

     if(dvalue1 < dvalue)
     {
         printf("\n Since D is less tha Dalpha so the data is unformily 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]); 
            }
        }

      float lar = larges(&data[n], 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] = ((count/n)-num[i]);   
                       } 
      }

      float lar;
      lar = larges(&data[n], 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];
     // printf("%f",lar);
      return lar;
}
ForceBru
  • 43,482
  • 10
  • 63
  • 98
SahSantoshh
  • 73
  • 2
  • 12

2 Answers2

0

You should always check the return value from scanf. That is - scanf returns the number of items matched, so you should do:

if (1 != scanf("%d", &n))
{
   // Add error handling
}

If you want to repeat until the input is matched, do:

while(1)
{
    printf("Enter number of elements to compute for tets: \t");
    if (1 == scanf("%d", &n)) break;
    printf("\nIllegal input, try again\n");
}

The same applies when reading in the float values.

That said, I don't think this is the reason, your program is stuck. To debug it, just add more printf statements. Example:

printf("Enter number of elements to compute for tets: \t");
scanf("%d", &n);

printf("Got %d\n", n);   // DEBUG PRINT

float num[n];
float dp, dn;

for(int i=0; i<n; i++)
{
    scanf("%f", &num[i]);
} 

printf("Got all floats\n");     // DEBUG PRINT


//sorting in ascending order

 for(int i=0; i<n; i++)
{
    printf("Got i = %d\n", i);      // DEBUG PRINT

    for(int j=i+1; j<n; j++)
    {
        if(num[i]>num[j])
        {
            float temp = num[i];
            num[i] = num[j];
            num[j] = temp;
            }
        }
    }

and so on.

Support Ukraine
  • 42,271
  • 4
  • 38
  • 63
0

in the while loop which you used to initiating functions d plus and d minus you used while loop which is going for a infinite loop as the count value is not changing in the loop.It is going on and on and on.