Write a C program for the following Define two arrays x and

Write a C program for the following:

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_i=1^size-1 (f_i + f_i+1) (x_i+1-x_i)

Solution

#include <stdio.h>
#include <stdlib.h>

//function sum definition
int sum(int *x,int *f)
{
float sum1=0;
int i=0;
int t=0;
int z=0;

// sum formula
for(i=0;i<10-1;i++)
{
z=(f[i]+f[i+1]);
t= (x[i+1]-x[i]);
sum1 = sum1 + (float)(z*t)/2.0;
}

return sum1;

}

// main function
int main()
{
int i;
float result;
//Define a array
int x[10]={3,3,5,2,4,3,5,7,6,2};
int f[10]={4,5,7,4,9,5,7,9,4,6};


//print array in main function
printf(\"x array is : \");
for(i=0;i<10;i++)
{
printf(\"%d, \",x[i]);
}
printf(\"\ f array is : \");
for(i=0;i<10;i++)
{
printf(\"%d, \",f[i]);
}

//sum function calling parameter pass by reference
result=sum(&x,&f);

//print sum value
printf(\"\ Sum of the array using formula is : %.2f\",result);

return 0;
}

========================================

output sample:-

x array is : 3, 3, 5, 2, 4, 3, 5, 7, 6, 2,
f array is : 4, 5, 7, 4, 9, 5, 7, 9, 4, 6,
Sum of the array using formula is : 3.00

---------------------------------------------------------------------------------------------

If you have any query, please feel free to ask.

Thanks a lot.

Write a C program for the following: 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,
Write a C program for the following: 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,

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site