Advertisement
Guest User

Untitled

a guest
Mar 28th, 2017
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.77 KB | None | 0 0
  1. #include <cstdlib>
  2. #include <iostream>
  3.  
  4. using namespace std;
  5.  
  6. template <class T>
  7.  T MaxT( T* tab, int n)
  8. {
  9.     T temp = tab[0];
  10.     if(n<1||tab==NULL)
  11.     {
  12.         string wyjatek="T MaxT(T*tab, int n):Niepoprawne dane";
  13.         throw wyjatek;
  14.     }
  15.  
  16.     for(int i=1;i<n;i++){
  17.         if(temp<tab[i]){
  18.             temp=tab[i];   
  19.         }
  20.     }
  21.     return temp;
  22. }
  23.    
  24.  
  25. int main()
  26. {
  27.    
  28.     int tabInt[5] = {1,8,3,4,5};
  29.     string tabString[5] = {"Alicja","Marek","Paweł","Zosia","Adam"};
  30.    
  31.     try{
  32.         cout<<"Maksimum dla liczb całkowitych w tablicy to: ";
  33.         cout<<MaxT<int>(tabInt,5)<<endl<<endl;
  34.        
  35.         cout<<"Maksimum dla stringow w tablicy to: ";
  36.         cout<<MaxT<string>(tabString,5)<<endl;
  37.        
  38.     }catch(const char* err){  
  39.         cout<<err<<endl;
  40.     }
  41.    
  42.    
  43.     system("PAUSE");
  44.     return 0;
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement