0% found this document useful (0 votes)
16 views11 pages

CAT2 Key

Oops
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views11 pages

CAT2 Key

Oops
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 11

1. #include <stdio.

h>
#include <string.h>

#define MAX_DAYS 30

// Structure to store weather data


typedef struct {
char date[15]; // Date in string format (e.g., "2025-03-20")
float temperature; // Temperature in Celsius
float humidity; // Humidity percentage
float windSpeed; // Wind speed in m/s
} Weather;

// 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;
}

// Function to input weather data


void inputWeatherData(Weather data[], int *n) {
printf("Enter the number of days (5-30): ");
scanf("%d", n);
getchar(); // Consume newline

if (*n < 5 || *n > 30) {


printf("Invalid input! Number of days must be between 5 and 30.\n");
return;
}

for (int i = 0; i < *n; i++) {


printf("\nEnter details for day %d:\n", i + 1);
printf("Date (YYYY-MM-DD): ");
fgets(data[i].date, 15, stdin);
data[i].date[strcspn(data[i].date, "\n")] = '\0'; // Remove newline

printf("Temperature (°C): ");


scanf("%f", &data[i].temperature);

printf("Humidity (%%): ");


scanf("%f", &data[i].humidity);

printf("Wind Speed (m/s): ");


scanf("%f", &data[i].windSpeed);
getchar(); // Consume newline
}
printf("Weather data recorded successfully!\n");
}

// Function to find the hottest and coldest days


void findTemperatureStats(Weather data[], int n) {
if (n == 0) {
printf("No data available!\n");
return;
}
int hotIndex = 0, coldIndex = 0;
for (int i = 1; i < n; i++) {
if (data[i].temperature > data[hotIndex].temperature) {
hotIndex = i;
}
if (data[i].temperature < data[coldIndex].temperature) {
coldIndex = i;
}
}

printf("\nHottest Day: %s (%.2f°C)\n", data[hotIndex].date, data[hotIndex].temperature);


printf("Coldest Day: %s (%.2f°C)\n", data[coldIndex].date, data[coldIndex].temperature);
}

// Function to calculate average weather parameters


void calculateAverageWeather(Weather data[], int n) {
if (n == 0) {
printf("No data available!\n");
return;
}

float totalTemp = 0, totalHumidity = 0, totalWindSpeed = 0;


for (int i = 0; i < n; i++) {
totalTemp += data[i].temperature;
totalHumidity += data[i].humidity;
totalWindSpeed += data[i].windSpeed;
}

printf("\nAverage Weather Data:\n");


printf("Temperature: %.2f°C\n", totalTemp / n);
printf("Humidity: %.2f%%\n", totalHumidity / n);
printf("Wind Speed: %.2f m/s\n", totalWindSpeed / n);
}

// Function to display all recorded weather data


void displayWeatherReport(Weather data[], int n) {
if (n == 0) {
printf("No data available!\n");
return;
}

printf("\nWeather Report:\n");
printf("--------------------------------------------------\n");
printf("| %-12s | %-10s | %-10s | %-10s |\n", "Date", "Temp (°C)", "Humidity", "Wind
(m/s)");
printf("--------------------------------------------------\n");

for (int i = 0; i < n; i++) {


printf("| %-12s | %-10.2f | %-10.2f | %-10.2f |\n",
data[i].date, data[i].temperature, data[i].humidity, data[i].windSpeed);
}

printf("--------------------------------------------------\n");
}

// Function to search for weather data by date


void searchWeatherByDate(Weather data[], int n) {
if (n == 0) {
printf("No data available!\n");
return;
}

char searchDate[15];
printf("Enter the date (YYYY-MM-DD) to search: ");
fgets(searchDate, 15, stdin);
searchDate[strcspn(searchDate, "\n")] = '\0'; // Remove newline

for (int i = 0; i < n; i++) {


if (strcmp(data[i].date, searchDate) == 0) {
printf("\nWeather on %s:\n", data[i].date);
printf("Temperature: %.2f°C\n", data[i].temperature);
printf("Humidity: %.2f%%\n", data[i].humidity);
printf("Wind Speed: %.2f m/s\n", data[i].windSpeed);
return;
}
}
printf("No weather data found for %s.\n", searchDate);
}

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;
}

// Inline function to add a paying vehicle (₹50 charge)


inline void addPayingVehicle() {
totalVehicles++;
totalRevenue += 50;
totalVehicleCount++;
}

// Inline function to add a non-paying vehicle


inline void addNonPayingVehicle() {
totalVehicles++;
totalVehicleCount++;
}

// Function to display details of an individual parking lot


void displayDetails() const {
cout << "Parking Lot ID: " << lotID
<< "\nTotal Vehicles: " << totalVehicles
<< "\nTotal Revenue: ₹" << totalRevenue << "\n------------------\n";
}

// Static function to display the total vehicle count across all lots
static void displayTotalVehicleCount() {
cout << "Total Vehicles Across All Parking Lots: " << totalVehicleCount << endl;
}

// Destructor to adjust total vehicle count when an object is deleted


~ParkingLot() {
totalVehicleCount -= totalVehicles;
}
};

// Initialize the static variable


int ParkingLot::totalVehicleCount = 0;

int main() {
vector<ParkingLot> parkingLots;

// Creating multiple parking lots


parkingLots.push_back(ParkingLot(101));
parkingLots.push_back(ParkingLot(102));
parkingLots.push_back(ParkingLot(103));

// Simulating vehicle entries


parkingLots[0].addPayingVehicle();
parkingLots[0].addNonPayingVehicle();
parkingLots[1].addPayingVehicle();
parkingLots[2].addNonPayingVehicle();
parkingLots[2].addPayingVehicle();
parkingLots[2].addPayingVehicle();

// Display details of individual parking lots


for (const auto &lot : parkingLots) {
lot.displayDetails();
}

// Display total vehicle count across all lots


ParkingLot::displayTotalVehicleCount();

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;
}

