LAB TASK SOLUTION NOUROOZ

Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 8

#include <iostream>

using namespace std;

class ShoppingMall {
public:

double calculateBill(double weightInKg) {


const double pricePerKg = 300.0;
return weightInKg * pricePerKg;
}

double calculateBill(int quantity) {


const double pricePerPiece = 100.0;
return quantity * pricePerPiece;
}

// Version 3: For items with a discount


double calculateBill(double totalPrice, double discountPercentage) {
return totalPrice - (totalPrice * discountPercentage / 100.0);
}
};

int main() {
ShoppingMall mall;

// Version 1: Input for items bought in kilograms


double weightInKg;
cout << "Enter weight of items bought in kilograms: ";
cin >> weightInKg;
double billForKg = mall.calculateBill(weightInKg);
cout << "Total bill for items bought in kilograms: " << billForKg << endl;

// Version 2: Input for items bought in pieces


int quantity;
cout << "Enter quantity of items bought in pieces: ";
cin >> quantity;
double billForPieces = mall.calculateBill(quantity);
cout << "Total bill for items bought in pieces: " << billForPieces << endl;

// Version 3: Input for items with a discount


double totalPrice, discountPercentage = 5.0;
cout << "Enter total price of items for discount calculation: ";
cin >> totalPrice;
double discountedBill = mall.calculateBill(totalPrice, discountPercentage);
cout << "Total bill after discount: " << discountedBill << endl;

return 0;
}

#include <iostream>
using namespace std;

inline int calculateFine(int speed, int speedLimit) {


const int finePerKm = 10;
int excessSpeed = speed - speedLimit;
return (excessSpeed > 0) ? excessSpeed * finePerKm : 0;
}
inline bool isSafeSpeed(int speed, int speedLimit) {
return speed <= speedLimit;
}

int main() {
int numVehicles;
cout << "Enter the number of vehicles to process: ";
cin >> numVehicles;

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


int speed, speedLimit;
cout << "\nEnter the vehicle speed: ";
cin >> speed;
cout << "Enter the speed limit: ";
cin >> speedLimit;

if (isSafeSpeed(speed, speedLimit)) {
cout << "Vehicle speed is within the safe range." << endl;
} else {
int fine = calculateFine(speed, speedLimit);
cout << "Overspeeding! Fine: " << fine << " units." << endl;
}
}

return 0;
}

#include <iostream>
using namespace std;

#define TAX_MACRO(income, rate) (income * rate)

inline double taxInline(double income, double rate) {


return income * rate;
}

int main() {
double income, rate;

cout << "Enter income: ";


cin >> income;
cout << "Enter tax rate (as a decimal): ";
cin >> rate;

// Calculate tax using the macro


double macroTax = TAX_MACRO(income, rate);
double macroTaxWithExpression = TAX_MACRO((income - 1000), rate);

// Calculate tax using the inline function


double inlineTax = taxInline(income, rate);
double inlineTaxWithExpression = taxInline(income - 1000, rate);

// Display results
cout << "\nTax Calculation using Macro:" << endl;
cout << "Direct income * rate: " << macroTax << endl;
cout << "Expression (income - 1000) * rate: " << macroTaxWithExpression <<
endl;
cout << "\nTax Calculation using Inline Function:" << endl;
cout << "Direct income * rate: " << inlineTax << endl;
cout << "Expression (income - 1000) * rate: " << inlineTaxWithExpression <<
endl;

// Highlighting the difference


cout << "\nObservation:" << endl;
cout << "Macro fails to properly handle expressions due to lack of parenthesis
around the parameter usage." << endl;
cout << "Inline function handles expressions correctly as it evaluates them
before calculation." << endl;

return 0;
}
#include <iostream>
using namespace std;

float calculateHostelFee(float baseFee = 5000.0, float utility = 500.0, float


internet = 200.0, float laundry = 100.0) {
return baseFee + utility + internet + laundry;
}

