CAT2 Key
CAT2 Key
h>
#include <string.h>
#define MAX_DAYS 30
// Function prototypes
void inputWeatherData(Weather data[], int *n);
void findTemperatureStats(Weather data[], int n);
void calculateAverageWeather(Weather data[], int n);
void displayWeatherReport(Weather data[], int n);
void searchWeatherByDate(Weather data[], int n);
int main() {
Weather data[MAX_DAYS];
int n, choice;
do {
printf("\n--- Weather Data Management ---\n");
printf("1. Input Weather Data\n");
printf("2. Find Hottest and Coldest Days\n");
printf("3. Calculate Average Weather Data\n");
printf("4. Display Weather Report\n");
printf("5. Search Weather by Date\n");
printf("6. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
getchar(); // Consume newline left in buffer
switch (choice) {
case 1:
inputWeatherData(data, &n);
break;
case 2:
findTemperatureStats(data, n);
break;
case 3:
calculateAverageWeather(data, n);
break;
case 4:
displayWeatherReport(data, n);
break;
case 5:
searchWeatherByDate(data, n);
break;
case 6:
printf("Exiting program...\n");
break;
default:
printf("Invalid choice! Try again.\n");
}
} while (choice != 6);
return 0;
}
printf("\nWeather Report:\n");
printf("--------------------------------------------------\n");
printf("| %-12s | %-10s | %-10s | %-10s |\n", "Date", "Temp (°C)", "Humidity", "Wind
(m/s)");
printf("--------------------------------------------------\n");
printf("--------------------------------------------------\n");
}
char searchDate[15];
printf("Enter the date (YYYY-MM-DD) to search: ");
fgets(searchDate, 15, stdin);
searchDate[strcspn(searchDate, "\n")] = '\0'; // Remove newline
2. With Vector
#include <iostream>
#include <vector>
using namespace std;
class ParkingLot {
private:
int lotID;
int totalVehicles;
double totalRevenue;
static int totalVehicleCount; // Static variable to track vehicles across all parking lots
public:
// Constructor to initialize values using "this" pointer
ParkingLot(int id) {
this->lotID = id;
this->totalVehicles = 0;
this->totalRevenue = 0.0;
}
// Static function to display the total vehicle count across all lots
static void displayTotalVehicleCount() {
cout << "Total Vehicles Across All Parking Lots: " << totalVehicleCount << endl;
}
int main() {
vector<ParkingLot> parkingLots;
return 0;
}
Without Vector
#include <iostream>
using namespace std;
class ParkingLot {
private:
int lotID;
int totalVehicles;
double totalRevenue;
static int totalVehicleCount; // Static variable to track vehicles across all parking lots
public:
// Constructor to initialize values using "this" pointer
ParkingLot(int id) {
this->lotID = id;
this->totalVehicles = 0;
this->totalRevenue = 0.0;
}
int main() {
const int numLots = 3; // Fixed number of parking lots
ParkingLot parkingLots[numLots] = {ParkingLot(101), ParkingLot(102),
ParkingLot(103)};
return 0;
}
3. Key
#include <iostream>
#include <string>
class Book {
private:
int bookID;
string title;
string author;
string genre;
bool isAvailable;
int noOfCopies;
public:
// Constructor to initialize book details
Book(int id, string t, string a, string g, int copies) {
bookID = id;
title = t;
author = a;
genre = g;
noOfCopies = copies;
isAvailable = (copies > 0);
}
cout << "\nTotal books available in library: " << availableCount << endl;
cout << "Total books borrowed: " << borrowedCount << endl;
}
int main() {
// Creating an array of books
int n = 3; // Number of books
Book books[] = {
Book(101, "Harry Potter", "J.K. Rowling", "Fantasy", 5),
Book(102, "The Hobbit", "J.R.R. Tolkien", "Adventure", 3),
Book(103, "A Brief History of Time", "Stephen Hawking", "Science", 2)
};
// Borrowing a book
int bookID;
cout << "Enter the Book ID to borrow: ";
cin >> bookID;
borrowBook(books, n, bookID);
return 0;
}
4. #include <iostream>
#include <iomanip>
#include <vector>
using namespace std;
// Base class to store device usage details
class DeviceUsage {
protected:
int deviceID;
float usageHours;
public:
DeviceUsage(int id = 0, float hours = 0) : deviceID(id), usageHours(hours) {}
void setUsage(int id, float hours) {
deviceID = id;
usageHours = hours;
}
float getUsage() const { return usageHours; }
};
int main() {
vector<SmartHomeSystem> devices;
// Sample devices
devices.push_back(SmartHomeSystem(1, 5, 0.10, 60, "Lamp", false));
devices.push_back(SmartHomeSystem(2, 3, 0.20, 1500, "Air Conditioner", true));
devices.push_back(SmartHomeSystem(3, 7, 0.15, 100, "Fan", false));
float totalCost = 0;
cout << "Total Electricity Cost: $" << fixed << setprecision(2) << totalCost << endl;
return 0;
}