the purpose of this program is to fill the stack by user input, and show the current value of top stack and number of that "top" variable every after new data input. but looks like my procedure isnt working as i intended. when i run the program "your data" and "current stack size" always 0. did i do smthing wrong ?
#include <stdio.h>
#include <stdlib.h>
#define size 5
typedef struct stacktype
{
double arr[size];
int top;
} stack;
/* prototype function */
void push(double elmt,stack c);
/*main program*/
int main(void)
{
char a;
double b;
stack c;
c.top=0;
printf("will you insert a data (Y/N) ?\n");
scanf("%c",&a);
while (a=='Y'){
printf("insert your data :\n");
scanf("%lf",&b);
push (b,c);
printf("your data is : %.2f",c.arr[c.top]);
printf("current stack size : %d",c.top);
if(c.top!=size){
printf("will you insert a data (Y/N) ?\n");
scanf("%s",&a);
}
}
return 0;
}
/* Implementation */
void push(double elmt, stack c)
{
if(c.top!=size) {
c.top = c.top + 1;
c.arr[c.top] = elmt;
}
else {
printf("Stack is full\n");
}
}
Pass the parameters to the function 'push()' with their reference like so..
#include <stdio.h>
#include <stdlib.h>
#define size 5
typedef struct stacktype
{
double arr[size];
int top;
} stack;
/* prototype function */
void push(double elmt,stack *p_c);
/*main program*/
int main(void)
{
char a;
double b;
stack c;
c.top=0;
printf("will you insert a data (Y/N) ?\n");
scanf("%c",&a);
while (a=='Y'){
printf("insert your data :\n");
scanf("%lf",&b);
push(b,&c); // Address of c
printf("your data is : %.2f",c.arr[c.top]);
printf("current stack size : %d",c.top);
if(c.top!=size){
printf("will you insert a data(Y/N) ?\n");
scanf("%s",&a);
}
}
return 0;
}
/* Implementation */
void push(double elmt, stack *p_c)
{
if(p_c->top!=size) {
p_c->top = p_c->top + 1;
p_c->arr[p_c->top] = elmt;
}
else {
printf("Stack is full\n");
}
}