Write a C code to perform the following tasks: In main: Define an array of SIZE
ID: 3674417 • Letter: W
Question
Write a C code to perform the following tasks: In main: Define an array of SIZE = 7 and initialize them with 3,5,7,9,11,13,15 Call function sum with the array and size, both passed in pointer representations Print each element of the array Print the sum In function sum: Take the parameters in pointer representations, Use for-loop to calculate sum 9.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 size-1 i=1 (f_i + f_i+1)(x_i+1 - x_i) 10.Define two arrays x and f, each of size 10, using call-by-reference (that is, use pointers), 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 size-1 i=1 (f_i + f_i+1)(x_i+1 -x_i)Explanation / Answer
Question 8
#include <stdio.h>
int sum (int* l,int n){
int res = 0;
for (int i = 0; i < n; i++)
res += l[i];
return res;
}
int main(){
int l[] = {3,5,7,9,11,13,15};
printf("%d ",sum(l,7));
return 0;
}
Question 9
#include <stdio.h>
int sum (int x[],int f[],int n){
double sum = 0.0;
for (int i = 0; i < n-1; i++)
sum += (f[i] + f[i+1])*(x[i+1] - x[i]);
return 0.5*sum;
}
int main(){
int x[] = {1,2,3,4,5,6,7,8,9,10};
int f[] = {1,2,3,4,5,6,7,8,9,10};
printf("%d ",sum(x,f,10));
return 0;
}
Question 10
#include <stdio.h>
int sum (int* x,int* f,int n){
double sum = 0.0;
for (int i = 0; i < n-1; i++)
sum += (f[i] + f[i+1])*(x[i+1] - x[i]);
return 0.5*sum;
}
int main(){
int x[] = {1,2,3,4,5,6,7,8,9,10};
int f[] = {1,2,3,4,5,6,7,8,9,10};
printf("%d ",sum(x,f,10));
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.