Question

Write a C++ program with the following functionality. Place the even integers from 10 (inclusive) to...

Write a C++ program with the following functionality.

Place the even integers from 10 (inclusive) to 40 (inclusive) in a queue, Q.

Remove the first 10 integers from Q and add to a stack, S.

Pop and print all the elements from S.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Code:

#include <iostream>

#include<stack>

#include<queue>

using namespace std;

int main() {

//created a queue

queue<int> evens;

//pushing values to queue even integers from 10 to 40

for(int i=10;i<=40;i=i+2){

evens.push(i);

}

//created a stack

stack<int> ten;

//pushing the first 10 values to stack

for(int i=0;i<10;i++){

ten.push(evens.front());

evens.pop();

}

//poping the values from stack and printing them until the queue is empty

while(!ten.empty()){

cout<<ten.top()<<" ";

ten.pop();

}

cout<<endl;

}

Screenshot:

main.cpp 1 #include <iostream> 2. #include<stack> 3 #include<queue> 4 using namespace std; 5 6 int main() { //created a queue

Output:

28 26 24 22 20 18 16 14 12 10

Please give it thumbsup and comment below for any problem in the answer

Add a comment
Know the answer?
Add Answer to:
Write a C++ program with the following functionality. Place the even integers from 10 (inclusive) to...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • P 6.1 page 373, Write a program to initialize a list with 10 random integers from...

    P 6.1 page 373, Write a program to initialize a list with 10 random integers from 0 to 1000(inclusive) and output the following: (10 points) import random SEED = int(input("Input seed: ")) random.seed(SEED) print("\na. Every element at an even index") print("\nb. Every even element") print("\nc. All elements in reverse order.") print("\nd. Only the first and the last element.")

  • Q.1. Write a C program that determines whether a line entered by a user is a...

    Q.1. Write a C program that determines whether a line entered by a user is a palindrome or not. You must demonstrate it as an application of stack (you may use linked list implementation demonstrated in the class). Hint! Think of the basic property of a stack i.e. LIFO (list-in-first-out). Q.2. Write a charQueue header file containing all the function headers of following functions: 1- initiateQueue—to initialize a queue 2- enqueue—to add a node at the rear end of the...

  • Using either a Stack or Queue, write a C++ program that uses a for loop to...

    Using either a Stack or Queue, write a C++ program that uses a for loop to push or enqueue 100 integers (values of 1-100) onto a stack or queue. Afterwards, the program should pop or dequeue these values off the stack or queue and write them to one of two textfiles (int.txt and int2.txt) based on whether a value is even or odd. If the popped or dequeued value is even, then that particular value should be written into int2.txt....

  • Write a C++ program to implement a queue using linked lists. You can use the queue...

    Write a C++ program to implement a queue using linked lists. You can use the queue data structure from the Standard Template Library (STL). The program should provide the following functionality: Enqueue data into queue Dequeue data from queue Print data at the front Print data at the back Print the entire queue Check if the queue is empty Print the number of elements in the queue Test your program using at least the following test cases (considering the queue...

  • Write a method called reverseFirstK that accepts an integer k and a queue of integers as...

    Write a method called reverseFirstK that accepts an integer k and a queue of integers as parameters and reverses the order of the first k elements of the queue, leaving the other elements in the same relative order. For example, if a queue named q stores [10, 20 30, 40, 50, 60, 70, 80, 90], the call of reverseFirstK (4, q):should change the queue to store [40, 30 20, 10, 50, 60, 70, 80, 90]. If k is 0 or...

  • (a) Write a C program to print a list of all integers between 1 and 1,000...

    (a) Write a C program to print a list of all integers between 1 and 1,000 (inclusive) which are divisible by 7 but not by 13. The list should be printed to a file called "output.dat". Remember to close the file after use. (35%) (b) Explain what is meant by the C preprocessor. Explain the effect of the following line of code: #define SQUARE (x) (x) * (x) (25%) (c) Explain the concept of an array in C. Write down...

  • Write a Java program that does the following. a. Declare an integer 2D array with 5...

    Write a Java program that does the following. a. Declare an integer 2D array with 5 rows and 5 columns. b. Initialize the array's elements to random integers between 1 and 10 (inclusive). c. Display all the elements in the 2D array as a table of rows and columns. d. Display the row index and column index of all the even integers in the 2D array. e. Display the sum of first row's elements.

  • WRITE A COMPLETE PROGRAM IN C++ A. Ask users to input two positive integers a and...

    WRITE A COMPLETE PROGRAM IN C++ A. Ask users to input two positive integers a and b (a < b). You need to do error checking in this stage to make sure the input is correct. For example, a = 1, b = 50. B. Write a separate function, isEven() to list all even within [a, b] inclusive. C. n the main function, print a, b and the list of even numbers.

  • Can I get a C++ code and output for this program using classes instead of using...

    Can I get a C++ code and output for this program using classes instead of using struct. The following program implements a Last In First Out (LIFO) stack. ( I want to use class for function definitions too and if main need.) #include <iostream> using namespace std; const int MAX = 100; struct stack {    int s[MAX]; // an array of integers    int top; // the index of the last number }; void push(stack &, int); void pop(stack&,...

  • C++ this program will get two integers from the user. The program will 1. call getProduct...

    C++ this program will get two integers from the user. The program will 1. call getProduct (int, int, int &); where the first two arguments are the two input integers and third one is the product of these 2 integers. 2. call printEvenAscending (int, int); where the two arguments are the two input integers. This function will print all -even numbers between the two arguments in an ascending order. Note, the two input integers could be in any order (such...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT