Code Pairing - Solutions For Asked Problems
Code Pairing - Solutions For Asked Problems
Solution 1:
#include <iostream>
#include <vector>
#include <string>
class Book {
public:
string title, author, isbn;
bool available;
class Member {
public:
string name;
int memberID;
vector<string> borrowedBooks;
class Library {
private:
vector<Book> books;
vector<Member> members;
public:
void addBook(string title, string author, string isbn) {
books.push_back(Book(title, author, isbn));
}
void viewAvailableBooks() {
cout << "Available Books: " << books.size() << "\n";
for (const auto& book : books) {
if (book.available) {
cout << "Title: " << book.title << ", Author: " << book.author << ", ISBN: "
<< book.isbn << "\n";
}
}
}
int main() {
Library lib;
lib.addBook("The Great Gatsby", "F. Scott Fitzgerald", "1234567890");
lib.addBook("1984", "George Orwell", "0987654321");
lib.addMember("Alice", 1);
lib.addMember("Bob", 2);
lib.borrowBook(1, "1234567890");
lib.viewAvailableBooks();
lib.returnBook(1, "1234567890");
lib.viewAvailableBooks();
lib.searchBook("1984");
return 0;
}
Problem statement 2:
Solution :
#include <iostream>
#include <vector>
#include <unordered_map>
#include <ctime>
using namespace std;
class Vehicle {
public:
string licensePlate;
VehicleSize size;
time_t entryTime;
class ParkingSpot {
public:
int level, spotNumber;
VehicleSize size;
bool isOccupied;
class ParkingLot {
private:
int levels;
int spotsPerLevel;
vector<vector<ParkingSpot>> parkingSpots;
unordered_map<string, pair<int, int>> parkedCars; // Maps license plate to (level,
spotNumber)
unordered_map<string, time_t> parkingTime; // Maps license plate to entry time
double hourlyRate = 50.0;
public:
ParkingLot(int lvls, int spots) : levels(lvls), spotsPerLevel(spots) {
for (int i = 0; i < levels; i++) {
vector<ParkingSpot> levelSpots;
for (int j = 0; j < spotsPerLevel; j++) {
VehicleSize size = (j < spots / 3) ? SMALL : (j < 2 * spots / 3) ? MEDIUM :
LARGE;
levelSpots.emplace_back(i, j, size);
}
parkingSpots.push_back(levelSpots);
}
}
bool isFull() {
return parkedCars.size() >= levels * spotsPerLevel;
}
int main() {
ParkingLot lot(3, 9);
lot.parkVehicle(car1);
lot.parkVehicle(car2);
lot.parkVehicle(car3);
if (lot.isFull()) {
cout << "Parking lot is full!\n";
} else {
cout << "Parking lot has space available.\n";
}
lot.reserveSpot(1, 2);
return 0;
}
Problem statement 3:
Solution:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define MAX_SHOWS 5
#define MAX_SEATS 10
typedef struct {
char eventName[50];
char date[20];
int availableSeats;
int seats[MAX_SEATS]; // 0 for available, 1 for booked
} Show;
Show shows[MAX_SHOWS];
int showCount = 0;
void viewShows() {
printf("Available Shows:\n");
for (int i = 0; i < showCount; i++) {
printf("%d. %s on %s (%d seats available)\n", i + 1, shows[i].eventName,
shows[i].date, shows[i].availableSeats);
}
}