Vector Addition/Subtraction
Operations like addition and subtraction of two vectors can be simply done by performing the operation on the individual components of the vectors. Like if we have two vectors (x1, y1) and (x2, y2), then the sum of the two vectors is (x1+x2, y1+y2) and the difference between them is (x1-x2, y1-y2).
Implementation:
#include <bits/stdc++.h>
using namespace std;
// Function to perform vector addition
vector<int> addVectors(const vector<int>& v1,
const vector<int>& v2)
{
vector<int> result;
if (v1.size() != v2.size()) {
cerr << "Vectors must be of the same size for "
"addition."
<< endl;
return result; // Returning an empty vector
// indicating an error
}
result.reserve(v1.size());
for (size_t i = 0; i < v1.size(); ++i) {
result.push_back(v1[i] + v2[i]);
}
return result;
}
// Function to perform vector subtraction
vector<int> subtractVectors(const vector<int>& v1,
const vector<int>& v2)
{
vector<int> result;
if (v1.size() != v2.size()) {
cerr << "Vectors must be of the same size for "
"subtraction."
<< endl;
return result; // Returning an empty vector
// indicating an error
}
result.reserve(v1.size());
for (size_t i = 0; i < v1.size(); ++i) {
result.push_back(v1[i] - v2[i]);
}
return result;
}
// Driver code
int main()
{
// Example vectors
vector<int> vector1 = { 1, 2, 3, 4, 5 };
vector<int> vector2 = { 5, 4, 3, 2, 1 };
// Perform vector addition
vector<int> sum = addVectors(vector1, vector2);
cout << "Vector Addition Result: ";
for (int value : sum) {
cout << value << " ";
}
cout << endl;
// Perform vector subtraction
vector<int> difference
= subtractVectors(vector1, vector2);
cout << "Vector Subtraction Result: ";
for (int value : difference) {
cout << value << " ";
}
cout << endl;
return 0;
}
import java.util.Arrays;
public class VectorOperations {
// Function for vector addition
public static int[] addVectors(int[] v1, int[] v2) {
int[] result = new int[v1.length];
// Check if vectors are of the same size for addition
if (v1.length != v2.length) {
System.out.println("Vectors must be of the same size for addition.");
// Returning an empty array indicating an error
return result;
}
// Perform vector addition
for (int i = 0; i < v1.length; i++) {
result[i] = v1[i] + v2[i];
}
return result;
}
// Function for vector subtraction
public static int[] subtractVectors(int[] v1, int[] v2) {
int[] result = new int[v1.length];
// Check if vectors are of the same size for subtraction
if (v1.length != v2.length) {
System.out.println("Vectors must be of the same size for subtraction.");
// Returning an empty array indicating an error
return result;
}
// Perform vector subtraction
for (int i = 0; i < v1.length; i++) {
result[i] = v1[i] - v2[i];
}
return result;
}
public static void main(String[] args) {
// Example vectors
int[] vector1 = {1, 2, 3, 4, 5};
int[] vector2 = {5, 4, 3, 2, 1};
// Perform vector addition
int[] sumResult = addVectors(vector1, vector2);
System.out.println("Vector Addition Result: " + Arrays.toString(sumResult));
// Perform vector subtraction
int[] differenceResult = subtractVectors(vector1, vector2);
System.out.println("Vector Subtraction Result: " + Arrays.toString(differenceResult));
}
}
def add_vectors(v1, v2):
result = []
if len(v1) != len(v2):
print("Vectors must be of the same size for addition.")
return result # Returning an empty list indicating an error
result = [x + y for x, y in zip(v1, v2)]
return result
def subtract_vectors(v1, v2):
result = []
if len(v1) != len(v2):
print("Vectors must be of the same size for subtraction.")
return result # Returning an empty list indicating an error
result = [x - y for x, y in zip(v1, v2)]
return result
# Example vectors
vector1 = [1, 2, 3, 4, 5]
vector2 = [5, 4, 3, 2, 1]
# Perform vector addition
sum_result = add_vectors(vector1, vector2)
print("Vector Addition Result:", *sum_result)
# Perform vector subtraction
difference_result = subtract_vectors(vector1, vector2)
print("Vector Subtraction Result:", *difference_result)
using System;
using System.Collections.Generic;
class Program
{
// Function to perform vector addition
static List<int> AddVectors(List<int> v1, List<int> v2)
{
List<int> result = new List<int>();
if (v1.Count != v2.Count)
{
Console.WriteLine("Vectors must be of the same size for addition.");
return result; // Returning an empty list indicating an error
}
for (int i = 0; i < v1.Count; ++i)
{
result.Add(v1[i] + v2[i]);
}
return result;
}
// Function to perform vector subtraction
static List<int> SubtractVectors(List<int> v1, List<int> v2)
{
List<int> result = new List<int>();
if (v1.Count != v2.Count)
{
Console.WriteLine("Vectors must be of the same size for subtraction.");
return result; // Returning an empty list indicating an error
}
for (int i = 0; i < v1.Count; ++i)
{
result.Add(v1[i] - v2[i]);
}
return result;
}
// Driver code
static void Main()
{
// Example lists
List<int> vector1 = new List<int> { 1, 2, 3, 4, 5 };
List<int> vector2 = new List<int> { 5, 4, 3, 2, 1 };
// Perform vector addition
List<int> sum = AddVectors(vector1, vector2);
Console.Write("Vector Addition Result: ");
foreach (int value in sum)
{
Console.Write(value + " ");
}
Console.WriteLine();
// Perform vector subtraction
List<int> difference = SubtractVectors(vector1, vector2);
Console.Write("Vector Subtraction Result: ");
foreach (int value in difference)
{
Console.Write(value + " ");
}
Console.WriteLine();
}
}
// Function for vector addition
function addVectors(v1, v2) {
let result = new Array(v1.length).fill(0);
// Check if vectors are of the same size for addition
if (v1.length !== v2.length) {
console.log("Vectors must be of the same size for addition.");
// Returning an empty array indicating an error
return result;
}
// Perform vector addition
for (let i = 0; i < v1.length; i++) {
result[i] = v1[i] + v2[i];
}
return result;
}
// Function for vector subtraction
function subtractVectors(v1, v2) {
let result = new Array(v1.length).fill(0);
// Check if vectors are of the same size for subtraction
if (v1.length !== v2.length) {
console.log("Vectors must be of the same size for subtraction.");
// Returning an empty array indicating an error
return result;
}
// Perform vector subtraction
for (let i = 0; i < v1.length; i++) {
result[i] = v1[i] - v2[i];
}
return result;
}
// Example vectors
let vector1 = [1, 2, 3, 4, 5];
let vector2 = [5, 4, 3, 2, 1];
// Perform vector addition
let sumResult = addVectors(vector1, vector2);
console.log("Vector Addition Result: " + sumResult);
// Perform vector subtraction
let differenceResult = subtractVectors(vector1, vector2);
console.log("Vector Subtraction Result: " + differenceResult);
Output
Vector Addition Result: 6 6 6 6 6 Vector Subtraction Result: -4 -2 0 2 4
Basic Geometry for Competitive Programming
Ever wondered how to tackle tricky problems in competitive programming? Well, basic geometry is your secret weapon! In this article, we’re diving into the basics Geometric Algorithms. It’s like a power-up for your problem-solving skills, giving you the tools to crack those coding challenges like a pro.
Table of Content
- What are Geometric Algorithms?
- Why to use Geometric Algorithms?
- Vector Addition/Subtraction
- Dot Product and Cross Product
- Distance from a point to a line
- Intersection Point of two lines
- Practice Problems on Basic Geometry for Competitive Programming