#include <iostream>
#include <cstdlib> // For malloc and free
using namespace std; // Using the standard namespace
// Define the Vector class
class Vector {
private:
int n; [cite_start]// Stores the dimension of the vector [cite: 6]
double* elements; [cite_start]// Dynamically allocated array for vector elements [cite: 8]
public:
// 1. Constructor
// Takes an integer n and an array to initialize the vector.
Vector(int dimension, double* initial_elements) : n(dimension) {
[cite_start]// Dynamically allocates memory using malloc [cite: 13]
elements = (double*)malloc(n * sizeof(double));
if (elements == nullptr) {
cerr << "Memory allocation failed!" << endl;
exit(1);
}
[cite_start]// Copies the array items [cite: 13]
for (int i = 0; i < n; ++i) {
elements[i] = initial_elements[i];
}
}
// 2. Destructor
~Vector() {
[cite_start]// Frees the dynamically allocated memory for elements using free [cite: 16]
free(elements);
}
// 3. Function Overloading: add()
[cite_start]// 1. Vector Addition: add(Vector& v) (same dimension) [cite: 20]
[cite_start]// Note: Passed by reference to prevent double free error [cite: 36, 37]
void add(Vector& v) {
for (int i = 0; i < n; ++i) {
this->elements[i] += v.elements[i];
}
}
[cite_start]// 2. Scalar Addition: add(double value) (to all elements) [cite: 21]
void add(double value) {
for (int i = 0; i < n; ++i) {
this->elements[i] += value;
}
}
// 3. Function Overloading: subtract()
[cite_start]// 1. Vector Subtraction: subtract(Vector& v) (same dimension) [cite: 23]
void subtract(Vector& v) {
for (int i = 0; i < n; ++i) {
this->elements[i] -= v.elements[i];
}
}
[cite_start]// 2. Scalar Subtraction: subtract(double value) (from all elements) [cite: 24, 25]
void subtract(double value) {
for (int i = 0; i < n; ++i) {
this->elements[i] -= value;
}
}
// 3. Function Overloading: product()
[cite_start]// 1. Dot Product: product(Vector& v) (same dimension, returns double) [cite: 27]
double product(Vector& v) {
double dot_product = 0.0;
for (int i = 0; i < n; ++i) {
dot_product += this->elements[i] * v.elements[i];
}
return dot_product;
}
[cite_start]// 2. Scalar Multiplication: product(double value) (with all elements) [cite: 28]
void product(double value) {
for (int i = 0; i < n; ++i) {
this->elements[i] *= value;
}
}
// 4. Other Member Functions
[cite_start]// Sets the value of the element at the specified index [cite: 31]
void set_element(int index, double value) {
if (index >= 0 && index < n) {
this->elements[index] = value;
} else {
cerr << "Index out of bounds in set_element!" << endl;
}
}
[cite_start]// Returns the value of the element at the specified index [cite: 33]
double get_element(int index) {
if (index >= 0 && index < n) {
return this->elements[index];
} else {
cerr << "Index out of bounds in get_element! Returning 0.0" << endl;
return 0.0;
}
}
[cite_start]// Displays the vector in a user-friendly format [cite: 35]
void display() {
cout << "[";
for (int i = 0; i < n; ++i) {
cout << this->elements[i];
if (i < n - 1) {
cout << ", ";
}
}
cout << "]" << endl;
}
};
[cite_start]// Sample Main function [cite: 39]
int main() {
[cite_start]double arr1[] = {1.2, 2.3, 3.4, 4.5}; [cite: 41]
Vector v1(4, arr1); [cite_start]// 4-dimensional vector initialized with arrl [cite: 42]
[cite_start]cout << "Vector v1: "; [cite: 43]
v1.display(); [cite_start]// Output: [1.2, 2.3, 3.4, 4.5] [cite: 44]
[cite_start]double arr2[] = {5.0, 6.0, 7.0, 8.0}; [cite: 45]
Vector v2(4, arr2); [cite_start]// Another 4-dimensional vector initialized with arr2 [cite: 46]
[cite_start]cout << "Vector v2: "; [cite: 47]
v2.display(); [cite_start]// Output: [5.0, 6.0, 7.0, 8.0] [cite: 48]
[cite_start]v1.add(v2); [cite: 49]
[cite_start]cout << "After adding v2 to v1: "; [cite: 50]
v1.display(); [cite_start]// Output: [6.2, 8.3, 10.4, 12.5] [cite: 51]
[cite_start]v1.add(2.0); [cite: 52]
cout << "After adding scalar 2.0 to v1: ";
v1.display(); [cite_start]// Output: [8.2, 10.3, 12.4, 14.5] [cite: 53]
[cite_start]v1.subtract(v2); [cite: 54]
cout << "After subtracting v2 from v1: ";
v1.display(); [cite_start]// Output: [3.2, 4.3, 5.4, 6.5] [cite: 55]
[cite_start]v1.subtract(1.5); [cite: 56]
cout << "After subtracting scalar 1.5 from v1: ";
v1.display(); [cite_start]// Output: [1.7, 2.8, 3.9, 5.0] [cite: 57]
[cite_start]double dotProduct = v1.product(v2); [cite: 58]
cout << "Dot product of v1 and v2: " << dotProduct << endl; [cite_start]// Output: 92.6 [cite: 59]
[cite_start]v1.product(3.0); [cite: 60]
[cite_start]cout << "After multiplying v1 with scalar 3.0: "; [cite: 61]
v1.display(); [cite_start]// Output: [5.1, 8.4, 11.7, 15.0] [cite: 62]
[cite_start]return 0; [cite: 63]
}