Question

1. Create a function template that takes a number as an argument and returns the absolute...

1. Create a function template that takes a number as an argument and returns the absolute value. The absolute value of a number is the number without the sign, such that the absolute value of -5 is 5 and the absolute value of +5.0 is 5.0. Test your function with at least two numeric data types.

c++

0 0
Add a comment Improve this question Transcribed image text
Answer #1
#include <iostream>

using namespace std;

template<class T>
T absolute(T data) {
    if (data < 0)
        return -1 * data;
    else
        return data;
}

int main() {
    // testing with double
    cout << absolute(2.9) << endl;
    cout << absolute(-5.6) << endl;

    // testing with int
    cout << absolute(-9) << endl;
    cout << absolute(11) << endl;
    return 0;
}

phpmgiM3r.png

Add a comment
Know the answer?
Add Answer to:
1. Create a function template that takes a number as an argument and returns the absolute...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • using C++ in beginner form Create a function named absoluteValue that takes in a number and...

    using C++ in beginner form Create a function named absoluteValue that takes in a number and returns the absolute value of it (e.g. if it takes in 5 or -5 it will return 5)

  • Write a function that takes an int as an argument and returns true if the int...

    Write a function that takes an int as an argument and returns true if the int is an even number and false if not. In any case change the value of the original argument to twice its value. Use the integers 0 through 4 as test cases. Do not use any arrays.

  • Write a function count_vowels(s) that takes a string as an argument and returns the number of...

    Write a function count_vowels(s) that takes a string as an argument and returns the number of vowels ('a', 'e', 'i' 'o', 'u') in the string. Should you use a for or while loop? (Implement this as a function, not as a class with a method.) Be sure to include unittest test cases to demonstrate that your code works properly, e.g Part 2: last_occurance(target, sequence) Write a function last_occurance(target, sequence) that takes two arguments: 1. target: A target item to find...

  • Q.1. Create a C++ class template called ArrayCalc, to perform a series of mathematical operations on...

    Q.1. Create a C++ class template called ArrayCalc, to perform a series of mathematical operations on an array. These mathematical operations are calculating the minimum, maximum, sum and average values of the array. The ArrayCalc class template accepts one type parameter, which can be defined as: template <class T>. This allows ArrayCalc to carry out operations for different array data types. ArrayCalc has the following public methods, described here in pseudo code: i. void FindMin(inArrayData[], inArraySize, SoutMinVal); (This function accepts...

  • With using Python . Write a function that: 1. Takes two arguments and returns the product...

    With using Python . Write a function that: 1. Takes two arguments and returns the product of the arguments. The return value should be of type integer. 2. Takes two arguments and returns the quotient of the arguments. The return value should be of type float. 3. Takes one argument and squares the argument. It then uses the two values and returns the product of the two arguments.(Reuse function in 1.) 4. Takes one argument and prints the argument. Use...

  • (1) implement a function, which takes as its argument a DFA M, and returns a DFA...

    (1) implement a function, which takes as its argument a DFA M, and returns a DFA M ' equivalent to M but without useless states (i.'e., unreachable from the initial state q0) if there is. The header of the function should be: dfaType* removeUseless(dfaType* M); from (1) implement a function that returns minimum states equivalent to M. The header of the function should be: dfaType* minimizeDfa(dfaType* M);

  • Write a function called ''minandmax'' . Function ''minandmax'' takes only one input argument that is a...

    Write a function called ''minandmax'' . Function ''minandmax'' takes only one input argument that is a matrix M . This matrix M can be any size (any number of columns and rows) with any random integer values . Function ''minandmax'' returns two output arguments. The first output argument is called ''difference_row'' that returns a row vector containing the absolute values of the difference between the maximum and minimum valued elements in each row. The second output argument is called ''difference_all''...

  • Thonny Language Write a function called 'numToPercentage' which accepts a single argument n which will be a decimal number and returns the number formatted as a percentage E.g numToPercentage(...

    Thonny Language Write a function called 'numToPercentage' which accepts a single argument n which will be a decimal number and returns the number formatted as a percentage E.g numToPercentage(0.2) returns 20%. Percentages are to be whole numbers only E.g. numToPercentage (0.052) returns 5%. What is the output of your function for following, Enter the value with the % sign such as 5% A. numT○Percentage(0.074) = B. numT○Percentage(1.2) = Write a function called 'numToPercentage' which accepts a single argument n which...

  • 1. Write a function called ordinal_sum that accepts a string argument and returns the sum of...

    1. Write a function called ordinal_sum that accepts a string argument and returns the sum of the ordinal values of each character in the string. The ordinal value of a character is its numeric Unicode code point in decimal. 2. Write code that creates a Python set containing each unique character in a string named my_string. For example, if the string is 'hello', the set would be {'h', 'e', 'l', 'o'} (in any order). Assign the set to a variable...

  • Example 1: Define a function that takes an argument. Call the function. Identify what code is...

    Example 1: Define a function that takes an argument. Call the function. Identify what code is the argument and what code is the parameter. Example 2: Call your function from Example 1 three times with different kinds of arguments: a value, a variable, and an expression. Identify which kind of argument is which. Example 3: Create a function with a local variable. Show what happens when you try to use that variable outside the function. Explain the results. Example 4:...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT