Please complete the following two short functions utilizing C++ format: 1. Write
ID: 3626242 • Letter: P
Question
Please complete the following two short functions utilizing C++ format:1. Write the definition of a function absoluteValue , which receives an integer parameter and returns the absolute value of the parameter's value.
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. Write the definition of a function signOf , which receives an integer parameter and returns a -1 if the parameter is negative, returns 0 if the parameter is 0 and returns 1 if the parameter is positive.
So if the parameter's value is 7 or 803 or 141 the function returns 1. But if the parameter's value is -22 or -57, the function returns -1. And if the parameter's value is 0, the function returns 0.
Explanation / Answer
int absoluteValue(int k)
{
if(k<0) return -1*k;
return k;
}
int signOf(int a)
{
if(a==0) return 0;
if(a>0) return 1;
return -1;
}
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.