Question

For this part of the lab make a template out of the myMax function and test...

  1. For this part of the lab make a template out of the myMax function and test it on different data types.

myMaxTemplate.cpp :

/****************************************************
*
* FileName: maxTemplate.cpp
* Purpose: Demonstrate the use of function templates
*
********************************************************/
#include <iostream>
#include <string>
using namespace std;

//Make a template out of the prototype
int myMax(int one, int two);

int main()
{
int i_one = 3, i_two = 5;

cout << "The max of " << i_one << " and " << i_two << " is "
   << myMax(i_one, i_two) << endl;

//Test your template on float and string types
  
return 0;
}


//Make a template out of this function. Don't forget the return type.
int myMax(int one, int two)
{
int bigger;
if (one < two)
{
bigger = two;
}
else
{
bigger = one;
}
return bigger;
}

2. compile and run the program to see how it works.

3. make a template out of myMax. Don't forget the return type.

4.modify the prototype appropriately.

5. test your myMax template on int, double, and string types

When you are done your output should resemble this:

The max of 3 and 5 is 5

The max of 5.6 and 7.3 is 7.3

The max of donkey and apple is donkey

0 0
Add a comment Improve this question Transcribed image text
Answer #1
/****************************************************
*
* FileName: maxTemplate.cpp
* Purpose: Demonstrate the use of function templates
*
********************************************************/
#include <iostream>
#include <string>

using namespace std;

template <typename T>
T myMax(T one, T two);

int main() {
    int i_one = 3, i_two = 5;

    cout << "The max of " << i_one << " and " << i_two << " is "<< myMax(i_one, i_two) << endl;
    cout << "The max of " << 5.6 << " and " << 7.3 << " is "<< myMax(5.6, 7.3) << endl;
    cout << "The max of donkey and apple is "<< myMax("donkey", "apple") << endl;
    return 0;
}


template <typename T>
T myMax(T one, T two) {
    T bigger;
    if (one < two) {
        bigger = two;
    } else {
        bigger = one;
    }
    return bigger;
}

Add a comment
Know the answer?
Add Answer to:
For this part of the lab make a template out of the myMax function and test...
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
  • Need C++ coding for this lab exercise given below :) 4. Lab Exercise — Templates Exercise...

    Need C++ coding for this lab exercise given below :) 4. Lab Exercise — Templates Exercise 1 - Function Templating For this part of the lab make a template out of the myMax function and test it on different data types. Copy maxTemplate.cpp to your Hercules account space. Do that by: Entering the command:cp /net/data/ftp/pub/class/115/ftp/cpp/Templates/maxTemplate.cpp maxTemplate.cpp Compile and run the program to see how it works. Make a template out of myMax. Don't forget the return type. Modify the prototype appropriately. Test your myMax template on int, double,...

  • Code in C++ Function Prototypes For the second part of the lab activity, you will be...

    Code in C++ Function Prototypes For the second part of the lab activity, you will be practicing writing function prototypes. Continue working on the same project from part A. First, rewrite the code so that you are using function prototypes for the functions getNumber.getMessage, and repeat. Remember that the prototypes go at the top of the file, followed by main, then the definitions of the three functions at the end. Second, you will be creating a new function (procedure) called...

  • Submissions) Part A Type and run the Array class template discussed in lecture (Templates notes). Modify...

    Submissions) Part A Type and run the Array class template discussed in lecture (Templates notes). Modify the class by adding sort member function (refer to Algorithm Analysis notes for sorting algorithms) Sample usage: a. sort(); Add the needed code in main to test your function. template <class T> 1/you can use keyword typename instead of class class Array { private: T *ptr; int size; public: Array(T arr[], int s); void print(); template <class T> Array<T>:: Array (T arr[], int s)...

  • Transform the find function of Question 4 into a function template. Here is the program used...

    Transform the find function of Question 4 into a function template. Here is the program used to test your template, followed by the output of that program: #include <iostream> #include <string> #include "find.h" using namespace std; #define NUM_ELEMENTS(a) (sizeof(a) / sizeof(a[0])) int main() {         cout << "int" << endl;         cout << "---" << endl;         int arr1[] = {1, 2, 3, 4, 5, 6, 7, 8, 9};         cout << "3 is at location " << find(arr1, NUM_ELEMENTS(arr1),...

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

  • Extend the code from Lab6.A template is given to you with CircleHeader.h, Circle.cpp and CircleMain.cpp Use...

    Extend the code from Lab6.A template is given to you with CircleHeader.h, Circle.cpp and CircleMain.cpp Use the same Circle UML as below and make extensions as marked and as needed. Given below is a UML for the Painting Class. You will need to write PaintingHeader.h and Painting.cpp. The Painting Class UML contains a very basic skeleton. You will need to flesh out this UML and add more instance variables and methods as needed. You do NOT need to write a...

  • In c++ please How do I get my printVector function to actually print the vector out?...

    In c++ please How do I get my printVector function to actually print the vector out? I was able to fill the vector with the text file of names, but I can't get it to print it out. Please help #include <iostream> #include <string> #include <fstream> #include <algorithm> #include <vector> using namespace std; void openifile(string filename, ifstream &ifile){    ifile.open(filename.c_str(), ios::in);    if (!ifile){ cerr<<"Error opening input file " << filename << "... Exiting Program."<<endl; exit(1); }    } void...

  • Java Programming Lab 07 coding /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in th...

    Java Programming Lab 07 coding /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package lab07; /** * * @author ckandjimi */ public class Lab07 { /** * @param args the command line arguments */ public static void main(String[] args) { int outcome = methodX(5); System.out.printf("Result from Method X= %d",outcome); String [] studentNames = {"Kingston","John","Nangula","Daniela"}; int Test01[]...

  • 11.3 LAB: Convert String to Cyphertext cstring mplement the following (Called a Ceasar Cypher, since it...

    11.3 LAB: Convert String to Cyphertext cstring mplement the following (Called a Ceasar Cypher, since it was used in Ceasar's time) using this function prototype in the same file char cypher (string str, int rotate); The idea is that you will declare a variable of type string and give it a value in main. Then pass it into the cypher function. Cypher will create a cstring by copying the str to a new char* of size str.size). Remember that you...

  • PLEASE HELP ME WITH THIS HOMEWORK. Create a template function that, given an array of elements of any template type, deletes an element on a given position. Submit in the standard format in Dropbox Hw...

    PLEASE HELP ME WITH THIS HOMEWORK. Create a template function that, given an array of elements of any template type, deletes an element on a given position. Submit in the standard format in Dropbox Hw 4 before the deadline. Example for insertion (needs a fix!!! - 1st e-mail gets extra-credit) #include <iostream> using namespace std; // A template function to implement element insertion on given position in array. template <class T> void insert(T a[], int &n,T el, int place )...

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
ADVERTISEMENT