Even stack and odd stack logic error

Asked

Viewed 515 times

-1

Guys I got this code down, it works, only it’s weird, it doesn’t print the last position.

#include <stdio.h>
#include <stdlib.h>

#define TAM_PILHA 2
#define NUM_ENTRA 5

void empilha(int *pilha, int *topo, int valor)
{
if (*topo < TAM_PILHA)
{
    pilha[*topo] = valor;
    (*topo)++;
}
else
{
    printf("Pilha Cheia\n");
}
}

int desempilha(int *pilha, int *topo)
{
if (*topo > 0)
{
    (*topo)--;
    return pilha[*topo];
}
else
{
    printf("Pilha Vazia\n");
}
}

int main()
{
int pilha_par[TAM_PILHA], pilha_impar[TAM_PILHA], topo_par = 0, topo_impar = 0;
int i = 0, num_do_usuario = 0;
for (i = 0; i < NUM_ENTRA; i++)
{
    printf("\nEntre com um numero: ");
    scanf("%d", &num_do_usuario);

    if ((num_do_usuario % 2) == 0)
    {
        empilha(pilha_par, &topo_par, num_do_usuario);
    }
    else
    {
        empilha(pilha_impar, &topo_impar, num_do_usuario);
    }
}

printf("\n\nNumeros pares sao: ");
while (topo_par > 0)
{
    printf("%d ", desempilha(pilha_par, &topo_par));
}

printf("\n\nNumeros impares sao: ");
while (topo_impar > 0)
{
    printf("%d ", desempilha(pilha_impar, &topo_impar));
}

printf("\n\n");

system("pause");
return 0;
}
  • For me it’s working, of course there was an error and it didn’t compile: http://ideone.com/qD47TT

  • Here is a time that comes up to the 3 question and you type the number already gives full stack. And there are times when you ask five times, which is right, and you don’t print the last number I type.

  • In fact in the form made to change the define can give some problem, the code is not anything robust.

  • Type number 3 first, then 4, 2 and 6, it’s gone from the full stack! Since it wasn’t meant to give, you have to go to the fifth vector, the last vector and print the last vector too, the last vector doesn’t print on the screen.

2 answers

2

There’s nothing wrong with the code. I put some printf() to show what’s going on.

The pile is really filling up. Put 1 item in odd, then put 1 in even, then put a second in even, so the 2 elements that the stack may have already been hit, then try to put the third and give full stack, then put another value in the odd stack filling it and close. All within the normal range. If you want to put 3 pairs and do not fill the stack, increase the size to 3. Or try to only put 2 elements in each stack.

#include <stdio.h>
#include <stdlib.h>

#define TAM_PILHA 2
#define NUM_ENTRA 5

void empilha(int *pilha, int *topo, int valor) {
    printf("%d na posição %d ", valor, *topo);
    if (*topo < TAM_PILHA) {
        pilha[*topo] = valor;
        (*topo)++;
    } else {
        printf("Pilha Cheia\n");
    }
}

int desempilha(int *pilha, int *topo) {
    if (*topo > 0) {
        (*topo)--;
        return pilha[*topo];
    } else {
        printf("Pilha Vazia\n");
        return -1;
    }
}

int main() {
    int pilha_par[TAM_PILHA] = { 0 }, pilha_impar[TAM_PILHA] = { 0 }, topo_par = 0, topo_impar = 0;
    for (int i = 0; i < NUM_ENTRA; i++) {
        int num_do_usuario = 0;
        printf("\nEntre com um numero: ");
        scanf("%d", &num_do_usuario);
        if (num_do_usuario % 2 == 0) {
            printf("Vai entrar na pilha   par: ");
            empilha(pilha_par, &topo_par, num_do_usuario);
        } else {
            printf("Vai entrar na pilha impar: ");
            empilha(pilha_impar, &topo_impar, num_do_usuario);
        }
    }
    printf("\n\nNumeros pares sao: ");
    while (topo_par > 0) {
        printf("%d ", desempilha(pilha_par, &topo_par));
    }
    printf("\n\nNumeros impares sao: ");
    while (topo_impar > 0) {
        printf("%d ", desempilha(pilha_impar, &topo_impar));
    }
}

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

1


In his job empilha I switched the sign of < for <= within the for (i = 0; i < NUM_ENTRA; i++).

Entree:

Entre com um numero: 1 
Entre com um numero: 2 
Entre com um numero: 3
Entre com um numero: 4 
Entre com um numero: 5

Exit:

Even numbers are: 4 2

Odd numbers are: 5 3 1

#include <stdio.h>
#include <stdlib.h>

#define TAM_PILHA 2
#define NUM_ENTRA 5

void empilha(int *pilha, int *topo, int valor)
{
    if (*topo <= TAM_PILHA)
    {
        pilha[*topo] = valor;
        (*topo)++;
    }
    else
    {
        printf("Pilha Cheia\n");
    }
}

int desempilha(int *pilha, int *topo)
{
    if (*topo > 0)
    {
        (*topo)--;
        return pilha[*topo];
    }
    else
    {
        printf("Pilha Vazia\n");
    }
}

int main()
{
    int pilha_par[TAM_PILHA], pilha_impar[TAM_PILHA], topo_par = 0, topo_impar = 0;
    int i = 0, num_do_usuario = 0;
    for (i = 0; i < NUM_ENTRA; i++)
    {
        printf("\nEntre com um numero: ");
        scanf("%d", &num_do_usuario);

        if ((num_do_usuario % 2) == 0)
        {
            empilha(pilha_par, &topo_par, num_do_usuario);
        }
        else
        {
            empilha(pilha_impar, &topo_impar, num_do_usuario);
        }

    }

    printf("\n\nNumeros pares sao: ");
    while (topo_par > 0)
    {
        printf("%d ", desempilha(pilha_par, &topo_par));
    }

    printf("\n\nNumeros impares sao: ");
    while (topo_impar > 0)
    {
        printf("%d ", desempilha(pilha_impar, &topo_impar));
    }

    printf("\n\n");


    return 0;
}

See on Ideone.

  • Here it happened: first I typed the number 1, 2, 3, 4 and 5, the output was: even numbers: 4 and 5 - odd numbers: 5 3 and 1. It’s wrong that.

  • 1

    @Thiagoferreira, I don’t know why it is different, I even tested it on another compiler and it appeared correctly. See: https://goo.gl/7xu317

  • I’m testing by codeblocks, try by it you will see the difference, in fact by your online compiler there worked out, but still in the online compiler ai, if you type: 65, 43, 23 and 5 will already stack full.

  • 1

    Friend worked only in Visual Studio. In code Blocks I could not. I will open a new asking about this.

Browser other questions tagged

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