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

//program is given in c++

#include <iostream>

using namespace std;
//declare function sum
int sum(int[],int[]);

int main()
{
int x[10],f[10]; //declare array x and f
int i,result;
//prompt and accpet array x
cout<<\"Enter elements of array x:\";
for(i=0;i<10;i++)
cin>>x[i];
//prompt and accept array f
cout<<\"Enter elements of array f:\";
for(i=0;i<10;i++)
cin>>f[i];
//pass array x and f and store resut in result variable
result=sum(x,f);
//print array x
cout<<\"Array x:\";
for(i=0;i<10;i++)
cout<<x[i]<<\" \";
//print array f
cout<<endl;
cout<<\"Array f:\";
for(i=0;i<10;i++)
cout<<f[i]<<\" \";
cout<<endl;
//print result
cout<<\"Result is:\"<<result;

return 0;
}
//define a function
int sum(int x[],int f[])
{
int j,total=0;
int sum;
//use for loop for summation
for(j=0;j<9;j++)
{
total=total+((f[j]+f[j+1])*(x[j+1]-x[j]));
  
}
//after summation multiply by 0.5
sum=0.5*total;
return(sum);
}

/* Output

Enter elements of array x:1 2 3 4 5 12 6 2 7 22

Enter elements of array f:21 3 4 6 5 3 1 7 4 2

Array x:1 2 3 4 5 12 6 2 7 22

Array f:23 21 3 4 6 5 3 1 7 4

Result is:151

 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
 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