APP - Assignment 3
APP - Assignment 3
BY
THE ONE AND ONLY
Simple Mini Calculator Program using Subroutines:
#include <iostream>
int main() {
int num1, num2;
char op;
int 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!\n";
return 1;
}
return 0;
}
Output:
#include <iostream>
#include <string>
#include <vector>
Output
3. subroutine program to calculate the area and perimeter of different geometric shapes
#include <iostream>
#include <cmath>
int main() {
int choice;
double area, perimeter;
do {
std::cout << "Select a shape to calculate properties:\n";
std::cout << "1. Circle\n";
std::cout << "2. Rectangle\n";
std::cout << "3. Triangle\n";
std::cout << "4. 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;
circleProperties(radius, area, perimeter);
std::cout << "Area: " << area << "\n";
std::cout << "Perimeter: " << perimeter << "\n";
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;
rectangleProperties(length, width, area, perimeter);
std::cout << "Area: " << area << "\n";
std::cout << "Perimeter: " << perimeter << "\n";
break;
}
case 3: {
double side1, side2, side3;
std::cout << "Enter the length of side 1 of the triangle: ";
std::cin >> side1;
std::cout << "Enter the length of side 2 of the triangle: ";
std::cin >> side2;
std::cout << "Enter the length of side 3 of the triangle: ";
std::cin >> side3;
triangleProperties(side1, side2, side3, area, perimeter);
std::cout << "Area: " << area << "\n";
std::cout << "Perimeter: " << perimeter << "\n";
break;
}
case 4:
std::cout << "Exiting the program.\n";
break;
default:
std::cout << "Invalid choice. Try again.\n";
}
return 0;
}
Output
#include <iostream>
#include <string>
#include <cctype>
if (std::tolower(str[left]) != std::tolower(str[right])) {
return false; // Characters do not match, not a palindrome
}
left++;
right--;
}
int main() {
std::string input;
if (isPalindrome(input)) {
std::cout << "The string is a palindrome.\n";
} else {
std::cout << "The string is not a palindrome.\n";
}
return 0;
}
Output
#include <iostream>
left++;
right--;
}
}
int main() {
int size;
std::cout << "Enter " << size << " integers: ";
for (int i = 0; i < size; i++) {
std::cin >> arr[i];
}
reverseArray(arr, size);
delete[] arr;
return 0;
}
Output
#include <iostream>
int main() {
int size;
std::cout << "Enter " << size << " integers: ";
for (int i = 0; i < size; i++) {
std::cin >> arr[i];
}
std::cout << "Sum of elements in the array: " << sum << std::endl;
return 0;
}
Output
#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;
std::ifstream inputFile(filename);
if (!inputFile.is_open()) {
std::cout << "Error: Unable to open the file.\n";
return 1;
}
std::vector<std::string> lines;
std::string line;
inputFile.close();
if (lines.empty()) {
std::cout << "The file is empty.\n";
return 0;
}
return 0;
}
Output
8. Create a program that uses dynamic memory allocation to implement a stack data
structure to push and pop elements.
#include <iostream>
class Stack {
private:
int* arr;
int capacity;
int top;
public:
Stack(int capacity) {
this->capacity = capacity;
arr = new int[capacity];
top = -1;
}
~Stack() {
delete[] arr;
}
void pop() {
if (top == -1) {
std::cout << "Stack Underflow: Cannot pop element, the stack is empty.\n";
return;
}
top--;
}
int peek() {
if (top == -1) {
std::cout << "Stack is empty.\n";
return -1;
}
return arr[top];
}
bool isEmpty() {
return top == -1;
}
void display() {
if (top == -1) {
std::cout << "Stack is empty.\n";
return;
}
int main() {
int capacity;
std::cout << "Enter the capacity of the stack: ";
std::cin >> capacity;
Stack stack(capacity);
stack.push(10);
stack.push(20);
stack.push(30);
std::cout << "Stack top element: " << stack.peek() << std::endl;
stack.pop();
stack.push(40);
stack.display();
return 0;
}
Output
9. Implement a program that uses dynamic memory allocation to simulate a banking system
that stores customer information, account details, and transactions.
#include <iostream>
#include <string>
#include <vector>
struct Transaction {
std::string type;
double amount;
};
struct Account {
int accountNumber;
std::string customerName;
double balance;
std::vector<Transaction> transactions;
};
int main() {
std::vector<Account*> accounts;
switch (choice) {
case 1: {
std::string customerName;
std::cout << "Enter customer name: ";
std::cin.ignore(); // Ignore the newline character left by previous input
std::getline(std::cin, customerName);
if (account == nullptr) {
std::cout << "Account not found.\n";
break;
}
if (account == nullptr) {
std::cout << "Account not found.\n";
break;
}
if (account == nullptr) {
std::cout << "Account not found.\n";
break;
}
displayAccountInfo(account);
break;
}
case 5: {
int accountNum;
std::cout << "Enter account number: ";
std::cin >> accountNum;
if (account == nullptr) {
std::cout << "Account not found.\n";
break;
}
displayTransactionHistory(account);
break;
}
case 6:
// Free memory for all accounts
for (const auto& account : accounts) {
deleteAccount(account);
}
std::cout << "Exiting the program.\n";
break;
default:
std::cout << "Invalid choice. Try again.\n";
}
} while (choice != 6);
return 0;
}
Output
#include <iostream>
#include <fstream>
#include <vector>
#include <opencv2/opencv.hpp>
struct Image {
int width;
int height;
std::vector<std::vector<int>> pixels;
};
return resizedImage;
}
if (image.empty()) {
std::cerr << "Error: Unable to load the image from file: " << filename << std::endl;
exit(EXIT_FAILURE);
}
return image;
}
return customImage;
}
int main() {
std::string filename = "your_image_file.jpg"; // Replace with the path to your image file
return 0;
}
Output
Original Image:
16711680 65280 255
16776960 16711935 65535
13421772 10066329 3355443
Enter the scale factor (0-1 for downsizing, >1 for upsizing): 5
Resized Image:
16711680 16711680 16711680 16711680 16711680 65280 65280 65280 65280 65280 255
255 255 255 255
16711680 16711680 16711680 16711680 16711680 65280 65280 65280 65280 65280 255
255 255 255 255
16711680 16711680 16711680 16711680 16711680 65280 65280 65280 65280 65280 255
255 255 255 255
16711680 16711680 16711680 16711680 16711680 65280 65280 65280 65280 65280 255
255 255 255 255
16711680 16711680 16711680 16711680 16711680 65280 65280 65280 65280 65280 255
255 255 255 255
16776960 16776960 16776960 16776960 16776960 16711935 16711935 16711935
16711935 16711935 65535 65535 65535 65535 65535
16776960 16776960 16776960 16776960 16776960 16711935 16711935 16711935
16711935 16711935 65535 65535 65535 65535 65535
16776960 16776960 16776960 16776960 16776960 16711935 16711935 16711935
16711935 16711935 65535 65535 65535 65535 65535
16776960 16776960 16776960 16776960 16776960 16711935 16711935 16711935
16711935 16711935 65535 65535 65535 65535 65535
16776960 16776960 16776960 16776960 16776960 16711935 16711935 16711935
16711935 16711935 65535 65535 65535 65535 65535
13421772 13421772 13421772 13421772 13421772 10066329 10066329 10066329
10066329 10066329 3355443 3355443 3355443 3355443 3355443
13421772 13421772 13421772 13421772 13421772 10066329 10066329 10066329
10066329 10066329 3355443 3355443 3355443 3355443 3355443
13421772 13421772 13421772 13421772 13421772 10066329 10066329 10066329
10066329 10066329 3355443 3355443 3355443 3355443 3355443
13421772 13421772 13421772 13421772 13421772 10066329 10066329 10066329
10066329 10066329 3355443 3355443 3355443 3355443 3355443
13421772 13421772 13421772 13421772 13421772 10066329 10066329 10066329
10066329 10066329 3355443 3355443 3355443 3355443 3355443
Grayscale Image:
5592405 5592405 5592405
11184810 11184810 11184810
13421772 10066329 3355443