Stack does not work properly in Codeblocks

Asked

Viewed 168 times

1

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

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

and added these:

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

changed the scanf_s for scanf

and compiled, compiles normally, only that the output is wrong, this in Codeblocks. In VS is quiet 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:

Erro ao empilhar/desempilhar

Another error scenario:

Erro novamente

  • And what went wrong?

1 answer

1


You’re doing the stacking wrong.

Your batteries have TAM_PILHA size, so the check when stacking should be:

if (*topo < TAM_PILHA)

instead of

if (*topo <= TAM_PILHA)

I copied your code, compiled and ran it on Windows 10. Follow the entries and outputs:

Entrances

10, 10, 15, 15, 15

Exit

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 on Windows 10.

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

Entrances

10, 10, 15, 15, 15

Exit

Even numbers are: 10 10

Odd numbers are: 15 15 15

The result, however correct it may seem, is still wrong. Because TAM_PILHA is worth 2, that is, the stack should store only 2 elements.

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

Ps1: I used stdlib. h and stdio. h headers.
Ps2: I compiled with GCC and ran on Windows cmd. In both codes(original and changed).
Ps3: I compiled with G++ and ran on Ubuntu bash.

  • Look I did what you said and did not surtiu at first the expected effect, at least here in CB. You are not reading the last vector.

  • 1

    What do you mean by reading the last vector? If you set the stack to size 2, then it should be able to store 2 elements. If you’re reading 5 entries, it won’t fit all entries in the 2 stacks. I just ran your code on an Ubuntu 16.04 and it ran right. The only problem is that your code stores in the stack position[2]. This position does not exist in the stack, but the C language allows you to do this. In fact your code only needs 1 change, in the case the first one I quoted.

  • Send me a message I show on team view

  • Post some prints to make it clearer. Maybe we’re talking about different problems.

  • all right I posted there

  • 1

    According to this print the operation is OK. Everything went as expected. The even stack contains only even numbers and the odd stack contains only odd numbers. And finally, it was not possible to stack more numbers in the odd stack as it was already full(2 elements).

  • There’s no way that’s right, number 3 and 5 are not being printed, and I look at another scenario I posted there.

  • 1

    Exactly, they should not be being printed. This happens because the stack has the size defined by the TAM_PILHA constant. If TAM_PILHA is worth 2, then the stack can stack at most 2 elements. If it is worth 3, then the stack can stack at most 3 elements. In your example, it is probably worth 2. Then when trying to insert a third element there will be an error of " full stack". That is, when the destacking process happens, this number will not be in the stack and consequently will not be popped.

Show 4 more comments

Browser other questions tagged

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