Question about row in C

Asked

Viewed 160 times

0

Can someone analyze my code and see what might be wrong?. It’s a simulation exercise I got to do in college. Error: Code runs, but loops. Which while has a 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);

}
  • Could you at least say something about the error that occurs ? or is it secret ? "I’m a doctor Jim, not a Psychic"

  • Jose, I think it’s an error in the withdrawal of the value at the end of the line.

  • 1

    Line 74: [Error] 'c' was not declared in this scope. You are passing a 'c' variable that does not exist as a function parameter. Declare the c variable and put a value in it before using it.

  • The right one was I use counter and not c, I already fixed it. However it goes into looping.

  • @Ygorfraga Place the requested information in the question itself, using the [Edit button].

1 answer

0

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

I made two changes to your code:

  1. I used int main(), you use void main()
  2. I used *c = *c + 1, in place of all *c++

Stayed 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 on Ideone.

A hint, be careful with code tabulation.

  • It worked, thanks. But why weren’t you counting on *c++? and in this code it would make a difference to use void main()?

  • @Ygorfraga, honestly I don’t know why *c++, it didn’t work. You can ask another question and other users will know how to answer that. I always use *x = *x +1. On the other hand, "void" functions do not return any value, which means that they are only procedures, they are static. The correct is to use the "main" function as integer, because its value can indicate whether the program has been correctly completed or not.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.