### for loop to find vector length

Ok my assignment has me doing vector math with some canned code provided for me by the instructor This is the header file to the class I'm working with and the .cpp file as far as I've gotten it.

 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849 #pragma once #include "Scalar.h" class Vector2D { public: Vector2D(); Vector2D( const Vector2D& ) ; // copy constructor Vector2D( Scalar element[2] ) ; // initialize with an array Vector2D( double x, double y ) ; Scalar& operator [] ( int index ) ; // use to both read and write elements, just like a normal array Scalar operator [] ( int index ) const ; // use to read elements from const vectors Scalar Length() ; void Normalize() ; // Scalar-Vector product void operator - ( void ) ; // negate a vector void operator *= ( Scalar ) ; void operator /= ( Scalar ) ; Vector2D operator * ( Scalar ) const ; // for the case when the operand order is Vector * Scalar friend Vector2D operator * ( Scalar, Vector2D& ) ; // for the case when the operand order is Scalar * Vector // vector addition Vector2D operator + ( Vector2D& ) const ; Vector2D operator - ( Vector2D& ) const ; void operator += ( Vector2D& ) ; void operator -= ( Vector2D& ) ; // the Dot-Product Scalar operator * ( Vector2D& ) const ; // Construct a vector that is orthogonal (perpendicular) to the given vector Vector2D Orthogonal() const ; bool operator == ( const Vector2D& ) const ; bool operator != ( const Vector2D& ) const ; // use this to test whethere or not a vector == zero vector // it returns a reference to a constant static vector full of 0's static const Vector2D& Zero_Vector() ; protected: static const int dimension = 2 ; Scalar v[dimension] ; };

 123456789101112131415161718192021222324252627282930313233343536373839404142 #include "Vector2D.h" Vector2D::Vector2D() { for(int i=0;i

I'm having trouble seeing which data members I'm multiplying together and what the initial state, continuing state, and after loop action I'm supposed to be using in the for loop.
closed account (o1vk4iN6)
 123456 Scalar Vector2D::Length() { Scalar lengthSquared=0.0; for(int i = 0; i < dimension; ++i) lengthSquared += v[i]*v[i]; return std::sqrt(lengthSquared); }

Really hope this wasn't an assignment, it's really really easy...
Topic archived. No new replies allowed.