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"
– zentrunix
Jose, I think it’s an error in the withdrawal of the value at the end of the line.
– Ygor Fraga
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.– Sérgio Mucciaccia
The right one was I use counter and not c, I already fixed it. However it goes into looping.
– Ygor Fraga
@Ygorfraga Place the requested information in the question itself, using the [Edit button].
– Pablo Almeida