Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8593295
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T23:57:21+00:00 2026-06-11T23:57:21+00:00

Hi i have a problem with my stack data structure program. It seems that

  • 0

Hi i have a problem with my stack data structure program. It seems that when i define the size of my array/the imaginary size of array just to call it through a loop the size i defined or specified by the user is being depleted or somewhat edited when i enter a data or push.

For ex. i entered 5 for the size and choose push and then add 2. It is working properly. But if i choose to push data again, it is now passing to the size variable. I do not know understand what is happening…

#include <stdio.h>
#include <ctype.h>
#include <conio.h>
#include <string.h>
#include <stdlib.h>
#define p printf
#define s scanf

int top;
int ar[1];
int size;

main()
{
    void push();
    int opt, num;
    char cont[] = { 'y' };
    clrscr();

    p("Stacking Program");
    p("\n\nData Size: ");
    s("%d", &size);
    p("\n");

    while((cont[0] == 'y') || (cont[0] == 'Y'))
    {
        clrscr();
        p("Stacking Program");
        p("\n\nData Size: %d\n\n", size);
        p("MAIN MENU\n1. Pop\n2. Push\n3. Pick\n4. View\nChoose: ");
        s("%d", &opt);
        p("\n");

        switch(opt) {
            case 1:
                pop();
                break;
            case 2:
                if(top > size)
                {
                    p("You can't push more data");
                }
                else
                {
                    p("Enter data for Data[%d]: ", top);
                    s("%d", &num);
                    push(num);
                }
                break;
            case 3:
                pick();
                break;
            case 4:
                view();
                break;
            default:
                p("Your choice is not in the list.");
                break;
        }

        p("\n\nDo you want continue\(Y\/N\)?");
        s("%s", &cont[0]);
    }
}

pop()
{
    int a;
    if(top < 0)
    {
        p("Stack empty.");
        return 0;
    }
    else
    {
        a = ar[top];
        p("\(Data[%d] = %d\) removed.", top, a);
        top--;
    }
}
void push(int b)
{
    top++;
    ar[top] = b;
}
pick()
{
    if(top < 0)
    {
        p("Nothing to display.");
        return 0;
    }
    else
    {
        p("\(Data[%d] = %d\) is the last data.", top, ar[top]);
    }
}
view()
{
    int i;
    if(top < 0)
    {
        p("Nothing to display.");
        return 0;
    }
    else
    {
        for(i = 1; i < (top + 1); i++)
        {
            p("Data[%d] = %d\n", i, ar[i]);
        }
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T23:57:22+00:00Added an answer on June 11, 2026 at 11:57 pm

    You need to define the size of the array at runtime, using the size entered by the user.
    instead of:

    int top;
    int ar[1];
    int size;
    

    …

    int top = -1;
    int *ar = NULL;
    int size = 0;
    

    and then after getting size from the user:

       if ( size > 0 )
       {
          ar = malloc(size * sizeof(int));
          if ( ar == NULL )
          {
             printf("ERROR: malloc() failed\n");
             exit(2);
          }
       }
       else
       {
          printf("ERROR: size should be positive integer\n");
          exit(1);
       }
    
    
       ....
          p("\n\nDo you want continue(Y/N)?");
          s("%s", &cont[0]);
       }
       free(ar);
    
    
    } // end of main
    

    I think the for loop in view() should be:

     for(i = 0 ; i <= top ; i++)
    

    also

    case 2:
        if ( top == ( size - 1 ))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two C functions, which basically operate on a stack data structure. This
I am writing a program that creates a stack using linked lists. I have
I have a program that's using adt / abstract data classes with data structures.
I'm having a problem w/ my program. I have extracted a set of data
I have a problem and I am stack. If anyone have a spare moment
I have this problem, see the trace stack: E/AndroidRuntime(2410): Caused by: org.xmlpull.v1.XmlPullParserException: Binary XML
Ok so I have a recursion problem thats causing a stack overflow error at
I have Bitnami Rails stack installed on my Mac. To better explain my problem
I have a simple problem that I've been stuck on for some time and
My problem is that I have three different stock sizes for each product that

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.