Question about queuing in C

0

Can someone analyze my code and see what could be wrong ?. It's a simulation exercise I got to do in college. Error: The code executes, but it goes into looping. Whats the problem?

Code:

#include <stdio.h>
#include <stdlib.h>
#define N 10

int suc(int p){
  int temp;
  if (p==N)
        temp=1;
  else
        temp=p+1;
  return(temp);
}

void inserir(char F[], int i, int *f, int *over, int *c, char ch){
    *over= 0;
    if(suc(*f)==i){
        printf("Overflow na fila\n");
        *over=1;
    }
    else{
        *f=suc(*f);
        F[*f]=ch;
        *c++;
      }
}

char remover(char F[], int *i, int f, int *c){
    char temp;
    if(*i==f)
        printf("Underflow na fila");
    else
    {   *i=suc(*i);
        temp=F[*i];
        *c++;
    }
    return(temp);
}

void exibirSomenteFila(char F[], int i, int f){
  int x;
  if (i == f)
    printf ("\nFila vazia");
  else
   { x=i;
     while (x != f)
       { x = suc(x);
         printf(" %c ",F[x]);
       }
   }
   printf ("\n");
}

void main()
{
char F[N+1], ch;
int i,f, minutos, contador, ov, r, k ;

minutos=0;
i=1;
f=1;
contador=0;
ov=0;

while(contador<40){

    minutos++;

    if (minutos % 2==0){
        inserir(F, i, &f, &ov, &contador, 'a');
        if (ov==1){
            printf("A area sera esvaziada");
            exibirSomenteFila(F,i,f);
            while (i != f){
                ch=remover(F, &i, f, &c);
                printf("%d", ch);
            }
            inserir(F, i, &f, &ov, &contador, 'a');
            exibirSomenteFila(F,i,f);
        }
    }

    if (minutos%3==0){
        r=rand() % 3;
        k=1;
        while (k<=r){
            ch=remover(F, &i, f, &contador);
            printf("%c", ch);
            k++;
        }
    }

    if (minutos%8==0)
        exibirSomenteFila(F,i,f);
}
exibirSomenteFila(F,i,f);

}
    
asked by anonymous 24.09.2016 / 20:38

1 answer

0

Your accountant is not being updated. If you put a printf("\n Contador = %d", contador) within while of main you will see this.

I made two changes to your code:

  • I used int main() , you use void main()
  • I used *c = *c + 1 , instead of all *c++
  • It looks like this:

    #include <stdio.h>
    #include <stdlib.h>
    #define N 10
    
    int suc(int p){
    
        int temp;
        if (p==N)
            temp=1;
        else
            temp=p+1;
        return(temp);
    }
    
    void inserir(char F[], int i, int *f, int *over, int *c, char ch){
    
        *over= 0;
        if(suc(*f)==i){
            printf("Overflow na fila\n");
            *over=1;
        }
        else{
            *f=suc(*f);
            F[*f]=ch;
            *c = *c + 1;
        }
    
    }
    
    char remover(char F[], int *i, int f, int *c){
    
        char temp;
        if(*i==f)
            printf("Underflow na fila");
        else
        {   *i=suc(*i);
            temp=F[*i];
            *c = *c + 1;
        }
    
        return(temp);
    }
    
    void exibirSomenteFila(char F[], int i, int f){
    
       int x;
       if (i == f)
         printf ("\nFila vazia");
       else
       { 
           x=i;
           while (x != f){ 
            x = suc(x);
            printf(" %c ",F[x]);
           }
       }
       printf ("\n");
    }
    
    int main(){
    
        char F[N+1], ch;
        int i,f, minutos, contador, ov, r, k ;
    
        minutos=0;
        i=1;
        f=1;
        contador=0;
        ov=0;
    
        while(contador<40){
    
            minutos++;
            if (minutos % 2==0){
                inserir(F, i, &f, &ov, &contador, 'a');
                if (ov==1){
                    printf("A area sera esvaziada");
                    exibirSomenteFila(F,i,f);
                    while (i != f){
                        ch=remover(F, &i, f, &contador);
                        printf("%d", ch);
                    }
                    inserir(F, i, &f, &ov, &contador, 'a');
                    exibirSomenteFila(F,i,f);
                }
            }
    
            if (minutos%3==0){
                r=rand() % 3;
                k=1;
                while (k<=r){
                    ch=remover(F, &i, f, &contador);
                    printf("%c", ch);
                    k++;
                }
            }
    
            if (minutos%8==0)
                exibirSomenteFila(F,i,f);
        }
    
        exibirSomenteFila(F,i,f);
    
        return 0;
    }
    

    See working at Ideone .

    One tip, be careful with the code tab.

        
    25.09.2016 / 00:43