Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

In C++ Using Templates Convert the given code to use templates. The struct type

ID: 3686935 • Letter: I

Question

In C++

Using Templates
Convert the given code to use templates.
The struct type and all functions that use it need to be altered so that they will work with
a variable data type sent from main.
The main program contains commented and uncommented code. The uncommented part
needs to be altered to work with the template, but no new code logic is required. The
commented parts need to be uncommented and should work once you have successfully
added the templates.

Code:

#include
using namespace std;

const int ARRSIZE = 10;

struct Array
{
   int data[ARRSIZE];
   int datasize;
};

int findMax( Array );
void output( Array );
void addValue( Array &, int );

int main()
{
   Array one;
   one.datasize = 5;
   for(int i=0; i        one.data[i] = i+1;
   addValue(one, -1);
   cout << endl << "Integers" << endl;
   output(one);
   cout << "max value: " << findMax(one) << endl;  
/*
   Array two;
   two.datasize = 7;
   for(int j=0; j        two.data[j] = (j+1) * 1.1;
   addValue( two, -2.7);
   cout << endl << "Doubles" << endl;
   output(two);
   cout << "max value: " << findMax(two) << endl;

   Array three;
   three.datasize = 9;
   for(int k=0; k        three.data[k] = k + 'a';
   addValue(three, 'C');
   cout << endl << "Chars" << endl;
   output(three);
   cout << "max value: " << findMax(three) << endl << endl;
*/
   return 0;
}

int findMax( Array rec)
{
   int max = rec.data[0];
   for(int i=0; i        if( rec.data[i] > max )
           max = rec.data[i];
   return max;
}

void output( Array rec)
{
   for(int i=0; i        cout << rec.data[i] << " ";
   cout << endl;
}

void addValue( Array & rec, int value )
{
   rec.data[rec.datasize] = value;
   rec.datasize++;
}

Explanation / Answer

#include <iostream>
using namespace std;

const int ARRSIZE = 10;
template <typename T>
struct Array
{
T data[ARRSIZE];
T datasize;
};

template <typename T>
T findMax( Array rec)
{
T max = rec.data[0];
for(int i=0; i<10;i++) if( rec.data[i] > max )
max = rec.data[i];
return max;
}

template <typename T>
void output( Array rec)
{
for(int i=0; i<10;i++)
cout << rec.data[i] << " ";
cout << endl;
}
template <typename T>
void addValue( Array & rec, T value )
{
rec.data[rec.datasize] = value;
rec.datasize++;
}
int main()
{
Array one;
one.datasize = 5;
for(int i=0; i<10;i++) one.data[i] = i+1;
addValue(one, -1);
cout << endl << "numbers" << endl;
output(one);
cout << "max value: " << findMax(one) << endl;
/*
Array two;
two.datasize = 7;
for(int j=0; j two.data[j] = (j+1) * 1.1;
addValue( two, -2.7);
cout << endl << "Doubles" << endl;
output(two);
cout << "max value: " << findMax(two) << endl;
Array three;
three.datasize = 9;
for(int k=0; k three.data[k] = k + 'a';
addValue(three, 'C');
cout << endl << "Chars" << endl;
output(three);
cout << "max value: " << findMax(three) << endl << endl;
*/
return 0;
}