Question

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] + " ");
     }
   }
 }
Was it helpful?

Solution 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] + " ");
     }
   }
 }

OTHER TIPS

You're never calling the scalarMultiply method.

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]);
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top