Logo 
Search:

C++ Programming Article

Submit Article
Comments on Program that defines template to find minimum value in an array
Category: C++ Programming (Homework Help)    Author: Easy Tutor

Write a program that finds minimum integer, smallest character and minimum decimal from an array using template class.


Jeremy  Brown
Jeremy Brown from United StatesSep 17
here is a better way of doing this because your code would not compile


[
#include <iostream>
#include <array>
#include <string>
using namespace std;
template<typename T>
T maxValue(T list[],const int& size)
{
T currentMax = list[0];

for (int i = 0; i < size; i++)
{

if(currentMax < list[i])
{
currentMax = list[i];
}
}
return currentMax;
}

int main()
{
int intArray[] =
{
1, 2, 4, 2, 5, 3, 1
};
cout << "Max int value is " << maxValue(intArray, 7) << endl;

double doubleArray[] =
{
1.4, 2, 0.4, 2, 0.5, 3, 1
};
cout << "Max double is " << maxValue(doubleArray, 7) << endl;

string strings[] =
{
"abc", "cbs", "nbc"
};

cout << "Max string is " << maxValue(strings, 3) << endl;

return 0;
}
]


Please enter your Comment
  • Comment should be atleast 15 Characters.
  • Please put code inside [Code] your code [/Code].