int main() {
cout << "Total Hostel Fee with default charges: $" << calculateHostelFee() <<
endl;

float customUtility, customInternet, customLaundry;


cout << "\nEnter custom charges:" << endl;
cout << "Utility: $";
cin >> customUtility;
cout << "Internet: $";
cin >> customInternet;
cout << "Laundry: $";
cin >> customLaundry;

// Displaying total fee with custom charges


cout << "\nTotal Hostel Fee with custom charges: $"
<< calculateHostelFee(5000.0, customUtility, customInternet,
customLaundry) << endl;

return 0;
}
#include <iostream>
using namespace std;

// Function to update inventory using a static local variable


void updateInventory(int dailySales) {
static int totalProductsSold = 0; // Static local variable to keep track of
cumulative sales
totalProductsSold += dailySales;
cout << "Products sold today: " << dailySales << endl;
cout << "Total products sold so far: " << totalProductsSold << endl;
}

int main() {
cout << "Product Inventory Tracker" << endl;

// Simulating daily sales over a week


for (int day = 1; day <= 7; ++day) {
int dailySales;
cout << "\nEnter the number of products sold on day " << day << ": ";
cin >> dailySales;
updateInventory(dailySales);
}

return 0;
}
.#include <iostream>
using namespace std;

int main() {
int n;

// Input the number of students


cout << "Enter the number of students: ";
cin >> n;

// Dynamically allocate memory for student marks


int* marks = new int[n];

// Input marks for n students


cout << "Enter the marks of " << n << " students:" << endl;
for (int i = 0; i < n; ++i) {
cout << "Student " << i + 1 << ": ";
cin >> *(marks + i); // Using pointer arithmetic
}

// Initialize variables for calculations


int highest = *marks;
int lowest = *marks;
int total = 0;

// Calculate highest, lowest, and total marks using pointer arithmetic


for (int i = 0; i < n; ++i) {
if (*(marks + i) > highest) {
highest = *(marks + i);
}
if (*(marks + i) < lowest) {
lowest = *(marks + i);
}
total += *(marks + i);
}

// Calculate the average


double average = static_cast<double>(total) / n;

// Print results
cout << "\nResults:" << endl;
cout << "Highest marks: " << highest << endl;
cout << "Lowest marks: " << lowest << endl;
cout << "Average marks: " << average << endl;

// Free dynamically allocated memory


delete[] marks;

return 0;
}
#include <iostream>
using namespace std;

const int SIZE = 3;

int calculateDiagonalSum(int (*matrix)[SIZE]) {


int sum = 0;
for (int i = 0; i < SIZE; ++i) {
sum += *(*(matrix + i) + i);
}
return sum;
}

void transposeMatrix(int (*matrix)[SIZE], int (*transposed)[SIZE]) {


for (int i = 0; i < SIZE; ++i) {
for (int j = 0; j < SIZE; ++j) {
*(*(transposed + j) + i) = *(*(matrix + i) + j);
}
}
}

// Function to display the matrix


void displayMatrix(int (*matrix)[SIZE]) {
for (int i = 0; i < SIZE; ++i) {
for (int j = 0; j < SIZE; ++j) {
cout << *(*(matrix + i) + j) << " ";
}
cout << endl;
}
}

int main() {
int matrix[SIZE][SIZE];
int transposed[SIZE][SIZE];

// Input the matrix


cout << "Enter elements of a 3x3 matrix:" << endl;
for (int i = 0; i < SIZE; ++i) {
for (int j = 0; j < SIZE; ++j) {
cin >> *(*(matrix + i) + j); // Input using pointers
}
}

// Calculate the sum of diagonal elements


int diagonalSum = calculateDiagonalSum(matrix);

// Transpose the matrix


transposeMatrix(matrix, transposed);

// Display the original matrix


cout << "\nOriginal Matrix:" << endl;
displayMatrix(matrix);

// Display the sum of diagonal elements


cout << "\nSum of diagonal elements: " << diagonalSum << endl;

// Display the transposed matrix


cout << "\nTransposed Matrix:" << endl;
displayMatrix(transposed);
return 0;
}
#include <iostream>
#include <string>
using namespace std;

int totalCarsParked = 0;

void parkCar() {
string carNumber;
int hoursParked;
const int feePerHour = 10;

cout << "Enter car number: ";


cin >> carNumber;
cout << "Enter hours parked: ";
cin >> hoursParked;

int parkingFee = hoursParked * feePerHour;


cout << "Car Number: " << carNumber << endl;
cout << "Hours Parked: " << hoursParked << endl;
cout << "Parking Fee: $" << parkingFee << endl;

// Increment global car counter


totalCarsParked++;
}

int main() {
int numberOfCars;

cout << "Enter the number of cars to simulate parking: ";


cin >> numberOfCars;

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


cout << "\nParking Car " << (i + 1) << ":" << endl;
parkCar();
}

// Display cumulative results


cout << "\nTotal cars parked: " << totalCarsParked << endl;

return 0;
}
#include <iostream>
#include <string>
using namespace std;

bool authenticateUser(const string& username, const string& password) {


const string storedUsername = "admin";
const string storedPassword = "12345";

return (username == storedUsername && password == storedPassword);


}

int main() {
string username, password;
int attempts = 0;
const int maxAttempts = 3;
while (attempts < maxAttempts) {
cout << "Enter username: ";
cin >> username;
cout << "Enter password: ";
cin >> password;

if (authenticateUser(username, password)) {
cout << "Access Granted" << endl;
return 0;
} else {
cout << "Invalid credentials. Try again." << endl;
attempts++;
}
}

cout << "Access Denied" << endl;


return 0;
}
#include <iostream>
using namespace std;

const double ELECTRICITY_UNIT_RATE = 5.50;

void calculateBill(double unitsConsumed) {


const double ELECTRICITY_UNIT_RATE = 6.00;

double billWithLocalRate = unitsConsumed * ELECTRICITY_UNIT_RATE;

double billWithGlobalRate = unitsConsumed * ::ELECTRICITY_UNIT_RATE;

cout << "Units Consumed: " << unitsConsumed << " units" << endl;
cout << "Bill with Local Rate ($" << ELECTRICITY_UNIT_RATE << "): $" <<
billWithLocalRate << endl;
cout << "Bill with Global Rate ($" << ::ELECTRICITY_UNIT_RATE << "): $" <<
billWithGlobalRate << endl;
}

int main() {
double unitsConsumed;

cout << "Enter the number of electricity units consumed: ";


cin >> unitsConsumed;

// Call the function to calculate the bill


calculateBill(unitsConsumed);

return 0;
}
#include <iostream>

using namespace std;

int main() {
int n;

cout << "Enter the number of employees: ";


cin >> n;

double* salaries = new double[n];


cout << "Enter the salaries for the employees:" << endl;
for (int i = 0; i < n; ++i) {
cout << "Employee " << (i + 1) << ": ";
cin >> *(salaries + i); // Using pointer arithmetic
}

char update;
do {
int empIndex;
double newSalary;

cout << "Do you want to update a salary? (y/n): ";


cin >> update;

if (update == 'y' || update == 'Y') {


cout << "Enter the employee number to update (1 to " << n << "): ";
cin >> empIndex;

if (empIndex < 1 || empIndex > n) {


cout << "Invalid employee number!" << endl;
continue;
}

cout << "Enter the new salary for Employee " << empIndex << ": ";
cin >> newSalary;

*(salaries + empIndex - 1) = newSalary; // Update salary using pointer


arithmetic
}

} while (update == 'y' || update == 'Y');

// Calculate total payroll


double totalPayroll = 0.0;
for (int i = 0; i < n; ++i) {
totalPayroll += *(salaries + i); // Using pointer arithmetic
}

// Display total payroll


cout << "\nTotal Payroll: " << totalPayroll << endl;

// Free dynamically allocated memory


delete[] salaries;

return 0;
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy