Question

Write templates for the two functions minimum and maximum. The minimum function should accept two arguments...


Write templates for the two functions minimum and maximum. The minimum function should accept two arguments and return the value of the argument that is the lesser of the two. The maximum function should accept two arguments and return the value of the argument that is the greater of the two.

Test your functions in a main program that propmts the user to choose what type of data they would like to compare (ints, doubles, or strings). Then, it should ask the user to enter two arguments, as well as whether they would like to know the minimum or the maximum. Then, it should print the minimum or maximum of the two arguments.

in C++

0 0
Add a comment Improve this question Transcribed image text
Answer #1
//main.cpp
#include <iostream>
using namespace std;

template <typename T>
T maximum(T val1, T val2)
{
   if(val1>val2){
          return val1;
   }
   return val2;
}

template <typename T>
T minimum(T val1, T val2)
{
   if(val1>val2){
          return val2;
   }
   return val1;
}

int main(){
   cout<<"maximum('a','b') = "<<maximum('a','b')<<endl;
   cout<<"maximum(1,2) = "<<maximum(1,2)<<endl;
   cout<<"maximum(2.1,1.2) = "<<maximum(2.1,1.2)<<endl;
   cout<<"minimum('a','b') = "<<minimum('a','b')<<endl;
   cout<<"minimum(1,2) = "<<minimum(1,2)<<endl;
   cout<<"minimum(2.1,1.2) = "<<minimum(2.1,1.2)<<endl;
   return 0;
}

Output:

maximum (a, b) = b maximum (1,2) = 2 maximum (2.1,1.2) = 2.1 minimum (a, b) = a minimum (1,2) = 1 minimum (2.1,1.2) =

Add a comment
Know the answer?
Add Answer to:
Write templates for the two functions minimum and maximum. The minimum function should accept two arguments...
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
  • Command line input In C++ it is possible to accept command line arguments. Command-line arguments are...

    Command line input In C++ it is possible to accept command line arguments. Command-line arguments are given after the name of a program in command-line operating systems like Linux and are passed in to the program from the operating system. To use command line arguments in the program, it must first understand the full declaration of the main function, which until now has accepted no arguments. In fact, main can accept two arguments: one argument is number of command line...

  • Design a function named max that accepts two integer values as arguments and returns the value...

    Design a function named max that accepts two integer values as arguments and returns the value that is the greater of the two. For example, if 7 and 12 are passed as arguments to the function, the function should return 12. Use the function in a program that prompts the user to enter two integer values. The program should display the value that is the greater of the two. Need Variable lists, Psuedocode, ipo chart, Flow Chart, and a python...

  • Write a program that asks the user to enter five test scores. The program should display...

    Write a program that asks the user to enter five test scores. The program should display a letter grade for each score and the average test score. Design the following functions in the program: calcAverage—This function should accept five test scores as arguments and return the average of the scores. determineGrade—This function should accept a test score as an argument and return a letter grade for the score (as a String), based on the following grading scale: Round the average...

  • Using C Write a function that takes two ints and returns the minimum (smallest) of those...

    Using C Write a function that takes two ints and returns the minimum (smallest) of those two ints. Include a function prototype. In main, call the function with two int inputs of your choice and print the returned value to the output window. The function should not print, just return the smallest number (int). If the numbers are the same, the minimum is just the number. Please use notes to explain, thanks!

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

  • 10. replaceSubstring Function Write a function named replaceSubstring. The function should accept three C-string or string...

    10. replaceSubstring Function Write a function named replaceSubstring. The function should accept three C-string or string object arguments. Let's call them string1, string2, and string3. It should search string for all occurrences of string2. When it finds an occurrence of Programming Challenges string2, it should replace it with string. For example, suppose the three arguments have the following values: stringt: "the dog jumped over the fence" string 2 "the" string3: "that" With these three arguments, the function would return a...

  • Template Exercise (50 pts) Modified FeetInches Specification file (30pts) – FeetInches.h Driver program with function template...

    Template Exercise (50 pts) Modified FeetInches Specification file (30pts) – FeetInches.h Driver program with function template (20 pts) – TempDriver.cpp Template Exercise Write template for two functions called minimum and maximum. Each function should accept two arguments and return the lesser or greater of the two values. Test these templates in a driver program. The template with the following types: int, double, string and FeetInches (which is an object). You will need: The FeetInches class (which was provided in Week...

  • Write a class named TestScores. The class constructor should accept an array of test scores as...

    Write a class named TestScores. The class constructor should accept an array of test scores as its argument. The class should have a method that returns the average of the test scores. If any test score in the array is negative or greater than 100, the class should throw an IllegalArgumentException. Demonstrate the class in a program (create a Driver class in the same file). The program should ask the user to input the number of test scores to be...

  • Document2 Tell me Layout References Mailings Review View 1. Write a program named Lab17B that will...

    Document2 Tell me Layout References Mailings Review View 1. Write a program named Lab17B that will read 2 strings and compare them. a. Create a bool function named compareLetters that will accept 2 string variables as parameters and will return true if the strings have the same first letters and the same last letters. It will return a false otherwise. b. Create a bool function named inside that will accept the 2 string variables in the order they were entered)...

  • write a function that use to change the char in an array. The function should accept...

    write a function that use to change the char in an array. The function should accept three arrays: main array(the array has value that needs to be change); replace array(the array that store the array of charaters that need to be replace.); with array(the array store the new value that replace the old one); The function should look like this: Change(char array[], char rep[], char with[]). example: Change("abc", "afd","gdh") . Than the array should become "gbc". (The last two arguments...

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