Answer to Question #212760 in C++ for tatheer

Question #212760

Create a class template for a class named Queue that holds

• A single data member as an array named list of size 10 to store certain elements

• A constructor to initialize queue with value 0 or 0.0 at all its indexes

• Three member functions i.e. sort() to sort the elements of the queue, max() that returns the maximum value present in the queue, min() that returns the smallest value present in the queue, and return_queue() that returns all the elements of the queue (in case of there being multiple maximum and minimum values present in the queue, the member functions should return notify the user of the number of times that value has been repeated in the queue and then return that value to the main function).

In the main() function, create three objects with different data types of class queue and test the functionality of member functions for various values of data members for these objects.


1
Expert's answer
2021-07-02T11:43:41-0400
#include <iostream>
using namespace std;
template<typename type>
class Queue{
    type list[10];
    public:
    Queue(){
        for(int i = 0; i < 10; i++) list[i] = (type)0;
    }
    Queue(type *a){
        for(int i = 0; i < 10; i++) list[i] = a[i];
    }
    void Sort(){
    for (int i = 0; i < 9; i++)
        for (int j = 9; i < j; j--)
            if (list[j] < list[j - 1])
              swap(list[j], list[j - 1]);
    }
    void check(type m){
        int freq = 0;
        for(int i = 0; i < 10; i++){
            if(list[i] == m) freq++;
        }
        if(freq > 1) cout<<endl<<m<<" has been repeated "<<freq<<" times\n";
    }
    type min(){
        type m = (type)INT_MAX;
        for(int i = 0; i < 10; i++){
            if(list[i] < m) m = list[i];
        }
        check(m);
        return m;
    }
    type max(){
        type m = (type)INT_MIN;
        for(int i = 0; i < 10; i++){
            if(list[i] > m) m = list[i];
        }
        check(m);
        return m;
    }
    type* return_queue(){
        return list;
    }
};
int main(){
    float b[10] = {1.5, 2, 3, 4, 7.8, 7.8, 7.8, 7.8, 0, 0};
    double c[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10.0045};


    Queue<int> queue1;
    Queue<float> queue2(b);
    Queue<double> queue3(c);


    cout<<"Integers";
    cout<<"\nMin: "<<queue1.min();
    cout<<"\nMax: "<<queue1.max()<<endl;
    cout<<"Elements: ";
    for(int i = 0; i < 10; i++) cout<<(queue1.return_queue())[i]<<" ";


    cout<<"\n\nFloats";
    cout<<"\nMin: "<<queue2.min();
    cout<<"\nMax: "<<queue2.max()<<endl;
    cout<<"Elements: ";
    for(int i = 0; i < 10; i++) cout<<(queue2.return_queue())[i]<<" ";


    cout<<"\n\nDouble";
    cout<<"\nMin: "<<queue3.min();
    cout<<"\nMax: "<<queue3.max()<<endl;
    cout<<"Elements: ";
    for(int i = 0; i < 10; i++) cout<<(queue3.return_queue())[i]<<" ";
    return 0;
}

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!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS