Advertisement
Leedwon

Untitled

Mar 30th, 2017
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.16 KB | None | 0 0
  1. // cw program rozdzial 8 ad.6
  2.  
  3. #include <iostream>
  4.  
  5. template <typename T>
  6. void max(T var1[], int tSize);
  7. template <> void max <char*> (char * arr[], int tSize);
  8.  
  9. int main()
  10. {
  11.     using namespace std;
  12.     int arr1[5] = { 1,2,3,4,5 };
  13.     cout << sizeof(arr1) / sizeof(arr1[0]) << endl;
  14.     max(arr1, 5);
  15.     double arr2[4] = { 1.1, 1.2, 1.3, 1.4 };
  16.     cout << endl << sizeof(arr2) / sizeof(arr2[0]) << endl;
  17.     max(arr2, 5);
  18.     char * arr3[4] = { "trzybobry", "czterybobry", "piecbobrow", "szescbobrow" };
  19.     cout << endl << &arr3[3];
  20.     cout << endl << arr3[3];
  21.     cout << endl;
  22.     max(arr3, 4);
  23.     return 0;
  24. }
  25.  
  26. template <typename T>
  27. void max(T var1[], int tSize)
  28. {
  29.     T max = var1[0];
  30.     for (int i = 1; i < tSize; i++)
  31.     {
  32.         if (var1[i] > max) max = var1[i];
  33.     }
  34.     std::cout << max;
  35. }
  36. template <> void max <char*>(char * arr[], int tSize)
  37. {
  38.     int * length = new int[tSize];
  39.     for (int i = 0; i < tSize; i++)
  40.     {
  41.         length[i] = 0;
  42.         for (char  j = *arr[0]; j != '\0'; j++) length[i] ++;
  43.     }
  44.     int max = length[0];
  45.     char * adress = arr[0];
  46.     for (int i = 1; i < tSize; i++)
  47.     {
  48.         if (length[i] > max)
  49.         {
  50.             max = length[i];
  51.             adress = arr[i];
  52.         }
  53.     }
  54.     std::cout << adress;
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement