Professional Documents
Culture Documents
B Assignment10 0594
B Assignment10 0594
Assignment no 10
struct Book {
string title;
string author;
int publicationYear;
double price;
};
int main() {
const int MAX_BOOKS = 50;
Book books[MAX_BOOKS];
int numBooks;
cout << "Enter book details (title, author, publication year, price):\n";
for (int i = 0; i < numBooks; ++i) {
cout << "Book " << i + 1 << ":\n";
cout << "Title: ";
cin.ignore();
getline(cin, books[i].title);
cout << "Author: ";
getline(cin, books[i].author);
cout << "Publication Year: ";
cin >> books[i].publicationYear;
cout << "Price: $";
cin >> books[i].price;
}
sortBooksByPriceDescending(books, numBooks);
displayBooks(books, numBooks);
return 0;
}
Output:
Task 2:
#include <iostream>
#include <string>
struct Employee {
string name;
int age;
string address;
float salary;
};
void calculateTax(Employee& emp) {
float taxRate;
int main() {
Employee emp;
calculateTax(emp);
displayEmployeeInfo(emp);
return 0;
}
Output:
Task 3:
#include <iostream>
#include <string>
#include <algorithm>
int main() {
const int MAX_SECTIONS = 10;
Section sections[MAX_SECTIONS];
int numSections = 0;
int choice;
do {
cout << "Menu:\n";
cout << "1. Add Section\n";
cout << "2. Add Student to Section\n";
cout << "3. Display Students of a Section\n";
cout << "4. Update Student CGPA\n";
cout << "5. Sort Students in a Section by CGPA\n";
cout << "6. Exit\n";
cout << "Enter your choice: ";
cin >> choice;
switch (choice) {
case 1: {
if (numSections < MAX_SECTIONS) {
cout << "Enter section name: ";
cin >> sections[numSections].sectionName;
cout << "Enter class teacher's name: ";
cin >> sections[numSections].classTeacherName;
sections[numSections].numStudents = 0;
numSections++;
}
else {
cout << "Maximum sections reached.\n";
}
break;
}
case 2: {
int sectionIndex;
cout << "Enter section index (0 to " << numSections - 1 << "): ";
cin >> sectionIndex;
if (sectionIndex >= 0 && sectionIndex < numSections) {
if (sections[sectionIndex].numStudents < 40) {
Student newStudent;
cout << "Enter student name: ";
cin >> newStudent.name;
cout << "Enter student CNIC: ";
cin >> newStudent.CNIC;
cout << "Enter student gender: ";
cin >> newStudent.gender;
cout << "Enter student CGPA: ";
cin >> newStudent.CGPA;
addStudentToSection(sections[sectionIndex], newStudent);
}
else {
cout << "Section is full. Cannot add more students.\n";
}
}
else {
cout << "Invalid section index.\n";
}
break;
}
case 3: {
int sectionIndex;
cout << "Enter section index (0 to " << numSections - 1 << "): ";
cin >> sectionIndex;
if (sectionIndex >= 0 && sectionIndex < numSections) {
displaySectionStudents(sections[sectionIndex]);
}
else {
cout << "Invalid section index.\n";
}
break;
}
case 4: {
string studentName, studentCNIC;
float newCGPA;
cout << "Enter student name: ";
cin >> studentName;
cout << "Enter student CNIC: ";
cin >> studentCNIC;
cout << "Enter new CGPA: ";
cin >> newCGPA;
updateStudentCGPA(sections, numSections, studentName, studentCNIC, newCGPA);
break;
}
case 5: {
int sectionIndex;
cout << "Enter section index (0 to " << numSections - 1 << "): ";
cin >> sectionIndex;
if (sectionIndex >= 0 && sectionIndex < numSections) {
sortStudentsByCGPA(sections[sectionIndex]);
cout << "Students in Section " << sections[sectionIndex].sectionName << " sorted by CGPA.\n";
}
else {
cout << "Invalid section index.\n";
}
break;
}
case 6: {
cout << "Exiting program.\n";
break;
}
default:
cout << "Invalid choice. Please enter a valid option.\n";
}
} while (choice != 6);
return 0;
}
Output:
Task 4:
#include <iostream>
#include <string>
int main() {
// Define items
Item drinks = { "Drinks", 60 };
Item burgers = { "Burgers", 200 };
Item sweet = { "Sweet", 120 };
// Display menu
cout << "Menu:\n";
cout << "1. " << drinks.name << " - " << drinks.price << " PKR\n";
cout << "2. " << burgers.name << " - " << burgers.price << " PKR\n";
cout << "3. " << sweet.name << " - " << sweet.price << " PKR\n";
// Display receipt
cout << "Your Order = " << numBurgers << " " << burgers.name << ", " << numDrinks << " " << drinks.name << ", "
<< numSweet << " " << sweet.name << ".\n";
cout << "Total Bill: " << totalBill << " PKR\n";
return 0;
}
Output:
Task 5:
#include <iostream>
#include <fstream>
#include <string>
bool saveDataToFile(string name, int rollNum, int CNIC, string degree, string FileName) {
ofstream outFile(FileName, ios::app);
if (outFile.is_open()) {
outFile << "Name: " << name << endl;
outFile << "Roll Number: " << rollNum << endl;
outFile << "CNIC: " << CNIC << endl;
outFile << "Degree: " << degree << endl;
outFile << "-------------------\n";
outFile.close();
return true;
}
else {
cout << "Error: Unable to open file." << endl;
return false;
}
}
int main() {
string batchName;
cout << "Enter batch name (e.g., batch2023): ";
cin >> batchName;
cin.ignore();
char choice;
do {
string name, degree;
int rollNum, CNIC;
cout << "Do you want to enter data for another student? (y/n): ";
cin >> choice;
cin.ignore();
} while (choice == 'y' || choice == 'Y');
system("CLS");
if (readAndPrintData(fileName)) {
cout << "Data printed successfully." << endl;
}
else {
cout << "Failed to print data." << endl;
}
return 0;
}
Output:
Task 6:
#include <iostream>
#include <fstream>
#include <string>
int main() {
ifstream file("names.txt");
if (!file.is_open()) {
cout << "Error: Unable to open file." << endl;
return 1;
}
string substring;
cout << "Enter the substring to search for: ";
cin >> substring;
cout << "Names containing \"" << substring << "\":\n";
string name;
while (getline(file, name)) {
if (name.find(substring) != string::npos) {
cout << name << endl;
}
}
file.close();
return 0;
}
Output:
Task 7:
#include <iostream>
struct Saxon {
double length;
double width;
double height;
double radius;
double depth;
};
int main() {
Saxon saxon;
return 0;
}
Output:
Task 8:
#include <iostream>
int main() {
int n;
cout << "Enter the size of the array: ";
cin >> n;
cout << "Enter " << n << " values for ptr1: ";
for (int i = 0; i < n; ++i) {
cin >> *(ptr1 + i);
}
if (palindrome) {
cout << "Ptr2 is a palindrome." << endl;
}
else {
cout << "Ptr2 is not a palindrome." << endl;
}
delete[] ptr1;
delete[] ptr2;
return 0;
}
Output:
Task 9:
#include <iostream>
#include <string>
struct Address {
int houseNumber;
int streetNumber;
string cityName;
string provinceName;
};
struct Student {
string name;
Address address;
int age;
float GPA;
};
int main() {
const int MAX_STUDENTS = 100;
Student students[MAX_STUDENTS];
int numStudents = 0;
char choice;
do {
Student newStudent;
students[numStudents++] = newStudent;
cout << "Do you want to enter data for another student? (y/n): ";
cin >> choice;
cin.ignore();
} while (choice == 'y' || choice == 'Y');
return 0;
}
Output:
Task 10:
#include <iostream>
#include <string>
struct Ship {
string name;
string type;
float length;
float width;
float topSpeed;
};
struct ShipStats {
float averageLength;
float averageWidth;
};
return stats;
}
int main() {
const int MAX_SHIPS = 10;
Ship ships[MAX_SHIPS];
int numShips;
cout << "Enter type of ship " << i + 1 << ": ";
getline(cin, ships[i].type);
cout << "Enter length of ship " << i + 1 << ": ";
cin >> ships[i].length;
cout << "Enter width of ship " << i + 1 << ": ";
cin >> ships[i].width;
cout << "Enter top speed of ship " << i + 1 << ": ";
cin >> ships[i].topSpeed;
cin.ignore();
}
cout << "Average Length of Ships: " << averageStats.averageLength << endl;
cout << "Average Width of Ships: " << averageStats.averageWidth << endl;
return 0;
}
Output:
Task 11:
#include <iostream>
#include <fstream>
#include <string>
struct Product {
string name;
float price;
int quantity;
};
void readProducts() {
ifstream inFile("inventory.txt");
if (inFile.is_open()) {
string line;
while (getline(inFile, line)) {
cout << line << endl;
}
inFile.close();
}
else {
cout << "Error: Unable to open file." << endl;
}
}
int main() {
int choice;
do {
cout << "\n1. Add new product\n2. Read products\n3. Update inventory\n4. Exit\n";
cout << "Enter your choice: ";
cin >> choice;
cin.ignore();
switch (choice) {
case 1: {
Product newProduct;
cout << "Enter product name: ";
getline(cin, newProduct.name);
cout << "Enter product price: ";
cin >> newProduct.price;
cout << "Enter product quantity: ";
cin >> newProduct.quantity;
addProduct(newProduct);
break;
}
case 2: {
cout << "Product details:\n";
readProducts();
break;
}
case 3: {
string productName;
int newQuantity;
cout << "Enter product name to update: ";
cin.ignore();
getline(cin, productName);
cout << "Enter new quantity: ";
cin >> newQuantity;
updateInventory(productName, newQuantity);
break;
}
case 4:
cout << "Exiting program.\n";
break;
default:
cout << "Invalid choice. Please try again.\n";
}
} while (choice != 4);
return 0;
}
Output
Task 12:
#include <iostream>
#include <algorithm>
using namespace std;
int main() {
int size;
cout << "Enter the size of the array (even number): ";
cin >> size;
if (size % 2 != 0) {
cout << "Please enter an even number." << endl;
return 1;
}
cout << "Enter " << size << " elements of the array:" << endl;
for (int i = 0; i < size; i++) {
cin >> arr[i];
}
sortAscending(arr, size);
sortDescending(arr, size);
cout << "\nSorted array:" << endl;
for (int i = 0; i < size; i++) {
cout << arr[i] << " ";
}
cout << endl;
updateArray(arr, size);
delete[] arr;
return 0;
}
Task 13:
#include <iostream>
#include <cstring>
int main() {
// Original line
char originalLine[100];
cout << "Enter the original line: ";
cin.getline(originalLine, sizeof(originalLine));
// Encoded line
char encodedLine[100];
for (int i = 0; i < strlen(originalLine); i++) {
if (originalLine[i] >= 'A' && originalLine[i] <= 'Z') {
encodedLine[i] = ((originalLine[i] - 'A' + code1) % 26) + 'A';
}
else if (originalLine[i] >= 'a' && originalLine[i] <= 'z') {
encodedLine[i] = ((originalLine[i] - 'a' + code2) % 26) + 'a';
}
else {
encodedLine[i] = originalLine[i];
}
}
encodedLine[strlen(originalLine)] = '\0'; // Null-terminate the string
// Decoded line
char decodedLine[100];
for (int i = 0; i < strlen(encodedLine); i++) {
if (encodedLine[i] >= 'A' && encodedLine[i] <= 'Z') {
decodedLine[i] = ((encodedLine[i] - 'A' - code1 + 26) % 26) + 'A';
}
else if (encodedLine[i] >= 'a' && encodedLine[i] <= 'z') {
decodedLine[i] = ((encodedLine[i] - 'a' - code2 + 26) % 26) + 'a';
}
else {
decodedLine[i] = encodedLine[i];
}
}
decodedLine[strlen(encodedLine)] = '\0'; // Null-terminate the string
// Output
cout << "Original Line: " << originalLine << endl;
cout << "Encoded Line: " << encodedLine << endl;
cout << "Decoded Line: " << decodedLine << endl;
return 0;
}
Output:
Task 14:
#include <iostream>
#include <string>
do {
cout << "\nTask Manager Menu:\n";
cout << "1. Add Task\n";
cout << "2. Mark Task as Completed\n";
cout << "3. Delete Task\n";
cout << "4. List Tasks\n";
cout << "5. Exit\n";
cout << "Enter your choice: ";
cin >> choice;
switch (choice) {
case '1':
cin.ignore(); // Ignore newline character left in the input buffer
addTask();
break;
case '2':
completeTask();
break;
case '3':
deleteTask();
break;
case '4':
listTasks();
break;
case '5':
cout << "Exiting Task Manager. Goodbye!\n";
break;
default:
cout << "Invalid choice. Please try again.\n";
break;
}
} while (choice != '5');
return 0;
}
Output:
Task 15:
#include <iostream>
#include <fstream>
#include <string>
if (!fin || !temp) {
cout << "Error opening file." << endl;
return;
}
string line;
while (getline(fin, line)) {
size_t pos = line.find(targetWord);
while (pos != string::npos) {
line.erase(pos, targetWord.length());
pos = line.find(targetWord, pos);
}
temp << line << endl;
}
fin.close();
temp.close();
remove(filename.c_str());
rename("temp.txt", filename.c_str());
cout << "Word '" << targetWord << "' deleted from file." << endl;
}
int main() {
const string filename = "poem.txt";
return 0;
}
Output: