Written by Sai Sandeep Thota on April 4, 2013 in C++ > Programming

C++ Program to Find Minimum Element in an Array using Templates

GET ALERTS:

Get our Latest updates delivered to your mailbox!

As we have already discussed about Templates concept in our earlier program lets get into Program here. In the minelement function we are accepting two paramenters of the type min. One array and another one is the size of array. First we are defining a variable called small to store the value of 1st element in the array and then using a for loop for checking the condition small<a[i], if a true instance is found the small element is being replaced by that value.

In the main function we are passing the arrays of different data types and sizes of the respective arrays by calling the function minelement.

You can refer this C Program to find minimum element in an Array without using Templates.

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

template<class min>
void minelement(min e[10], int n)
{
	int i, j;
	min small = e[0];

	for(i=0;i<n;i++)
	{
		if(small>e[i])
		{
			small = e[i];
		}
	}
	cout<<"\nSmallest Element:"<<small;
}

void main()
{
	int n=5;
	int a[5] = {3,2,4,1,5};
	float b[5] = {1.2,1.9,2.3,1.4,1.5};
	char c[5] = {'k','b','z','d','e'};
	clrscr();
	minelement(a,n);
	minelement(b,n);
	minelement(c,n);
	getch();
}

Output:
smallest-element-templates

{ 0 comments… add one now }

Leave a Comment

Previous post:

Next post: