Define two arrays x and f each of size 10 to pass the array

Define two arrays x and f, each of size 10, to pass the array to a function, named sum. In main: define array, pass arrays, print out the array and the results on screen. In function sum, take arrays from main and sum the arrays using the formula below: sum = 0.5 sigma^size-1_i=1 (f_i + f_i+1)(x_i+1 - x_i)

Solution

#include <iostream>
using namespace std;

double sum(int x[], int f[]){
double result = 0;
for(int i = 0; i < 9; i++){
result += (f[i] + f[i + 1]) * (x[i + 1] - x[i]);
}
result *= 0.5;
return result;
}

int main(){
int x[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
int f[] = {10, 9, 8, 7, 6, 5, 4, 3, 2, 1};
double result = sum(x, f);
cout << \"Array x: \";
for(int i = 0; i < 10; i++) cout << x[i] << \" \";

cout << \"\ Array f: \";
for(int i = 0; i < 10; i++) cout << f[i] << \" \";

cout << \"\ Sum = \" << result << \"\ \";
}

 Define two arrays x and f, each of size 10, to pass the array to a function, named sum. In main: define array, pass arrays, print out the array and the results

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site