0

I'm trying to write a program where the user introduces an array numbers and alphabethic characters. Then the program reads the array, if he sees a number, the program should push that number into one stack. However, if he sees an alphabetical character, the last number pushed is popped.

So far I have this code:

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

#define MAX 20

int top, i;
void push (double stack[], double x, int top)
{
stack[top] = x;
}
 int pop (double stack[])
{
    double x;
    stack [top]=x;
    return x;
}

void display (double stack[],int top)
{
    int i;
    printf ("\n The stack is: ");
        for (i=0; i<=top; i++)
         {
           printf ("%lf\n",stack[i]);
     }
}

void main()
{
int r;
int stack[10];
char array[10];
printf("introduce the numbers");
fgets(array,MAX,stdin);
int l;
r=strlen(array);
top=0;
for (l=0;l<=r;l++)
{
int n;
if (isdigit(array[l]))
 {
    push(stack,array[l],top);
    top=top+1;
 }

 if (islower(array[l]))
 {
        pop(stack);
        printf("you have popped %d", n);
        top=top-1;
 }
}
 display(stack,top);
}

For some reason the program does not work, if I introduce 22a the output is:
you have popped 4194432 The stack is: 50.00000 50.00000

I am particularly interested in how should I write the pop,push and display to make this program work. How can I do it?

Zoe
  • 27,060
  • 21
  • 118
  • 148
tyuui112
  • 27
  • 2
  • 1
    You're reading twenty characters `#define MAX 20` into an array of size 10. Also, in your loop you have `for(l=0; l<=r; l++)`, which may result in a segfault when you'll try to access `stack[l]` if `l==r`. – ForceBru Jun 03 '16 at 17:20
  • What's more, you're calling `pop(stack)` and _not using the return value_ (!), so what makes you think that an uninitialized variable `n` will contain some useful value? – ForceBru Jun 03 '16 at 17:24
  • Whenever you're looping, you do `for(i=0; i<=threshold; i++)`, which doesn't make sense: you're trying to get `treshold + 1` values from the array, while you definitely want only `treshold` of them. – ForceBru Jun 03 '16 at 17:27
  • When popping, you should decrement top first, otherwise you will not access the previously pushed element. – Hans-Martin Mosner Jun 03 '16 at 17:55

1 Answers1

3

First off, your variable n, the value of which you're printing, is uninitialized and contains whatever garbage that was in the memory at the moment of its creation.

Also, why are you printing it? I think you meant to say n = pop(stack);, right? Otherwise this printing is useless.

Throughout your code you're writing loops the wrong way: for (t=0; t<=threshold; t++). This code will make the loop run threshold + 1 times, but you obviously want only threshold, so do for (t=0; t<threshold; t++) instead.

You're also reading (fgets(array,MAX,stdin);) maximum twenty characters into your array, which can hold only ten characters.

To use strlen on an array, you need it to end with zero (null-terminator). In your code array is not necessarily initialized with zeros, so use memset(array, 0, 10); before using array:

  1. Docs on memset
  2. Tutorial on for loops
  3. void main() is wrong
  4. What to read to learn C
Community
  • 1
  • 1
ForceBru
  • 43,482
  • 10
  • 63
  • 98