Results 1 to 5 of 5
  1. #1
    Join Date
    Feb 2013
    Posts
    33
    Rep Power
    0

    Exclamation Finding the length of a vector

    I'm not sure if I did this right but when I run it, the answer is 0.0. I want to know what I am doing wrong.

    What i'm asked to do:

    Write a function that finds the length of a vector.
    double Length( double X[ ] )
    The length of a vector is the square root of the sum of the squares of the elements in the array. Use dynamic memory and allocate 10 elements for X. Initialize the data values of X to the counting numbers 1-10.


    Java Code:
    class Length {
       public Length() {}
    	
    	double length ( double[] X ) {
    		double sum = 0;
    		double l = 0;
    
    	   for ( int i = 1; i < X.length; i++ ) {
    		sum += Math.pow(X[i], 2);
    		l = Math.sqrt(sum);
    		return l;
            }
    	}
    }


    Java Code:
    class LengthTest {
       public static void main( String [] args ) {
    	Length myLength = new Length();
    
    	   double[] X; 
    
    	   X = new double [ 11 ];
    
    	   System.out.println( );
    	   System.out.println("Length of the vector is " + myLength.length(X) + ".");
    	   System.out.println( );
    
    	}
    }
    Last edited by wheehoowaffles; 04-06-2013 at 04:14 AM.

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,574
    Rep Power
    12

    Default Re: Finding the length of a vector

    What are the numbers in the array array when you pass it to the length() method?

    Also it would be a good idea to add some braces to the for loop in the length() method to show clearly just what is being repeated each time around the loop. (although this isn't the problem here. but every ounce of clarity helps when - or before - things go wrong!)

  3. #3
    Join Date
    Feb 2013
    Posts
    33
    Rep Power
    0

    Default Re: Finding the length of a vector

    It said to initialize the data values of X from 1-10 so I tried doing it by using a for loop.

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,574
    Rep Power
    12

    Default Re: Finding the length of a vector

    You see that the place to do that initialisation is in the main() method, right?

  5. #5
    Join Date
    Feb 2013
    Posts
    33
    Rep Power
    0

    Default Re: Finding the length of a vector

    Yes I know. I figured it out. Thanks anyway.

Similar Threads

  1. Length of an Array: length() or just length ?
    By Jack_Tauson_Sr in forum New To Java
    Replies: 8
    Last Post: 03-18-2013, 09:16 PM
  2. finding possible ordering set of vector
    By sara12345 in forum New To Java
    Replies: 7
    Last Post: 03-16-2010, 09:35 AM
  3. finding length on a number
    By thekrazykid in forum New To Java
    Replies: 8
    Last Post: 12-12-2008, 09:07 PM
  4. Finding elements in a vector
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-14-2008, 09:37 PM
  5. Replies: 2
    Last Post: 03-25-2008, 06:49 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •