Cells with vectors in C, is showing the elements that have already been removed

Asked

Viewed 452 times

0

How do I show my vector without the numbers that have been removed? 'Cause when I show him, even if I take a number off the function, he still shows up inside.

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

int pilha[MAX];
int inicio,fim;

int pilhaCheia(){
    return (fim == MAX);    
}
int pilhaVazia(){
    return (inicio == fim);
}
void push(int x){
    if( !pilhaCheia() ){
        pilha[fim++] = x;
    }else{
    printf("Pilha cheia \n");   
    }
}
int pop(){
    int aux;
    if( !pilhaVazia() ){
        aux=pilha[fim];
        fim--;
        return aux;

        }else{
            printf("Pilha vazia \n");
        return -1;  
        }
    }


void exibe(int pilha[MAX]){
    int x;
    for( x=0; x < MAX; x++){
        printf("%d",pilha[x]);
    }
}

main(){

    inicio = 0;
    fim = 0;
    int escolha,valor;
    do{
    printf("\n1 EMPILHA:\n");
    printf("\n2 DESEMPILHA:\n");
    printf("\n3 Mostra:\n");
    printf("\n4 Sair:\n");
    scanf("%d",&escolha);
    int x;
    switch(escolha){
        case 1:
            printf("Escolha o valor:");
            scanf("%d",&valor);
            push(valor);
            break;
            case 2:
            printf("%d",pop());
            break;
            case 3:
            exibe(pilha);   
            break;
            default:
            break;
    }



    }while( escolha != 4);  

    }

1 answer

1


Just change the condition of your for in function exibe:

for( x=0; x < fim; x++){
    printf("%d",pilha[x]);
}

Note that here I used fim instead of MAX.

  • Vlw for the help, I stayed a while thinking haha

  • @Igorvargas If this answer solved your problem and left no doubt, mark it as correct/accepted by clicking on the " " that is next to it, which also marks your question as solved. If you still have any questions or would like further clarification, feel free to comment.

Browser other questions tagged

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