Question

In: Computer Science

In C Programing Create a stack using an array. Define the index variable of the array...

  1. In C Programing Create a stack using an array. Define the index variable of the array name to be stacktop. Initially set stacktop to -1. Next create two functions push and pop. Both take as input 3 items: a pointer to the stack in memory, stacktop, and maxstack.
  2. Make sure to inc stacktop by one and also remember that stacktop[0] is the bottom of the stack and by stack rule cannot be accessed until all the other items are popped.
  3. Your program should push and pop values between 1 and 1000 until a negative input is encountered. When the program incurs a negative number, it should pop all remaining stack items until the stack is empty
  4. Next change the data field in the program to be char type. Then push the letters of your name one at a time. When you have finished entering them, pop each one off and print them. You will see that this demonstrates the reversing effect of a stack.

Solutions

Expert Solution

C program with int data field of stack (for inputs from 1 - 1000):

#include <stdio.h>
#define MAX_LEN 100   //it is the max length of the stack 


//function to push elements to stack
//parameters: stack array, value to be pushed, pointer to stack top
//return: void
void push (int *stack, int value, int *stackTop){

    ///if the stack is full
    if (*stackTop == MAX_LEN) {
        printf("Stack is full.\n\n");
        return ;
    }

    //increment the top of the stack
    *stackTop = *stackTop +1;

    //push the new value to stack
    stack[*stackTop] = value; 
    printf("Pushed %d to stack\n\n", value);

}


//function to pop elements from stack
//parameters: stack array,  pointer to stack top
//return: popped element
int pop (int *stack, int *stackTop) {

    //if stack is empty
    if ( *stackTop == -1) {
        printf("Stack is Empty\n\n");
        return -1 ;
    }


     int temp = stack[*stackTop] ;
    
    //decrement the top of the stack
     *stackTop = *stackTop -1;

     //return the popped element
     return temp ;

}

int main(){
   
  int stack[5000];
  int stackTop = -1 ;
  int input = 0 ;
  

  //take continuous input from user until a negative number is entered
  while(1){
    printf("1) PUSH \n2) POP \nChoose an option to perform your task: ");
    int op;
    scanf("%d", &op);

    //push
    if (op == 1) { 
        printf("Enter a number between 1 - 1000 (inclusive): ") ;
        scanf("%d", &input) ;

        //check is eneterd value is valid
        if ( input >0 && input < 1001) {
            push(stack, input, &stackTop);
        }

        //if its is negative
        else if(input < 0) break;
        //if its positive but out of range
        else {
            printf("Error: Number should be between 1 to 1000 (inclusive) \n\n");
        }
    }
    //pop
    else if (op == 2) { 
        int popped_elem = pop(stack, &stackTop);
        if(popped_elem != -1){
            printf("Popped %d from stack\n\n", popped_elem);
        }
    }
    
    else {
        printf("Invalid Choice. \n\n");
    }
  }
 
 printf("You entered a negative number\nPoppping all elements from stack:\n");

 //popping all elements from stack
 while(stackTop > -1){
     int popped_elem = pop(stack, &stackTop);
     printf("%d\n", popped_elem);
 }
printf("Now stack is empty!!\n");
}

Output:

C program with char data field of stack (for inputing your Name):

#include <stdio.h>
#define MAX_LEN 100  //it is the max length of the stack 

//function to push elements to stack
//parameters: stack array, value to be pushed, pointer to stack top
//return: void
void push (char *stack, char value, int *stackTop){

    // if stack is full 
    if (*stackTop == MAX_LEN) {
        printf("Stack is full.\n\n");
        return ;
    }
 
  //increment the top of the stack
    *stackTop = *stackTop +1;
    stack[*stackTop] = value; 
    printf("Pushed %c to stack\n\n", value);

}

//function to pop elements from stack
//parameters: stack array,  pointer to stack top
//return: popped element
char pop (char *stack, int *stackTop) {

     //if stack is empty
    if ( *stackTop == -1) {
        printf("Stack is Empty\n\n");
        return '\0';
    }
     char temp = stack[*stackTop] ;

     //decrement the top of the stack
     *stackTop = *stackTop -1;

     //return the popped element
     return temp ;

}

int main(){
   
  char stack[5000];
  int stackTop = -1 ;
  char input ;


//take continuous input from user until a negative number is entered
  while(1){
    printf("1) PUSH \n2) POP \nChoose an option to perform your task: ");
    int op;
    scanf("%d", &op);
    getchar();

    //push
    if (op == 1) { 
        printf("Enter a character(alphabet): ") ;
        scanf("%c", &input) ;
        
       // check if the input is an alphabet
        if ( (input >= 65 && input < 91) || (input >= 97 && input < 123)) {
            push(stack, input, &stackTop);
        }

        // if it is not an alphabet break the loop
        else  break;
    }
   // POP
    else if (op == 2) { 
        char popped_elem = pop(stack, &stackTop);
        if(popped_elem != '\0'){
            printf("Popped %c from stack\n\n", popped_elem);
        }
    }
    
    else {
        printf("Invalid Choice. \n\n");
    }
  }
 
 printf("You entered a n invalid character\nPoppping all elements from stack:\n");
 //popping all elements from stack
 while(stackTop > -1){
     char popped_elem = pop(stack, &stackTop);
     printf("%c\n", popped_elem);
 }
printf("Now stack is empty!!\n");
}

OUTPUT:

if you find this solution helpful, do upvote as it motivates me to post more answers.


Related Solutions

Implement a stack in C++ using an array, not an array list. Make your stack size...
Implement a stack in C++ using an array, not an array list. Make your stack size 5 when you test it, but do not hardcode this! You should be able to change the size for testing purposes with the change of one variable. DO NOT use Stack class defined in C++ Implement the following methods in your stack class. stack() creates an empty stacks, stack s is new and empty. push(item) adds a new item to the stack s, stacks...
in C++ For this program, you are going to implement a stack using an array and...
in C++ For this program, you are going to implement a stack using an array and dynamic memory allocation. A stack is a special type of data structure that takes in values (in our case integers) one at a time and processes them in a special order. Specifically, a stack is what's called a first-in-last-out (FILO) data structure. That is to say, the first integer inserted into the stack is the last value to be processed. The last value in...
This is C++ programing Reversing the elements of an array involves swapping the corresponding elements of...
This is C++ programing Reversing the elements of an array involves swapping the corresponding elements of the array: the first with the last, the second with the next to the last, and so on, all the way to the middle of the array.Given an array a, an int variable n containing the number of elements in a, and two other intvariables, k and temp, write a loop that reverses the elements of the array.Do not use any other variables besides...
In this lab, using C++, you will create two data structures: a stack and a queue....
In this lab, using C++, you will create two data structures: a stack and a queue. You will use STL containers to demonstrate basic ADTs. Queue For the queue, you will simulate a buffer. Remember it is first-in-first-out. The user will enter a number for the number of rounds to run your simulation. You need one function that randomly generates a number. You will also have a user specified percentage, and the function uses this percentage to randomly put the...
In C++ using a single dimensional array Create a program that uses a for loop to...
In C++ using a single dimensional array Create a program that uses a for loop to input the day, the high temperature, and low temperature for each day of the week. The day, high, and low will be placed into three elements of the array. For each loop the day, high, and low will be placed into the next set of elements of the array. After the days and temps for all seven days have been entered into the array,...
Explain this code: The structure used is max heap using array. C++ (i is the index...
Explain this code: The structure used is max heap using array. C++ (i is the index of the element to be deleted) void del(int i) {    int left,right,temp;    arr[i]=arr[n-1];    n=n-1;    left=2*i+1; /*left child of i*/    right=2*i+2; /* right child of i*/    while(right < n)    {        if( arr[i]>=arr[left] && arr[i]>=arr[right] )            return;        if( arr[right]<=arr[left] )        {            temp=arr[i];            arr[i]=arr[left];   ...
Using C++ language, create a program that uses a struct with array variables that will loop...
Using C++ language, create a program that uses a struct with array variables that will loop at least 3 times and get the below information: First Name Last Name Job Title Employee Number Hours Worked Hourly Wage Number of Deductions Claimed Then, determine if the person is entitled to overtime and gross pay. Afterwards, determine the tax and net pay. Output everything to the screen. Use functions wherever possible. Bonus Points: Use an input file to read in an unknown...
In C++ Create a dynamic array of 100 integer values named myNums. Use a pointer variable...
In C++ Create a dynamic array of 100 integer values named myNums. Use a pointer variable (like ptr) which points to this array. Use this pointer variable to initialize the myNums array from 2 to 200 and then display the array elements. Delete the dynamic array myNums at the end. You just need to write part of the program.
Using the Stack ADT: Create a program that uses a stack. Your program should ask the...
Using the Stack ADT: Create a program that uses a stack. Your program should ask the user to input a few lines of text and then outputs strings in reverse order of entry. (Optional) Create a similar program that uses a stack. Your new program should ask the user to input a line of text and then it should print out the line of text in reverse. To do this your application should use a stack of Character. In Java...
Create a C program that performs the following (please comment the codes): a) Create a Stack...
Create a C program that performs the following (please comment the codes): a) Create a Stack ADT. Stack should be implemented using the linked list. b) Enter 10 random integer numbers between 0 to 50 in the stack. c) After pushing each element, print the content of the top of the stack. c) Then pop out those 10 integer numbers and print those numbers. d) Finally destroy the Stack.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT