Once the result value is fixed to int, we want to take the mean of these arrays to decimal point, but even if we change it to (float) *result = sum_address/5; in the function call_address, there should be no error.
#include <stdio.h>
void call_value(int A[], int result)
{
int i;
int sum = 0;
for(i = 0; i < 5; i++)
{
sum += A[i];
}
result = sum / 5;
}
void call_address(int A[], int *result)
{
int i;
double sum_address = 0;
for(i = 0; i < 5; i++)
{
sum_address += A[i];
}
*result = sum_address / 5;
}
int main()
{
int result = 0;
int A[5] = {95, 88, 76, 54, 85};
call_value(A,result);
printf("[Call by value] result = %d\n",result);
call_address(A,&result);
printf("[Call by address] result = %.1f\n",(float)result);
}
We expected the average of the arrays to be 79.6. But only 79.0. Because this is a task, the int result = 0 and int *result should not change.