Oop Assignment # 2 Submitted By: Roll #: Date
Oop Assignment # 2 Submitted By: Roll #: Date
Submitted by:
Roll #:
Date:
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:
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]];
}
Question #5:
#include <iostream>
#include <cstring>
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 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
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>
struct Book {
char* title;
char* author;
int year;
float price;
};
int main() {
int n;
float total_price = 0;
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 << "Total price of all books: " << total_price << endl;
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;
calculateTaxWithholding(employee);
Question #8:
#include <iostream>
#include <string>
struct ShipStats {
float avg_length;
float avg_width;
};
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);