Click here to Skip to main content
15,611,312 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
While I push element in stack 2 and try to display element in stack 2 I am getting a 9 extra with the pushed element all the time why ?

What I have tried:

#include <stdio.h>
#define max 10

int top1, top2, stk_arr[max];

void push();
void pop();
void display();

void main()
{
int ch;
top1=-1,top2=max;
do
{
printf("\n 1:push\n 2:pop\n 3:display\n 4:exit\n Enter your choice: ");
scanf("%d", &ch);
switch (ch)
{
case 1:push();
break;
case 2:pop();
break;
case 3:display();
break;
case 4:printf("exiting from program\n");
break;
default:printf("wrong choice\n");
break;
}
}while(ch!=4);
}
void push()
{
int x,ch;
if(top1==top2-1)
{
printf("stack overflow \n");
return;
}
printf(" Enter a no: ");
scanf("%d",&x);
printf("\n Press 1 to push in stack1 or press 2 for stack2: ");
scanf("%d",&ch);
if(ch==1)
stk_arr[++top1]=x;
else
stk_arr[--top2]=x;
printf("\n %d Element is successfully pushed \n",x);
return;
}

void pop()
{
int y,ch;
printf("\n Press 1 to pop from stack1 or press 2 for stack2: ");
scanf("%d",&ch);
if(ch==1)
{
if(top1==-1)
{
printf("stack underflow\n");
return;
}
y=stk_arr[top1];
stk_arr[top1--]=0;
}
else
{
if(top2==max)
{
printf("stack underflow\n");
return;
}
y=stk_arr[top2];
stk_arr[top2++]=0;
}
printf("\n%d Element is successfully poped from stack \n", y);
return;
}

void display()
{
int i;
if (top1 == -1)
{
printf("Stack1 is empty \n");
}
else
{
printf("Elements of Stack1 are : \n");
for (i = 0; i <= top1; i++)
{
printf("%d\n",stk_arr[i]);
}
}
if (top2 == max)
{
printf("Stack2 is empty \n");
}
else
{
printf("Elements of Stack2 are : \n");
for (i = max; i >= top2; i--)
{
printf("%d\n",stk_arr[i]);
}
}
return ;
}
Posted
Updated 4-Apr-18 2:34am

Indent your code!
It makes it a whole load easier to read:
C++
void main()
    {
    int ch;
    top1=-1,top2=max;
    do
        {
        printf("\n 1:push\n 2:pop\n 3:display\n 4:exit\n Enter your choice: ");
        scanf("%d", &ch);
        switch (ch)
            {
            case 1:
                push();
                break;
            case 2:
                pop();
                break;
            case 3:
                display();
                break;
            case 4:
                printf("exiting from program\n");
                break;
            default:printf("wrong choice\n");
                break;
            }
        }while(ch!=4);
    }
But this is your homework, and getting it to work is part of the task.
So, it's going to be up to you.
Fortunately, you have a tool available to you which will help you find out what is going on: the debugger. How you use it depends on your compiler system, but a quick Google for the name of your IDE and "debugger" should give you the info you need.

Put a breakpoint on the first line in the function, and run your code through the debugger. Then look at your code, and at your data and work out what should happen manually. Then single step each line checking that what you expected to happen is exactly what did. When it isn't, that's when you have a problem, and you can back-track (or run it again and look more closely) to find out why.

Sorry, but we can't do that for you - time for you to learn a new (and very, very useful) skill: debugging!
 
Share this answer
 
The error is in the printing loop:
for (i = max; i >= top2; i--)
{
    printf("%d\n",stk_arr[i]);
}
You have an out of bound access by starting at stk_arr[max] because valid indexes are in the range 0 to max-1.

So change the loop to
for (i = max - 1; i >= top2; i--)
{
    printf("%d\n",stk_arr[i]);
}
 
Share this answer
 
Quote:
I am getting a 9 extra with the pushed element all the time why ?

Are you max is the first element of stack2 ?

Your code do not behave the way you expect, and you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.
Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]
The debugger is here to only show you what your code is doing and your task is to compare with what it should do.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900