App Week3 Assignment Abiram
App Week3 Assignment Abiram
PRACTICE
ASSIGNMENT-3
RA2211026010486
G.Abiram
Z2-cse Aiml
PROGRAMS
Input:
#include <iostream>
// Function prototypes
Double add(double a, double b); Double subtract(double a, double
b); Double multiply(double a, double b); Double divide(double a,
double b);
Int main() {
Double num1, num2; Char op;
Std::cout << “Enter first number: “; Std::cin >> num1;
Std::cout << “Enter an operator (+, -, *, /): “; Std::cin >> op;
Std::cout << “Enter second number: “; Std::cin >> num2;
Double result;
Switch (op) { Case ‘+’:
Result = add(num1, num2);
Break; Case ‘-‘:
Result = subtract(num1, num2);
Break; Case ‘*’:
Result = multiply(num1, num2);
Break; Case ‘/’:
Result = divide(num1, num2);
Break; Default:
Std::cout << “Invalid operator!”;
Return 1; }
Std::cout << “Result: “ << result << std::endl;
Return 0; }
// Subroutine definitions
Double add(double a, double b) {
Return a + b; }
Output:
Input :
Int age;
Std::string major; };
// Database to store students Std::map<int, Student>
studentDatabase;
// Function to add a student to the database
Void addStudent(int id, const std::string& name, int age, const
std::string& major) {
Student newStudent; newStudent.name = name; newStudent.age =
age; newStudent.major = major; studentDatabase[id] = newStudent;
}
// Function to display student information Void displayStudent(int id)
{
If (studentDatabase.find(id) != studentDatabase.end()) { Student
student = studentDatabase[id];
Std::cout << “Name: “ << student.name << std::endl; Std::cout <<
“Age: “ << student.age << std::endl; Std::cout << “Major: “ <<
student.major << std::endl;
} else {
Std::cout << “Student not found.” << std::endl;
}}
Int main() {
Int choice; Do {
Std::cout << “1. Add Student\n2. Display Student\n3. Exit\n”;
Std::cout << “Enter your choice: “;
Std::cin >> choice;
Switch (choice) { Case 1: {
Int id, age;
Std::string name, major;
Std::cout << “Enter Student ID: “; Std::cin >> id;
Std::cout << “Enter Name: “; Std::cin >> name;
Std::cout << “Enter Age: “;
Std::cin >> age;
Std::cout << “Enter Major: “; Std::cin >> major;
addStudent(id, name, age, major); break;
}
Case 2: {
Int id;
Std::cout << “Enter Student ID: “; Std::cin >> id;
displayStudent(id);
break;
}
Case 3:
Output:
1. Add Student
2. Display Student
3. Exit
Enter your choice: 1
Enter Student ID: 1
Enter Name: John
Enter Age: 20
Enter Major: Computer Science
1. Add Student
2. Display Student
3. Exit
Enter your choice: 2
Enter Student ID: 1
Name: John
Age: 20
Major: Computer Science
1. Add Student
2. Display Student
3. Exit
Enter your choice: 3
Exiting
Input:
}
// Function to calculate the perimeter of a rectangle Double
rectanglePerimeter(double length, double width) {
Return 2 * (length + width); }
// Function to calculate the area of a triangle Double
triangleArea(double base, double height) {
Return 0.5 * base * height; }
// Function to calculate the perimeter of a triangle
Double trianglePerimeter(double side1, double side2, double side3)
{
Return side1 + side2 + side3; }
Int main() { Int choice; Do {
Std::cout << “1. Circle\n2. Rectangle\n3. Triangle\n4. Exit\n”;
Std::cout << “Enter your choice: “;
Std::cin >> choice;
Switch (choice) { Case 1: {
Double radius;
Std::cout << “Enter the radius of the circle: “; Std::cin >> radius;
Output:
1. Circle
2. Rectangle
3. Triangle
4. Exit
Enter your choice: 1
Enter the radius of the circle: 5
Area of the circle: 78.5398
Perimeter of the circle: 31.4159
1. Circle
2. Rectangle
3. Triangle
4. Exit
Enter your choice: 2
Enter the length of the rectangle: 4
Enter the width of the rectangle: 6
Area of the rectangle: 24
Perimeter of the rectangle: 20
1. Circle
2. Rectangle
3. Triangle
4. Exit
Enter your choice: 3
Enter the base of the triangle:
Enter the height of the triangle: 4
Area of the triangle: 6
Perimeter of the triangle: 12
1. Circle
2. Rectangle3. Triangle
4. Exit
Enter your choice: 4
Exiting
4.implement a subroutine program to check if a given string is a
palindrome or not.
Input :
Return 0;
}
Output:
Input:
#include <iostream>
// Function to reverse an array of integers in place Void
reverseArray(int arr[], int size) {
For (int I = 0; I < size / 2; ++i) { Int temp = arr[i];
Arr[i] = arr[size – 1 – i]; Arr[size – 1 – i] = temp;
}}
Int main() { Int size;
Std::cout << “Enter the size of the array: “; Std::cin >> size;
Int arr[size];
Std::cout << “Enter the elements of the array:” << std::endl; For (int
I = 0; I < size; ++i) {
Std::cin >> arr[i]; }
Std::cout << “Original array: “;
Output:
Input:
#include <iostream>
Int main() { Int size;
Std::cout << “Enter the size of the array: “; Std::cin >> size;
// Dynamically allocate memory for the array
Output:
Input:
Input:
#include <iostream>
Class Stack { Private:
Int* arr; // Pointer to dynamically allocated array Int top; // Index of
the top element
Int capacity; // Capacity of the stack
Public:
// Constructor to initialize the stack Stack(int size) {
Arr = new int[size]; Capacity = size; Top = -1;
}
// Destructor to free dynamically allocated memory ~Stack() {
Delete[] arr; }
Input:
Public:
Customer(const std::string& name, int age, long long int
phoneNumber) :
Name(name), age(age), phoneNumber(phoneNumber) {}
Void displayInfo() {
Std::cout << “Name: “ << name << “\nAge: “ << age << “\nPhone: “
<< phoneNumber << std::endl;
} };
Class BankAccount { Private:
Int accountNumber; Double balance; Customer* customer;
Public:
BankAccount(int accountNumber, double initialBalance, Customer*
customer) :
accountNumber(accountNumber), balance(initialBalance),
customer(customer) {}
void displayAccountInfo() {
std::cout << “Account Number: “ << accountNumber << “\nBalance:
$” << balance << std::endl; customer->displayInfo();
}
Void deposit(double amount) {
Balance += amount;
Std::cout << “Deposited $” << amount << “. New balance: $” <<
balance << std::endl;
}
Input:
#include <iostream>
// Define a structure to represent an image Struct Image {
Int width;
Int height;
Unsigned char* data; // Pixel data (assume grayscale)
};
// Function to dynamically allocate memory for an image Image*
createImage(int width, int height) {
Image* img = new Image;
Img->width = width;
Img->height = height;
Img->data = new unsigned char[width * height]; Return img;
}
// Function to resize an image
Image* resizeImage(const Image* original, int newWidth, int
newHeight) {
// Create a new image with the new dimensions Image* resized =
createImage(newWidth, newHeight);
// Implement the image resizing logic here (e.g., interpolation)
Return resized; }
Int main() {
Int originalWidth, originalHeight;
Std::cout << “Enter the original width and height of the image: “;
Std::cin >> originalWidth >> originalHeight;
// Dynamically allocate memory for the original image
Image* originalImage = createImage(originalWidth, originalHeight);
// Read or manipulate the original image data if needed
Int newWidth, newHeight;
Std::cout << “Enter the new width and height for resizing: “; Std::cin
>> newWidth >> newHeight;