Created
January 19, 2012 15:18
-
-
Save sinannar/1640560 to your computer and use it in GitHub Desktop.
recursive template function that find smallest one of an array
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include<iostream> | |
using namespace std; | |
template<class T> | |
T findSmallest(T* arr,int size) | |
{ | |
if(size==1) | |
return arr[0]; | |
else{ | |
if(arr[0]<findSmallest(&arr[1],size-1)) | |
return arr[0]; | |
else return findSmallest(&arr[1],size-1); | |
} | |
} | |
int main() | |
{ | |
int a[10]={1,2,3,4,5,6,7,8,9,10}; | |
char b[10]={'-','b','c','d','e','f','g','h','i','j'}; | |
cout<<findSmallest(a,10)<<endl; | |
cout<<findSmallest(b,10)<<endl; | |
return 1; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment