Question

In: Computer Science

Create a generic function max() in C++, to find maximum of 3 generic type arguments, then...

Create a generic function max() in C++, to find maximum of 3 generic type arguments, then test this function with char, int and float type.

Solutions

Expert Solution

Program code:

#include<iostream>
using namespace std;

template <typename V>              //declaring template V to create generic variables.

// Max function with 3 generic variables as arguments and generic variable as return type.

V Max(V x,V y,V z)                   
{
    V res;                                   //Creating res variable in generic datatype to store max value.
    res=z;
    if(x>y && x>z)                       //Conditions to find maximum value.
        res=x;
    else if(y>z && y>x)
        res=y;
    return res;
}

int main()
{
    cout << "\nMax(3,7,1) function using Integer data type :" << Max<int>(3,7,1) << endl;

    //Max function with integers as arguments.

    cout << "\nMax(3.0,6.9,6.8) function using Float data type :" << Max<float>(3.0,6.9,6.8) << endl;

    //Max function with float values as arguments.

    cout << "\nMax('g','e','z') function using char data type :" << Max<char>('g','e','z') << endl;

    //Max function with char values as arguments.

   return 0;
}

Output:


Related Solutions

You'll implement a completely generic version of an algorithm to find the maximum of an array....
You'll implement a completely generic version of an algorithm to find the maximum of an array. Unlike in the past, when our algorithm only worked for int[] or double[], this version will work on any Java objects that are comparable, specifically any Java object that implements the Comparable interface. Create a public class named Max with a single class method named max. max should accept an array of Objects that implement Comparable and return the maximum. If the array is...
In Linux (Ubuntu), write a script to check command arguments (3 arguments maximum). Display the argument...
In Linux (Ubuntu), write a script to check command arguments (3 arguments maximum). Display the argument one by one. If there is no argument provided, remind users about the mistake. If there is an easy way to use a loop to get all arguments, use it? a. Display the source code in an editor (#4-1) b. Execute your script in the terminal, and display the command and the result (#4-2)
Language C++ Thank You! -Write a function getMeASentence() that takes zero arguments, and its return type...
Language C++ Thank You! -Write a function getMeASentence() that takes zero arguments, and its return type is string. This function will return a randomly generated sentence that abides by certain rules. -The rules of this sentence are that there are 5 randomly generated lowercase letters, a space, a randomly generated inequality symbol, a space, and a randomly generated 1 digit number. -In this context, an inequality symbol is understood to mean the "<", ">", or "=" symbol. -Write a function...
Requirements: C++ Array/File Functions Write a function named arrayToFile. The function should accept 3 arguments: The...
Requirements: C++ Array/File Functions Write a function named arrayToFile. The function should accept 3 arguments: The name of the file, a pointer to an array, and the size of the array. The function should open the specified file in binary mode, write the contents of the array to file, and then close the file. Write another function named fileToArray. This function should accept 3 arguments: the name of the file, a pointer, to an int array, and the size of...
This code assigns the maximum of the values 3 and 5 to the int variable max...
This code assigns the maximum of the values 3 and 5 to the int variable max and outputs the result int max; // your code goes here This code prompts the user for a single character and prints "true" if the character is a letter and "false" if it is not a letter // your code goes here
In Python Create a function called ℎ?????. The function has as arguments a list called ??????...
In Python Create a function called ℎ?????. The function has as arguments a list called ?????? and a list call center. • List ?????? contains lists that represent points. o For example, if ?????? = [[4,2], [3,2], [6,1]], the list [4,2] represents the point with coordinate ? at 4 and y coordinate at 2, and so on for the other lists. Assume that all lists within points contain two numbers (that is, they have x, y coordinates). • List ??????...
Write a C program/code that prompts the user for a minimum min and a maximum max....
Write a C program/code that prompts the user for a minimum min and a maximum max. Then use these values to print the squares of all even numbers between the min and max variables. For example if the user enters 6 as the minimum and 200 as the maximum, the program/code should print the following. Enter limit on minimum square: 6 Enter limit on maximum square: 200 36 64 100 144 196
Make a python code. Write a function named max that accepts two integer values as arguments...
Make a python code. Write 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. Write the program as a loop that...
C++ Question Create two circular linked lists and find their maximum numbers. Merge the two circular...
C++ Question Create two circular linked lists and find their maximum numbers. Merge the two circular linked lists such that the maximum number of 2nd circular linked list immediately follows the maximum number of the 1st circular linked list. Input: 12 -> 28 -> 18 -> 25 -> 19-> NULL 5 -> 24 -> 12 -> 6 -> 15-> NULL Output: 28 -> 24-> 25 -> 15 -> 19 -> 15->5-> 18 -> 25 -> 19->NULL Note:- Code should work...
In a typical optimization problem (max/min problem), we want to find a relative maximum or relative...
In a typical optimization problem (max/min problem), we want to find a relative maximum or relative minimum of a function. Our process is to • find the derivative of the function, • set that derivative equal to zero, • and solve for x. Use complete sentences to explain why this process makes sense.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT