amarek

OOP LV4 - Zadatak1

Nov 11th, 2019
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.25 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdlib>
  3. #include <ctime>
  4.  
  5. using namespace std;
  6.  
  7. template <typename Type>
  8. void swap(Type *xp, Type *yp)
  9. {
  10.     Type temp = *xp;
  11.     *xp = *yp;
  12.     *yp = temp;
  13. }
  14.  
  15. template <typename Type>
  16. void sort(Type arr[], int n) {
  17.     int i, j;
  18.     for (i = 0; i < n - 1; i++) {
  19.         for (j = 0; j < n - i - 1; j++) {
  20.             if (arr[j] > arr[j + 1]) {
  21.                 swap(&arr[j], &arr[j + 1]);
  22.             }  
  23.         }
  24.     }
  25. }
  26.  
  27. template <typename Type>
  28. void print(Type arr[], int n) {
  29.     int i;
  30.     for (i = 0; i < n; i++) {
  31.         cout << arr[i] << "\t";
  32.     }
  33.     cout << endl;
  34. }
  35.  
  36. int main() {
  37.     int i;
  38.     std::srand((unsigned int)std::time(0));
  39.  
  40.     int *a = new int[10];
  41.     for (i = 0; i < 10; i++) {
  42.         a[i] = (int)rand() * 10 / (int)RAND_MAX;
  43.     }
  44.     sort(a, 10);
  45.     print(a, 10);
  46.  
  47.     double *b = new double[10];
  48.     for (i = 0; i < 10; i++) {
  49.         b[i] = (double)rand() * 10 / (double)RAND_MAX;
  50.     }
  51.     sort(b, 10);
  52.     print(b, 10);
  53.  
  54.     char *c = new char[10];
  55.     for (i = 0; i < 10; i++) {
  56.         c[i] = (char)rand() * 10 / (char)RAND_MAX;
  57.     }
  58.     sort(c, 10);
  59.     print(c, 10);
  60.  
  61.     delete[] a;
  62.     delete[] b;
  63.     delete[] c;
  64.  
  65.     return 0;
  66. }
Advertisement
Add Comment
Please, Sign In to add comment