Answer to Question #249516 in C for Vish...

Question #249516
Push any five elements in stack using array implementation and check which element in the stack is the prime number
1
Expert's answer
2021-10-10T16:56:36-0400
struct stack {
  int array[10];
  int size = 10;
  int idx = 0;
};

void push(struct stack &s, int value) {
  if (s.idx + 1 < s.size) {
    s.array[s.i] = value;
    s.i = s.i + 1;
  }
}

int pop(struct stack &s) {
  int res = NULL;
  if (s.i > 0) {
    res = s.array[s.i-1];
    s.i = s.i - 1;
  }
  return res;
}

bool is_prime(int number) {
 // TODO impl..
}

int main() {
  struct stack stc;
  push(stc, 2);
  push(stc, 4);
  push(stc, 5);
  push(stc, 3);
  push(stc, 2);
  while (s.i > 0) {
    printf("%d\n", is_prime(pop(stc)));
  } 
  return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS