Ask Question

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.

+2
Answers (1)
  1. 27 March, 15:19
    0
    int absoluteValue (int number) {

    if (number < 0)

    number = number * (-1);

    else if (number > 0)

    number = number;

    else

    number = 0;

    return number;

    }

    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
Know the Answer?
Not Sure About the Answer?
Find an answer to your question 👍 “Write the definition of a function absoluteValue, that receives an integer parameter and returns the absolute value of the parameter's ...” in 📗 Computers & Technology if the answers seem to be not correct or there’s no answer. Try a smart search to find answers to similar questions.
Search for Other Answers