C++ Queue Exercises: Mean, variance and standard deviation of all elements of a queue
C++ Queue: Exercise-8 with Solution
Write a C++ program to find the mean, variance and standard deviation of all elements of a queue.
Sample Solution:
C Code:
#include <iostream>
#include <map>
#include <cmath>
using namespace std;
const int MAX_SIZE = 100;
class Queue {
private:
int front; // Front index of the queue
int rear; // Rear index of the queue
int arr[MAX_SIZE]; // Array to store elements
public:
Queue() {
front = -1; // Initialize front index to -1
rear = -1; // Initialize rear index to -1
}
bool isFull() {
return (rear == MAX_SIZE - 1); // Check if the queue is full
}
bool isEmpty() {
return (front == -1 && rear == -1); // Check if the queue is empty
}
void enqueue(int x) {
if (isFull()) {
cout << "Error: Queue is full" << endl; // Display error message if the queue is full
return;
}
if (isEmpty()) {
front = 0;
rear = 0;
} else {
rear++;
}
arr[rear] = x; // Insert the element at the rear index
}
void dequeue() {
if (isEmpty()) {
cout << "Error: Queue is empty" << endl; // Display error message if the queue is empty
return;
}
if (front == rear) {
front = -1;
rear = -1;
} else {
front++;
}
}
int peek() {
if (isEmpty()) {
cout << "Error: Queue is empty" << endl; // Display error message if the queue is empty
return -1;
}
return arr[front]; // Return the element at the front of the queue
}
void display() {
if (isEmpty()) {
cout << "Error: Queue is empty" << endl; // Display error message if the queue is empty
return;
}
cout << "Queue elements are: ";
for (int i = front; i <= rear; i++) {
cout << arr[i] << " "; // Display all elements in the queue
}
cout << endl;
}
float getMean() {
if (isEmpty()) {
cout << "Error: Queue is empty" << endl; // Display error message if the queue is empty
return 0;
}
float sum = 0;
for (int i = front; i <= rear; i++) {
sum += arr[i]; // Calculate sum of elements in the queue
}
return sum / (rear - front + 1); // Calculate and return the mean
}
float getVariance() {
if (isEmpty()) {
cout << "Error: Queue is empty" << endl; // Display error message if the queue is empty
return 0;
}
float mean = getMean();
float variance = 0;
for (int i = front; i <= rear; i++) {
variance += pow(arr[i] - mean, 2); // Calculate sum of squared differences from mean
}
return variance / (rear - front + 1); // Calculate and return the variance
}
float getStdDev() {
return sqrt(getVariance()); // Calculate and return the standard deviation using variance
}
};
int main() {
cout << "Initialize a Queue." << endl;
Queue q;
cout << "\nInsert some elements into the queue:" << endl;
q.enqueue(1);
q.enqueue(2);
q.enqueue(3);
q.enqueue(4);
q.enqueue(5);
q.display();
// Calculate and display mean, variance, and standard deviation of queue elements
cout << "Mean: " << q.getMean() << endl;
cout << "Variance: " << q.getVariance() << endl;
cout << "Standard Deviation: " << q.getStdDev() << endl;
cout << "\nRemove two elements from the said queue: " << q.getStdDev() << endl;
q.dequeue();
q.dequeue();
q.display();
// Calculate and display mean, variance, and standard deviation after removing elements
cout << "Mean: " << q.getMean() << endl;
cout << "Variance: " << q.getVariance() << endl;
cout << "Standard Deviation: " << q.getStdDev() << endl;
return 0;
}
Sample Output:
Initialize a Queue. Insert some elements into the queue: Queue elements are: 1 2 3 4 5 Mean: 3 Variance: 2 Standard Deviation: 1.41421 Remove two elements from the said queue: 1.41421 Queue elements are: 3 4 5 Mean: 4 Variance: 0.666667 Standard Deviation: 0.816497
Flowchart:
CPP Code Editor:
Contribute your code and comments through Disqus.
Previous C++ Exercise: Find the mode of all elements of a queue.
Next C++ Exercise: Find the maximum element of a queue.
What is the difficulty level of this exercise?
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://w3resource.com/cpp-exercises/queue/cpp-queue-exercise-8.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics