#include <stdio.h>
int middle(int a, int b)
{
int x=(a+b)/2;
return x;}
int main() {
printf("%d\n", middle(2,3));
return 0;
}
This code defines a function `middle` that calculates the average of two integers by adding them and dividing by 2. The `main` function calls it with 2 and 3 and prints the result. The output will be 2, not 2.5, because integer division truncates the decimal part.
- The function `middle` uses integer division, which discards any fractional part. For odd sums like 2+3=5, dividing by 2 gives 2 instead of 2.5. Consider how to handle cases where the result should be a non-integer.
- The function returns an `int`, so it cannot represent fractional values. Think about what return type would allow storing a decimal result.