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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#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] ;
};


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include "Vector2D.h"

Vector2D::Vector2D()
{
         for(int i=0;i<dimension;i++) v[i]=0.0f;
}

Vector2D::Vector2D(const Vector2D& other)
{
         *this=other;
}

Vector2D::Vector2D(double x, double y)
{
          v[X] = Scalar( x );
          v[Y] = Scalar( y );
}

 Vector2D:: Vector2D(Scalar element[2])
{
         for(int i=0;i<dimension;i++)
                   v[i]=element[i];
}

Scalar& Vector2D::operator[](int index)
{
          return v[index];
}

Scalar Vector2D::operator[](int index) const
{
          return v[index];
}

Scalar Vector2D::Length()
{
          Scalar lengthSquared=0.0;
write a for loop that computes length squared
 return Math:: <--then fill-in the blank here that returns the length using sqrt
 

}


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)
1
2
3
4
5
6
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.