Question #111815

– Write a C++ program that asks the user to input an integer n followed by n other decimals representing the ages of some people. These ages will be stored in a stack. The program will then read this stack and do the followings: if the age read is greater or equal to 20 then the program will insert in a queue the word OLD, otherwise it will insert in this queue the word YOUNG. The program will finally display on the screen the content of the stack and the queue.

Expert's answer

#include <iostream>
#include <stack>
#include <queue>

using namespace std;

int main() {
    int n;
    int age;
    stack<int> s;
    queue<int> OLD;
    queue<int> YOUNG;
    cout << "input n";
    cin >> n;
    cout << "input ages";
    for (int i = 0; i < n; ++i) {
        cin >> age;
        s.push(age);
    }

    while (!s.empty()) {
        age = s.top();
        if (age >= 20) {
            OLD.push(age);
        } else {
            YOUNG.push(age);
        }

        s.pop();
    }

    cout << "YOUNG: ";

    while (!YOUNG.empty()) {
        cout << YOUNG.front() << " ";
        YOUNG.pop();
    }

    cout << "OLD: ";

    while (!OLD.empty()) {
        cout << OLD.front() << " ";
        OLD.pop();
    }

}

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!

LATEST TUTORIALS
APPROVED BY CLIENTS