Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Exercise 1 - Function Templating For this part of the lab make a template out of the myMax function and test it on different data

image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
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 Start with the repl code provided to you. 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, and string types. When you are done your output should resemble this: The max of ) and 5 is 5 The max of 5.6 and 7.3 is 7.3 The max of donkey and apple is donkey Exercise 2 - Class Templating For this part of the lab you will make a template out of the Matrix class, test it on different data types, and add a generic sort member function. Part 1: Setup Start with the repl code provided to you. Add the template types to matrix.cpp Template the definition of the Matrix class in matrix.h. Template the member functions in matrix.cpp. The Makefile is set up for you. Use it to compile and run the program to make sure everything is working. Part 2: Extend Add two more 2D arrays to tempMain.cpp that hold a new data type. Create an instances of Matrix that can hold that data type and use the templated demo Template function to show that your templated class works Test your changes. Your completed exercise should resemble this: Demonstrating with string matrix: Matrix set to first array don ta Matrix incremented by second array Congratulations you are the bemonstrating with int matrix Matrix set to first array 1 2 3 ar Congray lain almost done Lab 17 7 7 batrix incremented by second array 7 7 7 Demonstrating with float matrixt Matrix set to first array 1.6 2.5 3.4 4.3 5.2 6.1 Matrix incremented by second array 7.7 7.7 7.7 7.7 7.7 7.7 const int MAXROWS=2; const int MAXCOLS=3; class Matrix { private: int doubleArray[MAXROWS] [MAXCOLS]; int rows; int cols; public: //Constructor Matrix(); //Set rows to MAXROWS and cols to MAXCOLS //Initialize all the values of doubleArray to zero //Getter Functions void printMatrix(); // Setter Functions void setMatrix (int [] [MAXCOLS]); //set the doubleArray to what is sent void addMatrix(int [][MAXCOLS]); //add an array to doubleArray void addMatrix(Matrix other Matrix); l/No destructor needed // Filename: maxTemplate.cpp // Purpose: Demonstrate the use of function templates. #include #include using namespace std; l/Make a template out of the prototype int myMax (int one, int two); int main() { int i_one = 3, i_two = 5; cout using namespace std; Matrix::Matrix() { rows = MAXROWS; cols = MAXCOLS; for (int i=0; i #include #include "matrix.h" using namespace std; template void demoTemplate(Matrix& M, T1 array1[][MAXCOLS], T1 array( [MAXCOLS]); int main() { string str1[MAXROWS] [MAXCOLS] = { {"Congra", "y", "ar"}, {"alm", "don", "La"} string str2[MAXROWS][MAXCOLS] = { {"tulations", "ou", "e"}, {"ost", "e the", "b!"} }; int nums1[MAXROWS][MAXCOLS] = { {1,2,3}, {4,5,6} int nums2[MAXROWS][MAXCOLS] = { {6,5,4}, {3,2,1} }; Matrix stringMatrix; Matrix intMatrix; cout stringMatrix; Matrix intMatrix; cout void demoTemplate(Matrix&M, T1 array10[MAXCOLS]. T1 array2[] [MAXCOLS]) { M.setMatrix(array1); cout

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Murach's SQL Server 2012 For Developers

Authors: Bryan Syverson, Joel Murach, Mike Murach

1st Edition

1890774693, 9781890774691

More Books

Students also viewed these Databases questions

Question

What is the purpose of the Salary Structure Table?

Answered: 1 week ago

Question

What is the scope and use of a Job Family Table?

Answered: 1 week ago