URGENT PLEASE check if my code is right and correct it if it isnt #include <stdi
ID: 3716356 • Letter: U
Question
URGENT PLEASE check if my code is right and correct it if it isnt
#include <stdio.h>
int sum(int *x,int *f)
{
int i, fSum, sum1=0, sum2=0, size=10;
for(i=0;i<size;i++)
{
if (i%2==0)
sum1 += (2*f[i])-(3*x[i]);
else
sum2 += (6*f[i+1])+x[i];
}
fSum=(.5*sum1)+(.6*sum2);
return fSum;
}
int main()
{
int size=10;
int x[10] = {2,1,5,-3,6,7,8,10,3,9};
int *xptr=x;
int f[10] = {2,5,7,3,5,-8,7,4,1,9};
int *fptr=f;
int i;
printf("Array x: ");
for(i=0;i<size;i++)
printf("%d ",x[i]);
printf(" ");
printf("Array f: ");
for(i=0;i<size;i++)
printf("%d ",f[i]);
printf(" ");
printf("Sum is: %d ",sum(x,f));
return 0;
}
This is the question
Define two arrays x={2,1,5,-3,6,7,8,10,3,9} and f={2,5,7,3,5,-8,7,4,1,9} each size 10 using call by reference(pointers) to pass the array to a function named sum.
In main define array, print out array, pass array to the function and print out the returned value on the screen. In funtion sum take arrays from main and sum the arrays using
Explanation / Answer
There is a small mistake
:sum2 += (6*f[i+1])+x[i];
This line should be
:sum2 += (6*f[i])+x[i];
You are already checking for even and odd, there is no need of writing i+1
The new output is:
Sum is:47
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.