Write a method called arrayProduct which takes two parameter

Write a method called arrayProduct which takes two parameters called array1and array2, both are arrays of type double, contain at least one value, have the same length, and there are no \"empty\" cells, i.e. their length=the number of values.

The method returns an array of type double called resultArray, which has the same length as the input arrays, and contains the pairwise product of the data from array1 and array2.
In subscript notation:

resultArrayi = array1i * array2i

for each value i in the input arrays.

For example, if the input arrays contained these values:

array1 = 3.5, 67.1, 4.2, 5.5, array2 = 2.9, 7.0, 10.7, 0.1

The method would return this array:

10.15, 469.69, 44.94, 0.55

Remember, your code must work for ANY values in the input arrays, not just the example given above.

Enter your answer below.

Write a method called arrayProduct which takes two parameters called array1and array2, both are arrays of type double, contain at least one value, have the same length, and there are no \"empty\" cells, i.e. their length=the number of values. The method returns an array of type double called resultArray, which has the same length as the input arrays, and contains the pairwise product of the data from array1 and array2. In subscript notation: resultArray_i= array1_i * array2_i for each value i in the input arrays. For example, if the input arrays contained these values: array1 = 3.5, 67.1, 4.2, 5.5, array2 = 2.9, 7.0, 10.7, 0.1 The method would return this array: 10.15, 469.69, 44.94, 0.55 Remember, your code must work for ANY values in the input arrays, not just the example given above. Enter your answer below.

Solution

PROGRAM CODE:

ProductArray.java

package samplepro;

import java.util.Scanner;

public class ProductArray {

   //method for product operation
   public static double[] product(double array1[], double array2[])
   {
       int lengthofArray = array1.length;
       double array3[] = new double[lengthofArray];
      
       for(int i=0; i<lengthofArray; i++)
       {
           array3[i] = array1[i] * array2[i];
       }
       return array3;
   }
  
   //method to print
   public static void printArray(double array[]) {
       for(int i=0; i<array.length; i++)
       {
           System.out.print(array[i] + \" \");
       }
       System.out.println(\"\ \");
   }
  
   //method to load the array from the inputstream
   public static double[] loadArray(int length) {
       Scanner doublesc = new Scanner(System.in);
       double array[] = new double[length];
       for(int i=0; i<length; i++)
       {
           array[i] = doublesc.nextDouble();
       }
       return array;
   }
   /**
   * main Method
   * @param args
   */
   public static void main(String[] args) {
       System.out.println(\"Enter the number of values in the array: \");
       Scanner sc = new Scanner(System.in);
       int num = sc.nextInt();
       double array1[];
       double array2[];
       System.out.println(\"Enter the value for array 1: \");
       array1 = loadArray(num);
       System.out.println(\"Enter the value for array 2: \");
       array2 = loadArray(num);
       double resultArray[] = product(array1, array2);
       System.out.println(\"\ Array1: \");
       printArray(array1);
       System.out.println(\"\ Array2: \");
       printArray(array2);
       System.out.println(\"\ Result Array: \");
       printArray(resultArray);
   }

}

OUTPUT:

Enter the number of values in the array:
4
Enter the value for array 1:
3.5 67.1 4.2 5.5
Enter the value for array 2:
2.9 7.0 10.7 0.1

Array1:
3.5 67.1 4.2 5.5


Array2:
2.9 7.0 10.7 0.1


Result Array:
10.15 469.69 44.94 0.55

Write a method called arrayProduct which takes two parameters called array1and array2, both are arrays of type double, contain at least one value, have the same
Write a method called arrayProduct which takes two parameters called array1and array2, both are arrays of type double, contain at least one value, have the same

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site