Logo 
Search:

C++ Programming Articles

Submit Article
Home » Articles » C++ Programming » Homework HelpRSS Feeds

Program that defines template to find minimum value in an array

Posted By: Easy Tutor     Category: C++ Programming     Views: 12693

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

Code for Program that defines template to find minimum value in an array in C++ Programming

#include <iostream.h>
#include <conio.h>

template <class T>
T findMin(T arr[],int n)
{
    int i;
    T min;
    min=arr[0];
    for(i=0;i<n;i++)
    {
         if(min > arr[i])
        min=arr[i];
    }
    return(min);
}


void main()
{
    clrscr();
    int iarr[5];
    char carr[5];
    double darr[5];

    cout << "Integer Values \n";
    for(int i=0; i < 5; i++)
    {
        cout << "Enter integer value "  << i+1 << " : ";
        cin >> iarr[i];
    }

    cout << "Character values \n";
    for(int j=0; j < 5; j++)
    {
        cout << "Enter character value " << j+1 << " : ";
        cin >> carr[j];
    }


    cout << "Decimal values \n";
    for(int k=0; k < 5; k++)
    {
        cout << "Enter decimal value " << k+1 << " : ";
        cin >> darr[k];
    }
    //calling Generic function...to find minimum value.
    cout<<"Generic Function to find Minimum from Array\n\n";
    cout<<"Integer Minimum is : "<<findMin(iarr,5)<<"\n";
    cout<<"Character Minimum is : "<<findMin(carr,5)<<"\n";
    cout<<"Double Minimum is : "<<findMin(darr,5)<<"\n";

    getch();
}
  
Share: 



Easy Tutor
Easy Tutor author of Program that defines template to find minimum value in an array is from United States. Easy Tutor says

Hello Friends,

I am Free Lance Tutor, who helped student in completing their homework.

I have 4 Years of hands on experience on helping student in completing their homework. I also guide them in doing their final year projects.

I have share many programs on this website for everyone to use freely, if you need further assistance, than please contact me on easytutor.2ya [at the rate] gmail [dot] com

I have special discount scheme for providing tutor services. I am providing tutor service to students from various contries, currently most of my students are from United States, India, Australia, Pakistan, Germany, UK and Canada.

I am also here to expand my technical network to receive more opportunity in my career, make friends to help them in resolving their technical problem, learn and share my knowledge, If you like to be my friend, Please send me friend request.

Thanks,
Happy Programming :)

 
View All Articles

 
Please enter your Comment

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

 
Jeremy Brown from United States Comment on: Sep 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;
}
]

View All Comments