Question

In: Computer Science

Given a queue of integers, write an algorithm and the program in c++ that, using only...

Given a queue of integers, write an algorithm and the program in c++ that, using only the queue ADT, calculates and prints the sum and the average of the integers in the queue without changing the contents of the queue.

Solutions

Expert Solution

// C++ program to calculate sum and average of the integers in the queue without changing the contents of the queue.

#include <iostream>

#include <queue>

using namespace std;

int main() {

       queue<int> q; // create an empty queue of integers

       int sum = 0;

       // push elements 1 to 10 in q such that 1 is at the front and 10 is at the end

       for(int i=1;i<=10;i++)

             q.push(i);

       // create another temporary queue which is empty initially

       queue<int> temp;

       // loop that removes the element from q and inserts into the temporary queue and add the element to the sum

       // queue removes the element from front and inserts the element at the back

       // so temp will contain the element in the same order as present in q

       while(!q.empty())

       {

             int num = q.front(); // get the front element of the queue

             q.pop(); // remove the front element from the queue

             sum += num; // add the num to sum

             temp.push(num); // push the num to temp queue

       }

       // loop that continues till temp is not empty

       // q will contain the element in the same order as it was present initially

       while(!temp.empty())

       {

             q.push(temp.front()); // push the front element of temp to q

             temp.pop(); // remove the front element from temp

       }

       // print the sum and average

       cout<<"Sum : "<<sum<<endl;

       cout<<"Average : "<<((double)sum)/q.size()<<endl;

       return 0;

}

//end of program

Output:


Related Solutions

Write a c++program using queue to find the minimum value in a queue. Use the above...
Write a c++program using queue to find the minimum value in a queue. Use the above program for implementing queue. You must use dequeue function to read values from queue.  
((by C++ ))Write a program that will reverse the content of a Queue using the following...
((by C++ ))Write a program that will reverse the content of a Queue using the following standard queue operations. enqueue(x) : Add an item x to rear of queue. dequeue() : Remove an item from front of queue. empty() : Checks if a queue is empty or not. For reversing the queue one approach could be to store the elements of the queue in a temporary data structure in a manner such that if we re-insert the elements in the...
Given two unsorted arrays of integers. a) Write a pseudocode algorithm which will output only the...
Given two unsorted arrays of integers. a) Write a pseudocode algorithm which will output only the integers not common to both arrays. When writing pseudocode, consider that no implementation for data structures or algorithms exist. b) Implement your algorithm in Modern C++
In C++ Please, using only the libraries given in this homework prompt, Write a program that...
In C++ Please, using only the libraries given in this homework prompt, Write a program that (1) prompts for two integers, (2) prints out their sum, (3) prints out the first divided by the second, and (4) prints out the natural log of the first number raised to the power of the second number. #include <iostream> #include <iomanip> using namespace std; int main() { <your answer goes here> return 0; }
Write a MIPS program using the Bubble Sort algorithm, that sorts an input list of integers...
Write a MIPS program using the Bubble Sort algorithm, that sorts an input list of integers by repeatedly calling a “swap” subroutine. The original unsorted list of integers should be received from the keyboard input. Your program should first prompt the user “Please input an integer for the number of elements:”. After the user enters a number and return, your program outputs message “Now input each element and then a return:”. For example, if the user enters 5 as the...
Write a C++ program to find the number of pairs of integers in a given array...
Write a C++ program to find the number of pairs of integers in a given array of integers whose sum is equal to a specified number.
Write a C++ program that prints a calendar for a given year. ONLY USING "#include<iostream>" and...
Write a C++ program that prints a calendar for a given year. ONLY USING "#include<iostream>" and "#include<cmath>" The program prompts the user for two inputs:       1) The year for which you are generating the calendar.       2) The day of the week that January first is on, you will use the following notation to set the day of the week:       0 Sunday                     1 Monday                   2 Tuesday                   3 Wednesday       4 Thursday                 5 Friday                      6 Saturday Your program should...
Write a program that performs a merge-sort algorithm without using a recursion. c++ programming language(Only #inlclude...
Write a program that performs a merge-sort algorithm without using a recursion. c++ programming language(Only #inlclude <iostream>)
Program – version 1: Sum of Range Algorithm Write a program that will sum the integers...
Program – version 1: Sum of Range Algorithm Write a program that will sum the integers between a given range (limit your range from 0 to 50). For example, if the user want to add the integers between (and including) 1 and 10, then the program should add: 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10 Algorithm: Program title/description Ask the user to input a start value in the...
Write a C++ program to find K largest elements in a given array of integers. For...
Write a C++ program to find K largest elements in a given array of integers. For eeample, if K is 3, then your program should ouput the largest 3 numbers in teh array. Your program is not supposed to use any additional array.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT