push and pop using array of doubles
I have an assignment that is asking me to fill up a stack with random
variables and pop them out in a FILO order. Whilst I managed to get it to
work using just the int type, it seems that now i need to change it in
order to implement an array of doubles and it would fill the array with
characters using the type double and Keep popping out doubles from the
stack until it is empty and print them. I have tried just changing the
types in both functions but keep getting errors andI'm not sure why. This
is what i have so far by the way.Any help would be appreciated.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#define STACK_SIZE 10
#define STACK_FULL -2
#define STACK_EMPTY -1
#define NORMAL 0
int myerror = NORMAL;
void push(double [],
double, // input - data being pushed onto the stack
double **, // input/output - pointer to pointer to the top of stack
int); // constant - maximum capacity of stack
double // output - data being popped out from the stack
pop(double [], // input/output - the stack
double **); // input/output - pointer to pointer to top of stack
void push(double stack[],
double item,
double **top,
int max_size){
stack[++(**top)] = item;
}
double pop(double stack[],
double **top){
return stack[(**top)--];
return 0.0;
}
int main(){
double s[STACK_SIZE];
double *s_top = NULL;
srand(time(NULL));
char randChar = ' ';
double i = 0;
double j=0;
double randNum = 0;
for (i = 0; i < STACK_SIZE; i++){
randNum = 33 + (double)(rand() % ((126-33)+ 1 ));
randChar = (double) randNum;
push(s,randChar, &(*s_top), STACK_SIZE);
printf ("Random characters: %c\n", randChar);}
printf("-----------\n");
for(j=STACK_SIZE; j>0; j--){
printf("Random characters: %c\n", pop(s, & *s_top));
}
return 0;
}
No comments:
Post a Comment