The following function compares integers of type int and gives as a result the smaller Number: int min (int a, int b) { if (a < b) return a; else return b; } Create a template and Convert this function to a template. Before converting this function to a template, display how you would handle var a & b if they held the same value. In other words if a and b are equal, what would you do (how would this be coded)?
The following function could be coded as :
int min (int a, int b)
{
IF (a<b):
THEN return a
ELSE IF (b<a):
THEN return b
ELSE:
return a
}
This function could be called with two variables (a & b) from main function. This could either be integers of float numbers. We have shown for integers. Hence, the return type is also given as 'int'. We can consider this as a pseudocode (not specific to any programming language, rather generic for understanding purpose).
Here, there are three conditions as given in IF-THEN-ELSE format - 1) a is less than b, in this case we return a, 2) b is less than a, in this case, we return b, 3) a and b are equal, in this case, we can retrun either of a or b, doesn't matter since both are equal.
Get Answers For Free
Most questions answered within 1 hours.