Question

Using C++, Java, or C# write a generic function/method that takes in 2 arguments of any...

Using C++, Java, or C# write a generic function/method that takes in 2 arguments of any numeric type, adds those 2 arguments together and returns the result.

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

using namespace std;

template <typename T>
T sum(T n1, T n2) {
    return n1 + n2;
}

int main() {
    cout << sum(2, 8) << endl;
    cout << sum(5.5, 9.2) << endl;
    return 0;
}

Add a comment
Know the answer?
Add Answer to:
Using C++, Java, or C# write a generic function/method that takes in 2 arguments of any...
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
  • Java Write a function/method called CheckHash: takes two arguments, an array and an integer count n....

    Java Write a function/method called CheckHash: takes two arguments, an array and an integer count n. The function computes the exclusive OR of the hashcodes (https://docs.oracle.com/javase/8/docs/api/java/lang/String.html#hashCode--) of the first n strings in the array (result type int).

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

  • Write a public static method named getMaxOf2Ints that takes in 2 int arguments and returns the...

    Write a public static method named getMaxOf2Ints that takes in 2 int arguments and returns the Maximum of the 2 values Write a public static method named getMinOf2Ints that takes in 2 int arguments and returns the Minimum of the 2 values Write apublic static method named getMaxOf3Ints that takes in 3 int arguments and returns the Maximum of the 3 values Write a public static method named getMedianOf3Ints that takes in 3 int arguments and returns the Median Value...

  • java Write a generic method that takes an ArrayList of objects (of a valid concrete object...

    java Write a generic method that takes an ArrayList of objects (of a valid concrete object type) and returns a new ArrayList containing no duplicate elements from the original ArrayList. The method signature is as follows: public static ArrayList UniqueObjects(ArrayList list) Test the method with at least 2 array lists of different concrete object types. Label your outputs properly and show the array lists content before and after calling method UniqueObjects

  • Write a generic method gReplace() that takes a 2D array of generic elements and two scalars...

    Write a generic method gReplace() that takes a 2D array of generic elements and two scalars of the same type as the array elements. The function must replace all occurrences of the first scalar with the second one. (scalar: given int x, x is a scalar of type int) with java

  • Create a function, myProd(), that takes two arguments, x and y, and multiplies the two arguments...

    Create a function, myProd(), that takes two arguments, x and y, and multiplies the two arguments together then adds 14 to the result. Finally, divide the sum by two and return as z I cannot come up with a solution for the above question, Could I get an explanation on how to approach and solve this problem. Thank you, I) Create a function, myProd), that takes two arguments, r and y, and multiplies the twoarguments together then adds 14 to...

  • Write a function with a variable number of arguments, that takes any number of integers as...

    Write a function with a variable number of arguments, that takes any number of integers as arguments and computes the average. Additionally write a main that calls this function at least twice on different numbers of arguments. Can this be done in command line and c++

  • Write a statement that declares a prototype for a function divide that takes four arguments and...

    Write a statement that declares a prototype for a function divide that takes four arguments and returns no value. The first two arguments are of type int. The last two arguments arguments are pointers to int that are set by the function to the quotient and remainder of dividing the first argument by the second argument. The function does not return a value. (C Program)

  • JAVA Question a) Write a generic method public static <...> Collection<Pair<...>> sortPairCollection(Collection <Pair<....>> col) in a...

    JAVA Question a) Write a generic method public static <...> Collection<Pair<...>> sortPairCollection(Collection <Pair<....>> col) in a Utils class that takes as parameter a collection of Pair<K,V> objects and returns a new collection object (use ArrayList<...>) with the pair elements from collection col sorted in ascending order. For comparing pairs, the K type must implement Comparable<....> Use the proper type constraints. b) Write a main() function in Utils.java that tests the sortPairCollection() with K=String and V=Integer.

  • JAVA Write a static method that takes an array of a generic type as its only...

    JAVA Write a static method that takes an array of a generic type as its only argument. The method should display the array and return the number of elements in the array. Test the method in main with at least three arrays of objects. SAMPLE OUTPUT Here is an Integer array 12 21 7 16 8 13 That array held 6 elements Here is a String array one two three four That array held 4 elements Here is a Double...

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