CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Feb 2011
    Posts
    5

    VectorOperations

    I want different operations to be done to all the numbers in an array using methods for each operation. I can't really point out where the errors are in the code.

    Code:
    public class VektorOperations {
    	public static void main(String[] args) {
    
    		double[] a = { 0.14, 23.5, 33.4, 17.4, 89.76 };
    		add(7.3, a);
    		invert(a);
    		mult(34.0, a);
    
    	}
    
    	public static void mult(double add, double[] a) {
    
    		double b = 7.3;
    
    		for (double y : a) {
    			System.out.print(b * y + " ");
    			// this method is supposed to take a float and an array of floats as
    			// parameters
    			// and perform a multiplication of the given float to all array elements
    
    		}
    
    	}
    
    	public static void add(double invert, double[] a) {
    		double c = 34.0;
    
    		for (double y : a) {
    			System.out.print(c + y + " ");
    			// This method has as task to take a float and an array of floats as
    			// parameters
    			// and perform an addition of the given float to all array
    			// elements
    
    		}
    	}
    
    	public static void invert(double[] a) {
    		int x = 1;
    
    		for (double y : a) {
    			System.out.print(x / y + " ");
    			// The method above is supposed to take an array of floats and
    			// perform
    			// an inversion (1 divided by x) of any array element
    
    		}
    
    	}
    }
    The output should look something like this: 4.56989247311828 1.1038961038961037 0.8353808353808354 1.376518218623482 0.3502987842571605

    put the foregoing code prints: 1.022 171.54999999999998 243.82 127.01999999999998 655.248 7.142857142857142 0.0425531914893617 0.029940119760479042 0.0574712643678161 0.011140819964349376 34.14 57.5 67.4 51.4 123.76

    Do anyone know what I should do?
    best regards//
    Last edited by NeWoX; March 6th, 2011 at 06:35 PM.

  2. #2
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: VectorOperations

    Just try to solve one method, for instance the add method, and then you will see what is wrong with the other methods.

    The add method:
    You pass in 2 parameters (one of which is wrongly named 'invert') but you totally ignore the first parameter and use a hard coded value in your calculation instead.

    You haven't explained if the array has to be modified to hold the result of each successive operation but if you are only expecting a final result then each method will have to either modify 'a' or return a new array with the result. So, you will need to store the result of your math operation because at the moment you are computing it, printing it and then forgetting it.
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured