0% found this document useful (0 votes)
12 views18 pages

App Week3 Assignment Abiram

The document outlines an advanced programming assignment consisting of ten different programming tasks implemented in C++. These tasks include creating a mini calculator, a student database, geometric calculations, palindrome checks, array manipulations, dynamic memory allocation for arrays, file reading, stack implementation, banking system simulation, and image processing. Each task includes code snippets and expected outputs demonstrating the functionality of the programs.

Uploaded by

kpnx8cpmmk
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views18 pages

App Week3 Assignment Abiram

The document outlines an advanced programming assignment consisting of ten different programming tasks implemented in C++. These tasks include creating a mini calculator, a student database, geometric calculations, palindrome checks, array manipulations, dynamic memory allocation for arrays, file reading, stack implementation, banking system simulation, and image processing. Each task includes code snippets and expected outputs demonstrating the functionality of the programs.

Uploaded by

kpnx8cpmmk
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

ADVANCED PROGRAMMING

PRACTICE

ASSIGNMENT-3

RA2211026010486
G.Abiram
Z2-cse Aiml

PROGRAMS

1 . simple mini calculator program in C++ that uses subroutines for


basic arithmetic operations

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; }

Double subtract(double a, double b) { Return a – b;


}
Double multiply(double a, double b) { Return a * b;
}
Double divide(double a, double b) { If (b == 0) {
Std::cout << “Cannot divide by zero!”;
Exit(1); }
Return a / b; }

Output:

Enter first number: 5


Enter an operator (+, -, *, /): *
Enter second number: 3
Result: 15
2. Write a complete students database with subroutines involves
storing and managing student Information using appropriate data
structures and providing various functionalites to Interact with the
database and implement in C++ with subroutines:

Input :

#include <iostream> #include <string> #include <vector> #include


<map>
// Define student structure Struct Student {
Std::string name;

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:

Std::cout << “Exiting...\n”;


Break; Default:
Std::cout << “Invalid choice. Please try again.\n”; }
} while (choice != 3);
Return 0;
}

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

3. Design a subroutine program to calculate the area and perimeter


of different geometric shapes (circle, rectangle, triangle, etc.)

Input:

include <iostream> #include <cmath>


// Function to calculate the area of a circle Double circleArea(double
radius) {
Return M_PI * radius * radius; }
// Function to calculate the perimeter of a circle Double
circlePerimeter(double radius) {
Return 2 * M_PI * radius; }
// Function to calculate the area of a rectangle Double
rectangleArea(double length, double width) {
Return length * width;

}
// 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;

Std::cout << “Area of the circle: “ << circleArea(radius) << std::endl;


Std::cout << “Perimeter of the circle: “ << circlePerimeter(radius) <<
std::endl; Break;
}
Case 2: {
Double length, width;
Std::cout << “Enter the length of the rectangle: “;
Std::cin >> length;
Std::cout << “Enter the width of the rectangle: “;
Std::cin >> width;
Std::cout << “Area of the rectangle: “ << rectangleArea(length,
width) << std::endl;
Std::cout << “Perimeter of the rectangle: “ <<
rectanglePerimeter(length, width) << std::endl; Break;
}
Case 3: {
Double base, height;
Std::cout << “Enter the base of the triangle: “;
Std::cin >> base;
Std::cout << “Enter the height of the triangle: “;
Std::cin >> height;
Std::cout << “Area of the triangle: “ << triangleArea(base, height) <<
std::endl; Break;
}
Case 4:
Std::cout << “Exiting...\n”;
Break; Default:
Std::cout << “Invalid choice. Please try again.\n”; }

} while (choice != 4);


Return 0;
}

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 :

#include <iostream> #include <string> #include <algorithm>


// Function to check if a string is a palindrome Bool
isPalindrome(const std::string& str) {
Std::string reversedStr = str; Std::reverse(reversedStr.begin(),
reversedStr.end()); Return str == reversedStr;
}
Int main() {
Std::string input;
Std::cout << “Enter a string: “; Std::cin >> input;
If (isPalindrome(input)) {
Std::cout << “\”” << input << “\” is a palindrome.” << std::endl;
} else {
Std::cout << “\”” << input << “\” is not a palindrome.” << std::endl;
}

Return 0;
}

Output:

Enter a string: radar


"radar" is a palindrome.

Enter a string: hello


"hello" is not a palindrome.
5. Implement a subroutine program to reverse an array of integers
in place.

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: “;

For (int I = 0; I < size; ++i) { Std::cout << arr[i] << “ “;


}
Std::cout << std::endl;
reverseArray(arr, size);
std::cout << “Reversed array: “; for (int I = 0; I < size; ++i) {
std::cout << arr[i] << “ “; }
Std::cout << std::endl;
Return 0;
}

Output:

Enter the size of the array: 5


Enter the elements of the array:
12345
Original array: 1 2 3 4 5
Reversed array: 5 4 3 2 1

6 Write a program that dynamically allocates memory for an array of


integers based on user Input and then finds the sum of all elements
in the array.

Input:
#include <iostream>
Int main() { Int size;
Std::cout << “Enter the size of the array: “; Std::cin >> size;
// Dynamically allocate memory for the array

Int* arr = new int[size];


Std::cout << “Enter the elements of the array:” << std::endl; For (int
I = 0; I < size; ++i) {
Std::cin >> arr[i]; }
// Calculate the sum of all elements in the array Int sum = 0;
For (int I = 0; I < size; ++i) {
Sum += arr[i]; }
Std::cout << “Sum of array elements: “ << sum << std::endl;
// Free the dynamically allocated memory Delete[] arr;
Return 0; }

Output:

Enter the size of the array: 5


Enter the elements of the array:
1
2
3
4
5
Sum of array elements: 15

7. Implement a program that reads a text file and dynamically stores


each line as a string in Memory. Then, display the content of the file
with line numbers.

Input:

#include <iostream> #include <fstream>

#include <vector> #include <string>


Int main() {
Std::string filename;
Std::cout << “Enter the name of the text file: “; Std::cin >> filename;
// Open the text file Std::ifstream inputFile(filename);
If (!inputFile.is_open()) {
Std::cerr << “Failed to open the file.” << std::endl; Return 1;
}
Std::vector<std::string> lines; // To store each line Std::string line;
// Read each line from the file and store it in the vector While
(std::getline(inputFile, line)) {
Lines.push_back(line); }
// Close the file inputFile.close();
// Display the content of the file with line numbers For (size_t I = 0; I
< lines.size(); ++i) {
Std::cout << “Line “ << I + 1 << “: “ << lines[i] << std::endl; }
Return 0;
}

8. Create a program that uses dynamic memory allocation to


implement a stack data structure To push and pop elements.

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; }

// Push an element onto the stack Void push(int value) {


If (top == capacity – 1) {
Std::cout << “Stack is full. Cannot push element.” << std::endl;
Return;
}
Arr[++top] = value;
Std::cout << “Pushed “ << value << “ onto the stack.” << std::endl;
}
// Pop an element from the stack Int pop() {
If (top == -1) {
Std::cout << “Stack is empty. Cannot pop element.” << std::endl;
Return -1; // Return a default value
}
Int popped = arr[top--];
Std::cout << “Popped “ << popped << “ from the stack.” <<
std::endl; Return popped;
} };
Int main() { Int size;
Std::cout << “Enter the size of the stack: “; Std::cin >> size;
Stack stack(size);

Stack.push(10); Stack.push(20); Stack.push(30);


Std::cout << “Popped: “ << stack.pop() << std::endl; std::cout <<
"Popped: " << stack.pop() << std::endl;
stack.push(40);
return 0;
}

9 .Implement a program that uses dynamic memory allocation to


simulate a banking system That stores customer information,
account details, and transactions.

Input:

#include <iostream> #include <string> #include <vector>


Class Customer { Private:
Std::string name;
Int age;
Long long int phoneNumber;

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;
}

Void withdraw(double amount) { If (balance >= amount) {


Balance -= amount;
Std::cout << “Withdrawn $” << amount << “. New balance: $” <<
balance << std::endl; } else {
Std::cout << “Insufficient balance.” << std::endl; }
} };
Int main() {
Std::vector<Customer*> customers;
// Create customers
Customers.push_back(new Customer(“Alice”, 30, 1234567890));
Customers.push_back(new Customer(“Bob”, 25, 9876543210));
// Create bank accounts
BankAccount* account1 = new BankAccount(1001, 1000.0,
customers[0]); BankAccount* account2 = new BankAccount(1002,
500.0, customers[1]);
// Perform transactions Account1->displayAccountInfo();
Account1->deposit(500.0); Account1->withdraw(200.0);
Account2->displayAccountInfo(); Account2->withdraw(600.0);

// Clean up dynamic memory


For (Customer* customer : customers) {
Delete customer; }
Delete account1; Delete account2;
Return 0;
}

10.Design a program that dynamically allocates memory for an


image processing application. Allowing users to resize and
manipulate Images.

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;

// Resize the image


Image* resizedImage = resizeImage(originalImage, newWidth,
newHeight);
// Display or save the resized image if needed
// Clean up memory
Delete[] originalImage->data; Delete originalImage; Delete[]
resizedImage->data; Delete resizedImage;
Return 0;
}

You might also like