Open anishcana opened 3 months ago
class FixedSizeBuffer { private: int arr[10]; // Fixed size array to hold up to 10 values int size; // Current number of elements in the buffer int start; // Start index of the buffer
public: FixedSizeBuffer() : size(0), start(0) {}
void addValue(int value) {
if (size < 10) {
arr[(start + size) % 10] = value;
++size;
} else {
start = (start + 1) % 10; // Move the start index forward in a circular manner
arr[(start + 9) % 10] = value; // Add the new value at the correct position
}
}
void checkDifference() {
if (size < 4) {
std::cout << "Buffer does not have enough elements." << std::endl;
return;
}
int firstValue = arr[start];
int fourthValue = arr[(start + 3) % 10];
int difference = std::abs(firstValue - fourthValue);
if (difference > 25) {
std::cout << "Difference between 1st and 4th value is greater than 25." << std::endl;
// Do something here
} else {
std::cout << "Difference between 1st and 4th value is not greater than 25." << std::endl;
}
}
void printBuffer() {
std::cout << "Buffer: ";
for (int i = 0; i < size; ++i) {
std::cout << arr[(start + i) % 10] << " ";
}
std::cout << std::endl;
}
};
int main() { FixedSizeBuffer buffer;
buffer.addValue(10);
buffer.addValue(-20);
buffer.addValue(30);
buffer.addValue(75);
buffer.addValue(100);
buffer.addValue(5); // Adding more values to demonstrate overwriting
buffer.addValue(-35);
buffer.addValue(60);
buffer.addValue(90);
buffer.addValue(15);
buffer.addValue(50); // This will overwrite the first value (10)
buffer.printBuffer(); // Print the current buffer state
buffer.checkDifference(); // Check difference between 1st and 4th value in the buffer
return 0;
}
void checkDifference(std::queue
std::queue<int> tempQueue = q; // Create a copy of the queue
int lastElement, fourthLastElement;
// Get the last element
for (size_t i = 0; i < q.size() - 1; ++i) {
tempQueue.pop();
}
lastElement = tempQueue.front();
// Reset the tempQueue to the original state
tempQueue = q;
// Get the 4th last element
for (size_t i = 0; i < q.size() - 4; ++i) {
tempQueue.pop();
}
fourthLastElement = tempQueue.front();
int difference = std::abs(lastElement - fourthLastElement);
std::cout << "The absolute difference between the last element and the 4th last element is: " << difference << std::endl;
}
int main() {
// Create a queue of integers
std::queue
// Push at least 10 elements onto the queue, can include negative values
int elements[] = {5, -3, 7, 8, -2, 10, -6, 4, -1, 9};
for (int i = 0; i < 10; ++i) {
fifoQueue.push(elements[i]);
}
try {
// Check the absolute difference between the last element and the 4th last element
checkDifference(fifoQueue);
} catch (const std::invalid_argument &e) {
std::cerr << e.what() << std::endl;
}
return 0;
}
void checkDifference(std::stack
// Temporary stack to hold the elements
std::stack<int> tempStack;
int lastElement;
int fourthLastElement;
// Pop elements to reach the last element
while (s.size() > 1) {
tempStack.push(s.top());
s.pop();
}
lastElement = s.top();
tempStack.push(s.top());
s.pop();
// Pop elements from tempStack to reach the 4th last element
while (tempStack.size() > 3) {
s.push(tempStack.top());
tempStack.pop();
}
fourthLastElement = tempStack.top();
// Restore the original stack
while (!tempStack.empty()) {
s.push(tempStack.top());
tempStack.pop();
}
int difference = std::abs(lastElement - fourthLastElement);
std::cout << "The absolute difference between the last element and the 4th last element is: " << difference << std::endl;
}
int main() {
// Create a stack of integers
std::stack
// Push at least 10 elements onto the stack, can include negative values
int elements[] = {5, -3, 7, 8, -2, 10, -6, 4, -1, 9};
for (int i = 0; i < 10; ++i) {
lifoStack.push(elements[i]);
}
try {
// Check the absolute difference between the last element and the 4th last element
checkDifference(lifoStack);
} catch (const std::invalid_argument &e) {
std::cerr << e.what() << std::endl;
}
return 0;
}
// Function to get the nth element from the end of the stack
int getNthFromEnd(std::stack
std::stack<int> tempStack;
int nthElement;
// Pop elements until we reach the nth element from the end
for (int i = 0; i < n - 1; ++i) {
tempStack.push(s.top());
s.pop();
}
nthElement = s.top();
// Restore the original stack
while (!tempStack.empty()) {
s.push(tempStack.top());
tempStack.pop();
}
return nthElement;
}
// Function to check the difference between the last element and the 4th last element
void checkDifference(std::stack
int difference = std::abs(lastElement - fourthLastElement);
std::cout << "The absolute difference between the last element and the 4th last element is: " << difference << std::endl;
}
int main() {
// Create a stack of integers
std::stack
// Push at least 10 elements onto the stack, can include negative values
int elements[] = {5, -3, 7, 8, -2, 10, -6, 4, -1, 9};
for (int i = 0; i < 10; ++i) {
lifoStack.push(elements[i]);
}
try {
// Check the absolute difference between the last element and the 4th last element
checkDifference(lifoStack);
} catch (const std::invalid_argument &e) {
std::cerr << e.what() << std::endl;
}
return 0;
}
// Function to shift elements in the array to the left void shiftLeft(int arr[], int size) { for (int i = 0; i < size - 1; ++i) { arr[i] = arr[i + 1]; } }
// Function to insert a new element at the end of the array void insertAtEnd(int arr[], int& size, int newElement, int capacity) { if (size >= capacity) { std::cerr << "Array is full. Cannot insert new element.\n"; return; }
// Shift all elements to the left
shiftLeft(arr, size);
// Insert the new element at the end of the array
arr[size - 1] = newElement;
++size;
}
int main() { const int capacity = 5; int arr[capacity] = {1, 2, 3, 4}; // Initialize array with elements int size = 4; // Current number of elements in the array
int newElement = 10;
std::cout << "Before insertion: ";
for (int i = 0; i < size; ++i) {
std::cout << arr[i] << " ";
}
std::cout << std::endl;
insertAtEnd(arr, size, newElement, capacity);
std::cout << "After insertion: ";
for (int i = 0; i < size; ++i) {
std::cout << arr[i] << " ";
}
std::cout << std::endl;
return 0;
}
include
include
include
include
void checkDifference(std::stack &s) {
if (s.size() < 4) {
throw std::invalid_argument("The stack has fewer than 4 elements.");
}
}
int main() { // Create a stack of integers std::stack lifoStack;
}