I was wondering if what I have done is correct or not. Here's what my book says to do:

"Write a function template that accepts an argument and returns its absolute value. The absolute value of a number is its value with no sign. For example, the absolute value of -5 is 5, and the absolute value of 2 is 2. Test the template in a simple driver program."

This is the code that I've done:

// This program displays the absolute value of an entered number.
#include <iostream>
#include <cmath>
#include <conio>
using namespace std;

template <class T>
T abs(T val1)
{
	return val1;
}


int main()
{
        double num1;

	cout << "Enter a number, and I will display the absolute value of that number: ";
	cin >> num1;
	cout << "The absolute value of the enterend number is: " << abs(num1) << endl;

	getch();
	return 0;
}

what about the following code.

template<class T> 
T const& Absolute(T const& v)
{
	if (v < 0)
	{
		return (v + (-v*2));
	}
	return v;
}

requires minute mathematics.

Absolute value also gets off the decimal places from the number. For eg: abs(3.42) returns 3

the builtin abs is integer only function.

Yeah, so basically if you put a number that is a decimal, then it'll truncate the rest after the decimal place, therefore making it an integer. Correct?

Yes! correct.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.