Define two arrays x and f each of site 10 to pass the array
Solution
FormulaSum.java :
package org.chegg;
import java.util.Scanner;
public class FormulaSum {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int x[] = new int[10];
int f[] = new int[10];
System.out.println(\"Enter \'x\' array elements:\");
for(int i=0;i<10;i++)
x[i] = sc.nextInt();
System.out.println(\"Enter \'f \'array elements:\");
for(int i=0;i<10;i++)
f[i] = sc.nextInt();
double result = sum(x,f);
System.out.println(\"\ x array:\");
for(int i=0;i<10;i++)
System.out.print(x[i]+\" \");
System.out.println(\"\ f array:\");
for(int i=0;i<10;i++)
System.out.print(f[i]+\" \");
System.out.println(\"\ Summation of arrays by using formula: \"+result);
}
public static double sum(int[] x,int[] f){
double total = 0;
for(int i=1;i<x.length-1;i++){
total = total + (f[i]+f[i+1])*(x[i+1] - x[i]);
}
return (0.5 * total);
}
}
Sample Input & output:
Enter \'x\' array elements:
43 87 89 44 32 56 77 86 54 10
Enter \'f \'array elements:
32 78 55 43 76 89 23 9 59 80
x array:
43 87 89 44 32 56 77 86 54 10
f array:
32 78 55 43 76 89 23 9 59 80
Summation of arrays by using formula: -3632.0
