2

Write a method scalarMultiply which takes as input a double[] array, and a double scale, and returns void. The method should modify the input array by multiplying each value in the array by scale. . Question to consider: When we modify the input array, do we actually modify the value of the variable array ?

Here is what i did until now, but i dont know what i did wrong because it still is not working.

public class warm4{

  public static void main(String[] args){
    double[] array1 = {1,2,3,4};
     double scale1 = 3;    
    }
 }
   public static void scalarMultiply(double[] array, double scale){
     for( int i=0; i<array.length; i++){
     array[i] = (array[i]) * scale; 
     System.out.print(array[i] + " ");
     }
   }
 }
3
  • 4
    you're never calling the scalarMultiply method Commented Oct 29, 2013 at 14:47
  • @Tyler That's an answer. Commented Oct 29, 2013 at 14:48
  • 2
    You have 5 { and 6 }... Commented Oct 29, 2013 at 14:48

3 Answers 3

6

You're never calling the scalarMultiply method.

Sign up to request clarification or add additional context in comments.

1 Comment

Also he should pay attention to { and }.
2

You're never calling scalarMultiply and the number of the brackets is incorrect.

public class warm4{

  public static void main(String[] args){
     double[] array1 = {1,2,3,4};
     double scale1 = 3;    
     scalarMultiply(array1, scale1);
   }

   public static void scalarMultiply(double[] array, double scale){
       for( int i=0; i<array.length; i++){
       array[i] = (array[i]) * scale; 
       System.out.print(array[i] + " ");
     }
   }
 }

Comments

0

Your method is OK. But you must call it from your main:

public static void main(String[] args){
    double[] array1 = {1,2,3,4};
    double scale1 = 3;    
    scalarMultiply(array1, scale1);
    for (int i = 0; i < array1.length; i++) {
        System.out.println(array1[i]);
    }
}

1 Comment

ah thank you! im new to java, didnt know that we had to call it x). thanks again

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.