0% found this document useful (0 votes)
13 views

DSA Paper Solution

Uploaded by

myselfdanish.t
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

DSA Paper Solution

Uploaded by

myselfdanish.t
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 5

solution 1:

#include <iostream>
#include <queue>
#include <string>

using namespace std;

class RestaurantOrderManager {
private:
queue<string> orders;
const int maxCapacity = 5;

public:
void addOrder(const string& order) {
if (orders.size() < maxCapacity) {
orders.push(order);
cout << "Order added: " << order << endl;
} else {
cout << "Cannot add order. Order list is full!" << endl;
}
displayOrders();
}

void serveOrder() {
if (!orders.empty()) {
cout << "Serving order: " << orders.front() << endl;
orders.pop();
} else {
cout << "No orders to serve. The list is empty!" << endl;
}
displayOrders();
}

void displayOrders() {
cout << "Current orders: ";
if (orders.empty()) {
cout << "None" << endl;
} else {
queue<string> temp = orders; // Create a copy to display
while (!temp.empty()) {
cout << temp.front() << " ";
temp.pop();
}
cout << endl;
}
}
};

int main() {
RestaurantOrderManager manager;

// Adding 5 orders
manager.addOrder("Burger");
manager.addOrder("Pizza");
manager.addOrder("Pasta");
manager.addOrder("Salad");
manager.addOrder("Soda");

// Try to add one more order


manager.addOrder("Ice Cream");

// Serving 2 orders
manager.serveOrder();
manager.serveOrder();

return 0;
}

solution 2:

#include <iostream>
#include <vector>
#include <string>

using namespace std;

struct InventoryItem {
string name;
int quantity;
double price;
};

class InventoryManager {
private:
vector<InventoryItem> items;

public:
void addItem(const string& name, int quantity, double price) {
InventoryItem item = { name, quantity, price };
items.push_back(item);
cout << "Added item: " << name << endl;
}

void removeItem(const string& name) {


for (auto& item : items) {
if (item.name == name) {
if (item.quantity > 0) {
item.quantity--;
cout << "Removed 1 from item: " << name << ". Remaining
quantity: " << item.quantity << endl;
if (item.quantity == 0) {
cout << "Item " << name << " is out of stock." << endl;
}
} else {
cout << "Item " << name << " is already out of stock." << endl;
}
return;
}
}
cout << "Item " << name << " not found." << endl;
}

void displayItems() const {


cout << "Inventory items (Regular Order):" << endl;
for (const auto& item : items) {
cout << "Name: " << item.name << ", Quantity: " << item.quantity << ",
Price: $" << item.price << endl;
}
}

void displayItemsReverse() const {


cout << "Inventory items (Reverse Order):" << endl;
for (int i = items.size() - 1; i >= 0; i--) {
const auto& item = items[i];
cout << "Name: " << item.name << ", Quantity: " << item.quantity << ",
Price: $" << item.price << endl;
}
}
};

int main() {
InventoryManager manager;

// Adding 3 items
manager.addItem("Apple", 10, 0.50);
manager.addItem("Banana", 5, 0.30);
manager.addItem("Orange", 8, 0.40);

// Removing 1 item
manager.removeItem("Banana");

// Displaying items in regular order


manager.displayItems();

// Displaying items in reverse order


manager.displayItemsReverse();

return 0;
}

solution 3:

#include <iostream>
#include <stack>
#include <string>

using namespace std;

bool isBalanced(const string& expression) {


stack<char> s;

for (char ch : expression) {


if (ch == '(') {
s.push(ch);
} else if (ch == ')') {
// If the stack is empty, there's no matching opening parenthesis
if (s.empty()) {
return false;
}
s.pop();
}
}

// If the stack is empty, all parentheses are balanced


return s.empty();
}

int main() {
// Test expressions
string expressions[] = {
"(5 + (3 * 2))",
"((7 - 3) * (5 + 2)",
"(3 * (2 + (7 - 5)) - 1)"
};

for (const string& expr : expressions) {


cout << "Expression: " << expr << " - ";
if (isBalanced(expr)) {
cout << "Balanced" << endl;
} else {
cout << "Not Balanced" << endl;
}
}

return 0;
}

solution 4:

#include <iostream>

using namespace std;

// Function to perform selection sort in descending order


void selectionSort(int sales[], int size) {
for (int i = 0; i < size - 1; i++) {
int maxIndex = i; // Assume the first element is the maximum
for (int j = i + 1; j < size; j++) {
if (sales[j] > sales[maxIndex]) {
maxIndex = j; // Update maxIndex if a larger element is found
}
}
// Swap the found maximum element with the first element
swap(sales[i], sales[maxIndex]);
}
}

// Function to perform binary search


bool binarySearch(int sales[], int size, int target) {
int left = 0;
int right = size - 1;

while (left <= right) {


int mid = left + (right - left) / 2;
if (sales[mid] == target) {
return true; // Target found
}
if (sales[mid] < target) {
right = mid - 1; // Search in the left half
} else {
left = mid + 1; // Search in the right half
}
}
return false; // Target not found
}

int main() {
// Initialize sales data array
int sales[] = {250, 300, 150, 400, 350, 200};
int size = sizeof(sales) / sizeof(sales[0]); // Calculate the size of the array

// Sort sales data in descending order


selectionSort(sales, size);

// Display sorted sales data


cout << "Sorted Sales Data (Descending Order): ";
for (int i = 0; i < size; i++) {
cout << sales[i] << " ";
}
cout << endl;

// Search for a specific sale amount


int target = 300;
if (binarySearch(sales, size, target)) {
cout << "Sale amount " << target << " exists in the sales data." << endl;
} else {
cout << "Sale amount " << target << " does not exist in the sales data." <<
endl;
}

return 0;
}

You might also like