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!

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;

&...

Tagged in

Find answers to questions asked by student like you

Show more Q&A

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...

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...

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 ...

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...

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:

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...

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.

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...

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...