public Vector(Vector vec) { this.vector = new List<int>(); for (int i=0;i<vec.vector.Count;i++) { this.vector.Add(vec.vector[i]); } }
public static Vector operator *(Vector a, int b) { int aLen = a.VectorLenght(); int[] newVecArrayForConstructor = new int[aLen]; for (int i = 0; i < aLen; i++) { newVecArrayForConstructor[i] =a[i]* b; } Vector newVector = new Vector(newVecArrayForConstructor); return newVector; }
static void Main(string[] args) { int[] arr1 = new int[5] { 1, 3, 5, 3, 2 }; int[] arr2 = new int[5] { 3, 3, 4, 7, 2 }; Vector vec = new Vector(arr1); Vector vec2 = new Vector(arr2); Vector result = vec + vec2; int len = result.VectorLenght(); for(int i=0;i<len; i++) { Console.Write(result[i]+ " "); } result = vec + len; Console.WriteLine(); result = vec * 3; for (int i = 0; i < len; i++) { Console.Write(result[i] + " "); } }
//define operators +, - between vectors (check if the 2 vectors have the same dimension!) //defining + public static Vector operator +(Vector a,Vector b) { if(a.vectorDimension != b.vectorDimension && a.VectorLenght() != b.VectorLenght()) { throw new Exception("The vectors must be from the same dimension!"); } int newVecLen = a.VectorLenght(); int[] PointArrayForConstructor = new int[newVecLen]; for(int i=0;i<PointArrayForConstructor.Length;i++) { PointArrayForConstructor[i] = a[i] + b[i]; } Vector newbie = new Vector(PointArrayForConstructor); return newbie; }