C++ format in notepad or codeblocks Write a function Absolute Value (x) that ret
ID: 3800437 • Letter: C
Question
C++ format in notepad or codeblocks
Write a function Absolute Value (x) that returns the absolute value of x. (The absolute value is the magnitude of x with no sign. Example: the absolute value of -5 is 5 and the absolute value of 7.38 is 7.38). The function and its argument should both have a type float. Write a small program to demonstrate your function Write your own function. Do NOT #include or . Do not use the standard library function abs (). All input and output should be in main () not in the function.Explanation / Answer
#include <iostream>
using namespace std;
float AbsoluteValue(float x){
if(x < 0){
x = -x;
}
return x;
}
int main()
{
float x;
cout << "Enter the value: ";
cin >> x;
float result = AbsoluteValue(x);
cout<<"Absolute value is "<<result<<endl;
return 0;
}
Output:
sh-4.2$ g++ -o main *.cpp
sh-4.2$ main
Enter the value: -5
Absolute value is 5
sh-4.2$ g++ -o main *.cpp
sh-4.2$ main
Enter the value: 7.38
Absolute value is 7.38
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.