i need help with this program public class Loop5SumOfProducts { /* TODO: Define
ID: 3575693 • Letter: I
Question
i need help with this program
public class Loop5SumOfProducts {
/* TODO: Define a method that return the sum of the product of
the corresponding elements of two given arrays with the same length.
For example, sumProduct of {a, b, c} and {d, e, f} is a*d+b*e+c*f.
*/
public static void main(String[] args) {
int[] list1 = {1, 2, 3};
int[] list2 = {3, 4, 0};
System.out.println("sum of products of list1 and list2 is " + sumProducts(list1, list2));
int[] list3 = {1, 2, 3, 4};
int[] list4 = {3, 4, 0, 2};
System.out.println("sum of products of list3 and list4 is " + sumProducts(list3, list4));
}
}
Explanation / Answer
//method
public static int sumProducts(int list1[], int list2[])
{
int sum_of_product = 0;
int size;
if(list1.length< list2.length)
{
size = list1.length;
}
else
{
size = list2.length;
}
for(int i = 0 ; i < size; i++)
{
sum_of_product+=list1[i]*list2[i];
}
return sum_of_product;
}
--------------------------------------------
output
sum of products of list1 and list2 is 11
sum of products of list3 and list4 is 19
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.