Code should be in C++Absolute Value Template: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. Also pls do not hard code the values! Give the user the option to properly enter the value as described, thank you!

Question
Asked Jul 27, 2019
29 views

Code should be in C++

Absolute Value Template:

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. 

Also pls do not hard code the values! Give the user the option to properly enter the value as described, thank you!

check_circle

Expert Answer

Step 1

Since Function Templates are used to handle all possible data types you can say generic data type at once. So you can use the Templates with keyword template and typename. Following is the function template which is coded for finding out the absolute value of a given number:

template<typename Temp>

Temp Absolutevalue(Temp tNumber)

{

            return (tNumber>0)? tNumber:-tNumber;

}

Step 2

To test the templates, let’s declare the main method. In the main method, you can input the value which is accepted by the Absolutevalue function template and it returns the absolute value of this value as following:

int main( void)

{

            double number;

    cout<<"enter the number"<<endl;

    cin>>number;

            cout<<"Absolute value  of  "      <<number<<"  = " <<Absolutevalue(number)<<endl;

Step 3

Hence the code for finding out the Absolute Value using Function Template is as following:

#include <iostream>

 

using namespace std;

 

template<typename Temp>

Temp Absolutevalue(Temp tNumber)

{

            return (tNumber>0)? tNumber:-tNumber;

}

 

int main( void)

{

    double number;

    cout<<"enter the number"<<endl;

  &...

Want to see the full answer?

See Solution

Check out a sample Q&A here.

Want to see this answer and more?

Solutions are written by subject experts who are available 24/7. Questions are typically answered within 1 hour.*

See Solution
*Response times may vary by subject and question.
Tagged in

Engineering

Computer Science

Related Computer Science Q&A

Find answers to questions asked by student like you
Show more Q&A
add
question_answer

Q: Why does Pinterest view Google as its primary competitor? Why does Pinterest focus on the smartphone...

A: Pinterest is a social media platform whose software system is designed to find and curate informatio...

question_answer

Q: I need help with the SQL query for the following question: 1.  Get the total number of reviews of mo...

A: The details about the table are given with table name and different entities. Here the table name is...

question_answer

Q: A pet shop wants to give a discount to its clients if they buy one or more pets and at least four ot...

A: The program has two main parts – the part which receives the input and the part which validates the ...

question_answer

Q: How do you convert 172.16.20.55 to binary format?

A: 172.16.20.55 represents IP address in decimal format. In order to convert it into binary format we n...

question_answer

Q: Task_1: Base-10 conversion of the following Base-2 values? 101101   2. 011101   3. 1001   4. 11111...

A: Conversion of Base-2 to Base-10: 1. 101101:

question_answer

Q: Convert the nfa defined by δ(q0,a)={q0,q1}δ(q1,b)={q1,q2}δ(q2,a)={q2}

A: The state table and the NFA of the given conditions are given below. The initial state is q0 and the...

question_answer

Q: Function Name: compliments Parameters: answer1 - a boolean (True or False) representing whether the ...

A: Solution:The following function displays the outputs as string of compliments.

question_answer

Q: Hello, please I need help with the following using Python:   Write flowchart, pseudo-code and progra...

A: Pseudocode:Import module statistics and Declare variables Students and Student_listPrompt for the nu...

question_answer

Q: Find all strings in L ((ab + b)∗ b (a + ab)∗) of length less than four.

A: Answer:The symbol “+”denoted as “OR” operator which means “a + b” means either “a” or “b”, so the la...