This question is in C++ format and relevant
(a) What is the purpose of the f1 function
below?
int f1( int data[], int n )
{
int a = 0;
for(int i = 1; i < n; i++)
if(data[i] == 0)
a++;
return a;
}
(b) In the signature of the f1 function above, state whether each of the parameters is pass-by-value or pass-by-reference?
(c) Complete the definition of the function f2
below such that when invoked, it will take the addresses of two
variables through the pointer parameters a and b shown in the
function signature below and double their
values?
void f2( int *a, int *b)
(a) What is the purpose of the f1 function below?
int f1( int data[], int n )
{
int a = 0;
for(int i = 1; i < n; i++)
if(data[i] == 0)
a++;
return a;
}
Answer: the function takes two parameters one is array data and integer length of the array
it counts the number of zeros in the array data and returns the number of zeros.
(b) In the signature of the f1 function above, state whether each of the parameters is pass-by-value or pass-by-reference?
Answer: first parameter data as array type
pass-by-reference
second parameter n as int type pass-by-value
(c) Complete the definition of the function f2 below such that when invoked, it will take the addresses of two variables through the pointer parameters a and b shown in the function signature below and double their values?
void f2( int *a, int *b)
Answer:
void f2( int *a, int *b)
{
*a=(*a)*2;
*b=(*b)*2;
}
Get Answers For Free
Most questions answered within 1 hours.