223k views
3 votes
Write the definition of a function absoluteValue, that receives an integer parameter and returns the absolute value of the parameter's value. (You should write the logic for absolute value yourself... do not use the abs function of the C library.) So, if the parameter's value is 7 or 803 or 141 the function returns 7, 803 or 141 respectively. But if the parameter's value is -22 or -57, the function returns 22 or 57 (same magnitude but a positive instead of a negative). And if the parameter's value is 0, the function returns 0.

User Jjohn
by
4.6k points

1 Answer

1 vote

Answer:

int absoluteValue(int number) {

if (number < 0)

number = number * (-1);

else if (number > 0)

number = number;

else

number = 0;

return number;

}

Step-by-step explanation:

Create a function called absoluteValue that takes an integer parameter, number

Check if the number is smaller than 0. If it is, multiply the number with -1

Check if the number is greater than 0. If it is, assign it to itself

Check if the number is 0. If it is, assign it to 0

Return the number

User Alkasm
by
4.6k points