// Inline function to add a paying vehicle (₹50 charge)


inline void addPayingVehicle() {
totalVehicles++;
totalRevenue += 50;
totalVehicleCount++;
}

// Inline function to add a non-paying vehicle


inline void addNonPayingVehicle() {
totalVehicles++;
totalVehicleCount++;
}

// Function to display details of an individual parking lot


void displayDetails() const {
cout << "Parking Lot ID: " << lotID
<< "\nTotal Vehicles: " << totalVehicles
<< "\nTotal Revenue: ₹" << totalRevenue << "\n------------------\n";
}
// Static function to display the total vehicle count across all lots
static void displayTotalVehicleCount() {
cout << "Total Vehicles Across All Parking Lots: " << totalVehicleCount << endl;
}

// Destructor to adjust total vehicle count when an object is deleted


~ParkingLot() {
totalVehicleCount -= totalVehicles;
}
};

// Initialize the static variable


int ParkingLot::totalVehicleCount = 0;

int main() {
const int numLots = 3; // Fixed number of parking lots
ParkingLot parkingLots[numLots] = {ParkingLot(101), ParkingLot(102),
ParkingLot(103)};

// Simulating vehicle entries


parkingLots[0].addPayingVehicle();
parkingLots[0].addNonPayingVehicle();
parkingLots[1].addPayingVehicle();
parkingLots[2].addNonPayingVehicle();
parkingLots[2].addPayingVehicle();
parkingLots[2].addPayingVehicle();

// Display details of individual parking lots


for (int i = 0; i < numLots; i++) {
parkingLots[i].displayDetails();
}

// Display total vehicle count across all lots


ParkingLot::displayTotalVehicleCount();

return 0;
}

3. Key
#include <iostream>
#include <string>

using namespace std;

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);
}

// Function to display book details


void displayBook() const {
cout << "Book ID: " << bookID << endl;
cout << "Title: " << title << endl;
cout << "Author: " << author << endl;
cout << "Genre: " << genre << endl;
cout << "Availability: " << (isAvailable ? "Available" : "Not Available") << endl;
cout << "Total Copies: " << noOfCopies << endl;
cout << "-----------------------------" << endl;
}

// Friend function to borrow a book


friend void borrowBook(Book books[], int n, int bookID);

// Friend function to display total books available


friend void totalBooks(Book books[], int n);
};

// Function to borrow a book


void borrowBook(Book books[], int n, int bookID) {
for (int i = 0; i < n; i++) {
if (books[i].bookID == bookID) {
if (books[i].isAvailable && books[i].noOfCopies > 0) {
books[i].noOfCopies--;
if (books[i].noOfCopies == 0) {
books[i].isAvailable = false;
}
cout << "Book \"" << books[i].title << "\" has been borrowed successfully!\n";
} else {
cout << "Sorry, this book is not available right now.\n";
}
return;
}
}
cout << "Book ID not found!\n";
}
// Function to display the total number of available books
void totalBooks(Book books[], int n) {
int availableCount = 0, borrowedCount = 0;

for (int i = 0; i < n; i++) {


availableCount += books[i].noOfCopies;
borrowedCount += (books[i].isAvailable ? 0 : 1);
}

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)
};

cout << "Library Book Details:\n";


for (int i = 0; i < n; i++) {
books[i].displayBook();
}

// Borrowing a book
int bookID;
cout << "Enter the Book ID to borrow: ";
cin >> bookID;
borrowBook(books, n, bookID);

// Display updated book details


cout << "\nUpdated Library Book Details:\n";
for (int i = 0; i < n; i++) {
books[i].displayBook();
}

// Display total books available


totalBooks(books, n);

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; }
};

// Base class to store rate details


class Rate {
protected:
float ratePerHour;
float powerConsumption; // in watts
public:
Rate(float rate = 0, float power = 0) : ratePerHour(rate), powerConsumption(power) {}
void setRate(float rate, float power) {
ratePerHour = rate;
powerConsumption = power;
}
float getRate() const { return ratePerHour; }
};

// Base class to store device type details


class DeviceType {
protected:
string type;
bool voiceControlEnabled;
public:
DeviceType(string t = "Unknown", bool voice = false) : type(t),
voiceControlEnabled(voice) {}
void setType(string t, bool voice) {
type = t;
voiceControlEnabled = voice;
}
string getType() const { return type; }
};

// Derived class using multiple inheritance


class SmartHomeSystem : public DeviceUsage, public Rate, public DeviceType {
public:
SmartHomeSystem(int id, float hours, float rate, float power, string t, bool voice) {
setUsage(id, hours);
setRate(rate, power);
setType(t, voice);
}

float calculateCost() const {


return usageHours * ratePerHour;
}

void display() const {


cout << "Device ID: " << deviceID
<< "\nType: " << type
<< "\nUsage Hours: " << usageHours
<< "\nRate Per Hour: " << ratePerHour
<< "\nVoice Control: " << (voiceControlEnabled ? "Yes" : "No")
<< "\nTotal Cost: $" << fixed << setprecision(2) << calculateCost()
<< "\n----------------------\n";
}
};

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 << "Smart Home Device Report\n";


cout << "========================\n";

for (const auto &device : devices) {


device.display();
totalCost += device.calculateCost();
}

cout << "Total Electricity Cost: $" << fixed << setprecision(2) << totalCost << endl;

return 0;
}

You might also like