How is the function calPoints returning null value?

Viewed 35

In order to identify the problem in the code, I tried to print the value of top after for loop and within each condition in the for loop. The value of top however cannot be printed after for loop where as it is printed after each iteration of for loop.Added to that the the values of stack are not getting printed either after for loop.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define max 1000 
void push(int *stack, int top, int n) {
  if(top==max-1) {
    return;
  }
  else {
    stack[top]=n;
  }
}

void pop(int *stack, int top) {
  if(top==-1) {
    return;
  }
}

int calPoints(char **ops, int opsSize) {
  int st[max],
  sum,
  p,
  x,
  i;
  int top=-1;
  for(i=0;
  i<opsSize;
  ++i) {
    if(strcmp(ops[i], "+")==0) {
      int sum=st[top]+st[top-1];
      ++top;
      push(st, top, sum);
      printf("sum= %d %d\n", sum, top);
    }
    else if(strcmp(ops[i], "D")==0) {
      int p=st[top]*2;
      top++;
      push(st, top, p);
      printf("p= %d %d\n", p, top);
    }
    else if(strcmp(ops[i], "C")==0) {
      pop(st, top);
      --top;
    }
    else {
      int x=atoi(ops[i]);
      ++top;
      push(st, top, x);
      printf("x=%d %d\n", x, top);
    }
  }
  printf("%d", top);
  int s=0;
  for(i=0;
  i<=top;
  ++i) {
    s+=st[i];
    printf("%d\t",st[i]);
  }
  return s;
}

int main() {
  char *c[5]= {
    "5",
    "2",
    "C",
    "D",
    "+"
  }
  ;
  int x=6;
  calPoints(c, x);
  return 0;
}

0 Answers
Related