3. (40 pts) Define an array f= [23,-12,34,-56,-10, 12, 19], using call-by-refere
ID: 3349115 • Letter: 3
Question
3. (40 pts) Define an array f= [23,-12,34,-56,-10, 12, 19], using call-by-reference (that is, use of pointers), to pass the array to a function, named Summation. In main: define array, pass array, print out the array and the result returned from Summation on screen In function Summation: 1) take array and size from main 2) sum each element according to the following rules 3) if the value of f(i) is positive, add 2*f(i) to the sum 4) if the value of fi) is negative, add-3* f(i) to the sum 5) return the final value of sum to main NOTE: you must use call-by-reference (the pointer) to call functions, as required; otherwise, no credits will be given.Explanation / Answer
C script:
/******************************************************************************
Online C Compiler.
Code, Compile, Run and Debug C program online.
Write your code in this editor and press "Run" button to compile and execute it.
*******************************************************************************/
#include <stdio.h>
int main()
{
int f[] = {23,-12,34,-56,-10,12,19};//defining array
int sum;
sum = Summation(&f,7);//passing an array
for (i=1;i<=7;i++){
printf("%d",f[i]);//printing array
}
printf("%d",sum);//printing the return value sum
return 0;
}
int Summation(int *f,int size){
int i;int sum=0;int ele;
for (i=1;i<=size;i++){
ele = *f;
if (ele>0)
sum = sum+2*ele;
else
sum = sum-3*ele;
f++;
}
return sum;
}
output:
410
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.