This code is leading me to an infinite cycle and I can not understand why

-1

This code is leading me to an infinite cycle and I can not figure out why.

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

void
troca (double *n1, double *n2)
{
    double n;

    n    = *n1;
    *n1  = *n2;
    *n2  = n  ;
}

void
ordena (double *r, int size)
{
    int t;
    int i;

    while (1)
    {
        t = 0;
        {
            for (i=0;i<size-1;i++)
            {
                if(r[i] > r[i+1])
                {
                    troca (&r[i], &r[i+1]);
                    t      = 1          ;
                }
            }
        if(t == 0)
            break;
        }
    }
}


int
main(int argc, char **argv)
{
    FILE   *f1   ;
    int    i     ;
    int    j=0   ;
    int    N     ;               
    int    k     ;             
    double Min   ;
    double Max   ;
    double *reais;           
    double n     ;
    char    c    ;

    sscanf(argv[1],"%d",&N)   ;
    sscanf(argv[2],"%lf",&Min);
    sscanf(argv[3],"%lf",&Max);

    f1    = fopen("f1.txt", "wb")            ; 

    srand(time(NULL));

    while (1)
    {

    if (j == 0)
    {
        reais = (double*)malloc((N)*sizeof(double));
        j=1;
    }

    else
        reais = realloc(reais, (N)*sizeof(double));

    n = Max - Min;
    for (i=0;i<N;i++)
    {
        reais[i] = Max - ((double) rand()/(double) RAND_MAX )*n;
        //printf("%9lf",reais[i]);
    }

    ordena(reais, N);

    k = 0;
    for (i=0;i<N;i++)
    {
        printf("%9lf",reais[i]);
        k++;

        if (k == 5)
        {
            k = 0;
            printf("\n");
        }
    }

    printf("\nDeseja repetir as operacoes:\n1-Repetir\n2-Sair\nR:");
    c = getchar();

    if (c == '1')
    {
        printf("\nIntroduza novos valores de N, Min e Max:");
        scanf("%d %lf %lf", &N, &Min, &Max);
        continue;
    }

    if (c == '2')
    {
        free(reais);
        break;
    }

    }

    return 0;
}

The program receives three command line arguments, N, the number of reals the user wants to generate, Max and Min the limits of the generated values. With these values is generated a vector of doubles with the necessary size that is then ordered. So far so good. The problem is that when I ask for new arguments, I go into an infinite loop

    
asked by anonymous 20.12.2017 / 20:51

2 answers

0

If I understand the code well, the problem is in the ordena method; as you have a for that traverses all positions of r , you do not need while(1) .

The loop was because t == 0 never occurs, since whenever it enters the iteration for (i=0;i<size-1;i++) this face runs and t = 1; also.

voi ordena (double *r, int size)
{
    int t = 0;

    for (int i = 0; i < size - 1; i++)
    {
        if (r[i] > r[i+1])
        {
            troca (&r[i], &r[i+1]);
            t = 1;
        }
    }
}

NOTE: I kept the structure of the variables and such, but try to use more intuitive names, it is much easier to understand the code.

In addition, there are some things to improve, for example FILE *f1; . You only create this variable and open a file, but it does not use anything.

    
20.12.2017 / 21:40
0

You are sorting by using the bubble strategy, with the right to a flag to mark if there were changes or not.

Mathematically, you have to do at most o( (n^2 - n)/2 ) operations. When rotating the inner side once, there is a guarantee that the largest number will be at the end of the vector. Then the next ordering move would not have to go to the last position, only until the penultimate, so the two biggest elements will be in their final positions. In this answer , I called this strategy to minimize the number of repetitions of "minimal bubblesort". You can by the flag to indicate that if there was no swap , then the vector is already ordered and no more working on it.

This operation the way you are doing is slow. Do not be surprised by that. My suggestion for improvement at best doubles the speed, but it is still slow (more relative performance information in the other response link ).

In theory, your ordering would one day end by considering that the trade-in operation is correct and that the comparison is correct. I could see no problem in these operations. If you want to debug, but do not have the most traditional tools for this, you can enter a printf to indicate when there is an exchange and what the values are being exchanged. Obviously this would be a debug code that should be removed in the final version.

Then, the loop failure must be at another point. The other loop there is in the main function. You are using getchar() . What happens if this function returns a value that you do not expect? From what I understand from the code, it loosely loops back, without changing any of the parameters.

Or maybe he just is not reacting to you typing? According to the function reference , it only returns the moment you press ENTER . It has not been clear to me what happens if you happen to have read something before.

    
20.12.2017 / 23:37