0

Why is this for loop running the code inside it on every second iteration ? to make my code function properly I had to double the number of iteration it was supposed to do.

#include <stdio.h>
int main(){
int t,rear,id,n,b;
char pt;
int *stack;
scanf("%d",&t);
while(t--){
    rear = 0;
    b=0;
    scanf("%d%d",&n,&id);
    stack = (int *) malloc(n*sizeof(int));
    stack[rear]=id;
    for(int i=2*n;i>0;i--){
        scanf("%c",&pt);
        printf("character read at %d %c\n",i,pt);
        if(pt=='B'){
            if(b==1){
                ++rear;
                b=0;
            }
            else{
                --rear;
                b=1;
            }
        }
        else if(pt=='P'){
            b=0;
            scanf("%d",&id);
            ++rear;
            stack[rear]=id;
        }
    }
    printf("Player %d\n",stack[rear]);

}
return 0;
}

0 Answers0