0% found this document useful (0 votes)
67 views14 pages

Oop Assignment # 2 Submitted By: Hashir Khan Roll #: 22f-7465 Date: 3-3-2023

The document contains an assignment submission by Hashir Khan for OOP assignment #2. It includes 7 questions involving C++ programs to: 1) Get string input and calculate length 2) Create and manipulate a 2D matrix 3) Manage student course registration using structures 4) Create and access a 2D array with variable rows 5) Create an employee structure with benefits sub-structure 6) Create a book structure and array to store book details 7) Function prototype (incomplete) to calculate tax withholding

Uploaded by

Hashir Khan
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)
67 views14 pages

Oop Assignment # 2 Submitted By: Hashir Khan Roll #: 22f-7465 Date: 3-3-2023

The document contains an assignment submission by Hashir Khan for OOP assignment #2. It includes 7 questions involving C++ programs to: 1) Get string input and calculate length 2) Create and manipulate a 2D matrix 3) Manage student course registration using structures 4) Create and access a 2D array with variable rows 5) Create an employee structure with benefits sub-structure 6) Create a book structure and array to store book details 7) Function prototype (incomplete) to calculate tax withholding

Uploaded by

Hashir Khan
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/ 14

Oop assignment # 2

Submitted by:
Hashir khan

Roll #:
22f-7465

Date:

3-3-2023
Question #1:
#include <iostream> question1
using namespace std;
int main()
{
char *str;
str = new char[100];
cout << "Enter a string: ";
cin >> str;
int len = strlen(str);
cout << "Length of the string: " << len << endl;
delete[] str;
return 0;
}

Question #2:
#include <iostream>
#include<math.h>
using namespace std;
void Input(int* Matrix, int size);
void MatrixDisplay(int* Matrix, int size);
void SwapIt(int* Matrix, int size);

int main()
{
int r,c;
cout << "Enter the rows and column of the matrix: ";
cin >> r>>c;
if (r!=c) {
cout << "Invalid size. Please enter a perfect square." << endl;
main();
}
int size=r*c;
int* Matrix = new int[size];
Input(Matrix, size);
cout << "Original matrix:" << endl;
MatrixDisplay(Matrix, size);
SwapIt(Matrix, size);
cout << "Matrix with swapped rows:" << endl;
MatrixDisplay(Matrix, size);
delete[] Matrix;
return 0;
}
void Input(int* Matrix, int size)
{
cout << "Enter the elements of the matrix:" << endl;
for (int i = 0; i < size; i++) {
cin >> Matrix[i];
}
}
void MatrixDisplay(int* Matrix, int size)
{
int sqrt_size = sqrt(size);
for (int i = 0; i < size; i++) {
cout << Matrix[i] << " ";
if ((i + 1) % sqrt_size == 0) {
cout << endl;
}
}
}
void SwapIt(int* Matrix, int size)
{
int sqrt_size = sqrt(size);
for (int i = 0; i < sqrt_size / 2; i++) {
for (int j = 0; j < sqrt_size; j++) {
int temp = Matrix[i * sqrt_size + j];
Matrix[i * sqrt_size + j] = Matrix[(sqrt_size - i - 1) * sqrt_size + j];
Matrix[(sqrt_size - i - 1) * sqrt_size + j] = temp;
}
}

Question #3:
#include <iostream>
#include <string>
using namespace std;

const int MAX_COURSES = 5;


const int MAX_STUDENTS = 100;

struct Student {
string name;
string courses[MAX_COURSES];
int numCourses;
};
void registerCourse(Student& student) {
if (student.numCourses == MAX_COURSES) {
cout << "Maximum number of courses reached!" << endl;
return;
}
string course;
cout << "Enter course name: ";
cin >> course;
student.courses[student.numCourses++] = course;
}

void dropCourse(Student& student) {


string course;
cout << "Enter course name to drop: ";
cin >> course;
for (int i = 0; i < student.numCourses; i++) {
if (student.courses[i] == course) {
student.courses[i] = student.courses[--student.numCourses];
cout << "Course dropped successfully!" << endl;
return;
}
}
cout << "Course not found!" << endl;
}

void displayCourseStudents(Student students[], int numStudents, string course) {


cout << "Students registered for " << course << ":" << endl;
for (int i = 0; i < numStudents; i++) {
Student student = students[i];
for (int j = 0; j < student.numCourses; j++) {
if (student.courses[j] == course) {
cout << "- " << student.name << endl;
break;
}
}
}
}

int main() {
Student students[MAX_STUDENTS];
int numStudents = 0;
while (true) {
cout << "1. Add student\n2. Register course\n3. Drop course\n4. Display course students\
n5. Exit\n";
int choice;
cin >> choice;
switch (choice) {
case 1: {
Student student;
cout << "Enter student name: ";
cin >> student.name;
student.numCourses = 0;
students[numStudents++] = student;
cout << "Student added successfully!" << endl;
break;
}
case 2: {
string name;
cout << "Enter student name: ";
cin >> name;
for (int i = 0; i < numStudents; i++) {
if (students[i].name == name) {
registerCourse(students[i]);
break;
}
}
break;
}
case 3: {
string name;
cout << "Enter student name: ";
cin >> name;
for (int i = 0; i < numStudents; i++) {
if (students[i].name == name) {
dropCourse(students[i]);
break;
}
}
break;
}
case 4: {
string course;
cout << "Enter course name: ";
cin >> course;
displayCourseStudents(students, numStudents, course);
break;
}
case 5:
return 0;
default:
cout << "Invalid choice!" << endl;
}
}
}
Question #4:
#include <iostream>
using namespace std;
int main() {
int n;
int* cols = new int[n];
cout << "Enter the number of rows : ";
cin >> n;
double** arr = new double* [n];
for (int i = 0; i < n; i++) {
arr[i] = new double[cols[i]];
}

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


cout << "Enter the number of elements in row " << i + 1 << ": ";
cin >> cols[i];
cout << "Enter values for row " << i + 1 << ": ";
for (int j = 0; j < cols[i]; j++) {
cin >> arr[i][j];
}
}
cout << "2D array elements are:" << endl;
for (int i = 0; i < n; i++) {
for (int j = 0; j < cols[i]; j++) {
cout << arr[i][j] << " ";
}
cout << endl;
}
for (int i = 0; i < n; i++) {
delete[] arr[i];
}
delete[] arr;
delete[] cols;
return 0;

Question #5:
#include <iostream>
#include <cstring>

using namespace std;

const int MAX_EMPLOYEES = 50;


const int MAX_BENEFITS = 5;

struct Benefit {
char name[50];
float amount;
};

struct Employee {
char name[50];
int age;
char address[100];
float salary;
Benefit* benefits;
};

int main() {
int num_employees;
Employee employees[MAX_EMPLOYEES];

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


cin >> num_employees;
cin.ignore();

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


cout << "Enter the name of employee #" << i+1 << ": ";
cin.getline(employees[i].name, 50);

cout << "Enter the age of employee #" << i+1 << ": ";
cin >> employees[i].age;
cin.ignore();

cout << "Enter the address of employee #" << i+1 << ": ";
cin.getline(employees[i].address, 100);

cout << "Enter the salary of employee #" << i+1 << ": ";
cin >> employees[i].salary;

int num_benefits;
cout << "Enter the number of benefits for employee #" << i+1 << ": ";
cin >> num_benefits;
cin.ignore(); // ignore the newline character left in the input stream

employees[i].benefits = new Benefit[num_benefits]; // allocate memory for the dynamic


array of benefits

for (int j = 0; j < num_benefits; j++) {


cout << "Enter the name of benefit #" << j+1 << " for employee #" << i+1 << ": ";
cin.getline(employees[i].benefits[j].name, 50);

cout << "Enter the amount of benefit #" << j+1 << " for employee #" << i+1 << ": ";
cin >> employees[i].benefits[j].amount;
cin.ignore();
}
}
cout << endl << "Employee details:" << endl;
for (int i = 0; i < num_employees; i++) {
cout << "Employee #" << i+1 << ":" << endl;
cout << "Name: " << employees[i].name << endl;
cout << "Age: " << employees[i].age << endl;
cout << "Address: " << employees[i].address << endl;
cout << "Salary: " << employees[i].salary << endl;
float total_compensation = employees[i].salary;
for (int j = 0; j < MAX_BENEFITS; j++) {
if (j >= sizeof(employees[i].benefits)/sizeof(Benefit)) {
break;
}
total_compensation += employees[i].benefits[j].amount;
}
cout << "Total compensation: " << total_compensation << endl << endl;
}}
Question #6:
#include <iostream>
#include <cstring>

using namespace std;

struct Book {
char* title;
char* author;
int year;
float price;
};

int main() {
int n;
float total_price = 0;

cout << "Enter the total number of books: ";


cin >> n;

Book* books = new Book[n]; // allocate memory for the array of books

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


cin.ignore(); // ignore any leftover newline characters
cout << "Enter the title of book #" << i+1 << ": ";
char title[100];
cin.getline(title, 100); // read the title from user
books[i].title = new char[strlen(title)+1]; // allocate memory for the title string
strcpy(books[i].title, title); // copy the title string into the allocated memory

cout << "Enter the author of book #" << i+1 << ": ";
char author[100];
cin.getline(author, 100); // read the author from user
books[i].author = new char[strlen(author)+1]; // allocate memory for the author string
strcpy(books[i].author, author); // copy the author string into the allocated memory

cout << "Enter the year of publication of book #" << i+1 << ": ";
cin >> books[i].year; // read the year from user

cout << "Enter the price of book #" << i+1 << ": ";
cin >> books[i].price; // read the price from user

total_price += books[i].price; // add the price of this book to the total price
}

cout << endl << "Book details:" << endl;


for (int i = 0; i < n; i++) {
cout << "Book #" << i+1 << ":" << endl;
cout << "Title: " << books[i].title << endl;
cout << "Author: " << books[i].author << endl;
cout << "Year of publication: " << books[i].year << endl;
cout << "Price: " << books[i].price << endl << endl;
}

cout << "Total price of all books: " << total_price << endl;

// deallocate memory for the books and their attributes


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

return 0;
}

Question #7:
#include <iostream>
using namespace std;
struct Employee {
string name;
int age;
string address;
float salary;
};
void calculateTaxWithholding(Employee& employee) {
float taxWithholding = 0;
if (employee.salary < 50000) {
taxWithholding = employee.salary * 0.1;
}
else if (employee.salary < 100000) {
taxWithholding = employee.salary * 0.15;
}
else {
taxWithholding = employee.salary * 0.2;
}
employee.salary -= taxWithholding;
cout << "Tax withholding amount: $" << taxWithholding << endl;
}

int main() {
Employee employee;

cout << "Enter employee name: ";


getline(cin, employee.name);
cout << "Enter employee age: ";
cin >> employee.age;
cin.ignore();
cout << "Enter employee address: ";
getline(cin, employee.address);
cout << "Enter employee salary: ";
cin >> employee.salary;

calculateTaxWithholding(employee);

cout << "Employee information:" << endl;


cout << "Name: " << employee.name << endl;
cout << "Age: " << employee.age << endl;
cout << "Address: " << employee.address << endl;
cout << "Salary (after tax withholding): $" << employee.salary << endl;
return 0;
}

Question #8:
#include <iostream>
#include <string>

using namespace std;


struct Ship {
string name;
string type;
float length;
float width;
float top_speed;
};

struct ShipStats {
float avg_length;
float avg_width;
};

ShipStats calculateShipStats(Ship ships[], int num_ships) {


float total_length = 0;
float total_width = 0;
for (int i = 0; i < num_ships; i++) {
total_length += ships[i].length;
total_width += ships[i].width;
}
ShipStats stats;
stats.avg_length = total_length / num_ships;
stats.avg_width = total_width / num_ships;
return stats;
}

int main() {
const int MAX_SHIPS = 10;
Ship ships[MAX_SHIPS];
int num_ships;
cout << "How many ships do you want to enter? (1 TO 10) ";
cin >> num_ships;
while (num_ships < 1 || num_ships > MAX_SHIPS) {
cout << "Invalid number of ships. Please enter a number between 1 and 10: ";
cin >> num_ships;
}
for (int i = 0; i < num_ships; i++) {
cout << "Enter the information for ship " << i + 1 << ":" << endl;
cout << "Name: ";
cin >> ships[i].name;
cout << "Type: ";
cin >> ships[i].type;
cout << "Length: ";
cin >> ships[i].length;
cout << "Width: ";
cin >> ships[i].width;
cout << "Top Speed: ";
cin >> ships[i].top_speed;
}
ShipStats stats = calculateShipStats(ships, num_ships);

cout << endl;


cout << "Ship Information:" << endl;
for (int i = 0; i < num_ships; i++) {
cout << "Name: " << ships[i].name << endl;
cout << "Type: " << ships[i].type << endl;
cout << "Length: " << ships[i].length << endl;
cout << "Width: " << ships[i].width << endl;
cout << "Top Speed: " << ships[i].top_speed << endl;
cout << endl;
}
cout << "Ship Statistics:" << endl;
cout << "Average Length: " << stats.avg_length << endl;
cout << "Average Width: " << stats.avg_width << endl;
return 0;
}

You might also like