Question

In: Computer Science

Write a program in C or C++ that takes a number series of size n (n...

Write a program in C or C++ that takes a number series of size n (n integers) as
input from the user, push all the numbers to the stack, and reverse the stack using recursion. Please note
that this is not simply popping and printing the numbers, but the program should manipulate the stack to
have the numbers stored in reverse order. In addition to the provided header file, the students can use
the following function to print the content of the stack:

Solutions

Expert Solution

#include <stdio.h>
#define MAXSIZE 50
int size; 

// structure for stack
struct Stack {
    int top;
    int elements[MAXSIZE];
} s;
 
// empty stack. top=-1
void init() {
 s.top = -1;
}
 
// check if stack is full or not
int isFull() {   
    if(s.top >= MAXSIZE-1)
        return 1;
    else
        return 0;
}
 
// check if stack is empty or not
int isEmpty() {
 if(s.top == -1)
     return 1;
 else
     return 0;
}
 
 
// display the stack
void displayStack(){
        if(!isEmpty()){
                for(int i=s.top;i>=0;--i)
                        printf("%d\n",s.elements[i]);
        }
}


//push element in the stack
void push(int ele) {
    if (isFull())
        printf("Stack is Full\n");
    else {
        s.elements[s.top + 1] = ele;
        s.top++;
    }
}
 
//pop top element from stack
int pop() {
    if (isEmpty())
        printf("Stack is Empty...\n");
    else {
     s.top = s.top - 1;
        return s.elements[s.top+1];
    }
}

// insert at bottom
void insertAtBottom(int ele) {
    // check if stack is empty
        if (isEmpty())
        push(ele); // psuh elemnet
    else {
        // store top element
        int t = pop();
        // insert at bottom
                insertAtBottom(ele);
        // push top element
        push(t);
    }
}



// reverse the stack using recursion
void doReverse() {
    if (!isEmpty()) {
        // pop top element using recursion 
        // until stack is empty
        int t = pop();
        doReverse();
        // insert element at bottom 
        insertAtBottom(t);
    }
}

 
int main() {
        
        // declare variables
        int i, j, element;
        // take size of stack as input
        printf("Enter the size of stack: ");
        scanf("%d",&size);
        
        // initialize the stack
    init();
    
    // take input from user
    // for stack elements
    i=0;
    while(i<size){
        printf("Enter a element to push in the stack: ");
            scanf("%d",&element);
                push(element);
                printf("Element %d has been pushed.\n",element);
                i++;
        }
        // display the stack
    printf("\nUser given stack: \n");
    // display the current stack
        displayStack();
    
        // call this function to reverse the stack
        doReverse();
    printf("\nReversed Stack: \n");
    displayStack();
    
    return 0;
}

___________________________________________________________________

Enter the size of stack: 5
Enter a element to push in the stack: 2
Element 2 has been pushed.
Enter a element to push in the stack: 4
Element 4 has been pushed.
Enter a element to push in the stack: 6
Element 6 has been pushed.
Enter a element to push in the stack: 8
Element 8 has been pushed.
Enter a element to push in the stack: 10
Element 10 has been pushed.

User given stack:
10
8
6
4
2

Reversed Stack:
2
4
6
8
10

___________________________________________________________________


Note: If you have queries or confusion regarding this question, please leave a comment. I would be happy to help you. If you find it to be useful, please upvote.


Related Solutions

Write a program (O(n), where n is the number of words) that takes as input a...
Write a program (O(n), where n is the number of words) that takes as input a set of words and returns groups of anagrams for those words. Complete your code here Do not change anything in the test file. CPP File: #include #include #include #include using namespace std; vector> findAnagrams(const vector& dict); vector> findAnagrams(const vector& dict) { // Your code here... } Test File: #include #include #include #include using namespace std; vector> findAnagrams(const vector& dict); int main() { vector word_list...
1. Write a program in C++ that takes as inputs a positiveinteger n and a...
1. Write a program in C++ that takes as inputs a positive integer n and a positive double a. The function should compute the geometric sum with base a up to the powern and stores the result as a protected variable. That is, the sum is: 1 + ? + ? ^2 + ? ^3 + ? ^4 + ⋯ + ? ^?2.  Write a program in C++ that takes as input a positive integer n and computes the following productsum...
Write a program that takes a set of m numbers up to size n and save...
Write a program that takes a set of m numbers up to size n and save them in an array. The program then allows you to search for any number in the array with O(1).
Write a program in C or in Java, that takes an integer value N from the...
Write a program in C or in Java, that takes an integer value N from the command line, generates N random points in the unit square, and computes the distance separating the closest pair of points. A unit square is a square with sides of length 1, at points (0, 0), (0, 1), (1, 0), and (1, 1). If you wish to avoid the command-line processing, you can just assume you will generate a fixed number of points, say between...
(8%) Write a C/C++ program that takes an input (array) from 1 to n (say n...
(8%) Write a C/C++ program that takes an input (array) from 1 to n (say n = 50) and displays the string representations of those numbers with following conditions If the current number is divisible by 2, then print CSU If the current number is divisible by 5, then print SB If the current number is divisible by both 2 and 5, then print CSUSB If the number is neither divisible by 2 nor 5, then print the number Example:...
2. Write a c++ program that takes from the user the ​number of courses​ and constructs...
2. Write a c++ program that takes from the user the ​number of courses​ and constructs 3 ​dynamic 1D arrays​ with size courses+1. Each array represents a student. Each cell in the array represents a student’s mark in a course. In the last cell of each 1D array you should calculate the average mark of that student. Then output the average mark of all students in each course. Delete any allocated memory. Example Number of courses : 4 50 60...
Using loop statements, write a C++ program which takes the number of items that a shopper...
Using loop statements, write a C++ program which takes the number of items that a shopper wants to buy, and then takes the price of each item, and at the end tells the shopper how much she must pay. This is a sample of the output: How many items do you have in your basket? 3 Enter the price in dollar? 10.25 Enter the price in dollar? 20.75 Enter the price in dollar? 67.5 You must pay 98.5 $ today.
Write a Java program that takes in a string and a number and prints back the...
Write a Java program that takes in a string and a number and prints back the string from the number repeatedly until the first character... for example Pasadena and 4 will print PasaPasPaP. Ask the user for the string and a number Print back the string from the number repeatedly until the first character For both programs please utilize: methods arrays loops Turn in screenshots
C Language - Programming Write a function that takes an array of ints, and the size...
C Language - Programming Write a function that takes an array of ints, and the size of the array – another int. It also returns a double. Call this one ‘average.’ Return a double that is the average of the values in the array. Demonstrate that it works by finding the average of an array with these values {78, 90, 56, 99, 88, 68, 92} Write a function that takes one double parameter, and returns a char. The parameter represents...
Write a program to reverse each integer number on array (size 3) using while loop. C++...
Write a program to reverse each integer number on array (size 3) using while loop. C++ Input: 3678 2390 1783 Output: 8763 0932 3871
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT