Stack does not work correctly in Codeblocks

1

This code compiles both in Codeblocks and Visual Studio, but the output is different in Codeblocks. For VS I do not delete anything from the code below, just run and ready, now in Codeblocks I took these two libraries:

#include "stdafx.h"
#include "windows.h"

and added these:

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

I changed the scanf_s to scanf

and compiled, compiles normally, except that the output is wrong, that's in Codeblocks. In VS it is quiet it works normally.

I wanted to know what's wrong with Codeblocks, I want to run with perfect output on Codeblocks.

// pilha.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "windows.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_s("%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;
}

Wrong:

Anothererrorscenario:

    
asked by anonymous 14.03.2017 / 18:45

1 answer

1

You're doing the wrong stacking.

Your stacks have TAM_PILHA size, so the check at the time of stacking should be:

if (*topo < TAM_PILHA)

instead of

if (*topo <= TAM_PILHA)

I've copied your code, compiled it, and then run it in Windows 10. Follow the entries and exits:

Tickets

  

10, 10, 15, 15, 15

Output

  

Even numbers are: 10 15

     

Odd numbers are: 15 15 15

In another test, only 4 values were read. That is, the program did not show expected results and did not have the desired behavior. Again in Windows 10.

On the other hand, in Ubuntu 16.04 the entries and the result were:

Tickets

  

10, 10, 15, 15, 15

Output

  

Even numbers are: 10 10

     

Odd numbers are: 15 15 15

The result, no matter how sound it seems, is still wrong. Because TAM_PILHA is worth 2, that is, the stack should only store 2 elements.

Making the change listed above the program executes as expected and produces the correct results.

Ps2: I used the headers stdlib.h and stdio.h.
Ps2: I compiled with GCC and executed in windows cmd. In both codes (original and changed).
Ps3: I compiled with G ++ and ran in Ubuntu bash.

    
15.03.2017 / 03:13