Advertisement
deko96

Untitled

May 14th, 2015
270
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.67 KB | None | 0 0
  1. #include <iostream>
  2. #include <cmath>
  3. #include <cstdlib>
  4.  
  5. using namespace std;
  6.  
  7. template <typename T>
  8. class Array {
  9. private:
  10.     T *arr;
  11.     int n;
  12. public:
  13.     Array<T>(int n) {
  14.         this->n = n;
  15.         arr = new T[n];
  16.     }
  17.     void Erase() {
  18.         for(int i = 0; i < n; ++i)
  19.             delete [] arr;
  20.         arr = new T[n];
  21.     }
  22.     T &operator [](const int i) {
  23.         if(i >= 0&&i < n)
  24.             return arr[i];
  25.     }
  26.     int getLenght() {
  27.         return n;
  28.     }
  29.     friend ostream& operator <<(ostream& out, const Array<T> &a) {
  30.         for(int i = 0; i < a.n; ++i) {
  31.             if(i != a.n-1)
  32.                 out << "Array[" << i << "] = " << a.arr[i] << ", ";
  33.             else
  34.                 out << "Array[" << i << "] = " << a.arr[i];
  35.         }
  36.         return out;
  37.     }
  38. };
  39.  
  40. template <typename T>
  41. void BubbleSort(Array<T> &a) {
  42.     T tmp;
  43.     for(int i = 0; i < a.getLenght(); ++i) {
  44.         for(int j = i; j < a.getLenght() - 1; ++j) {
  45.             if(a[i] > a[j]) {
  46.                 tmp = a[i];
  47.                 a[i] = a[j];
  48.                 a[j] = tmp;
  49.             }
  50.         }
  51.     }
  52. }
  53. template <typename T>
  54. T Sum(Array<T> &a) {
  55.     T result = 0;
  56.     for(int i = 0; i < a.getLenght(); ++i)
  57.         result += a[i];
  58.     return result;
  59. }
  60. template <typename T>
  61. T Average(Array<T> &a) {
  62.     return Sum(a) / a.getLenght();
  63. }
  64. template <typename T, typename M>
  65. bool Equal(Array<T> &a, Array<M> &b) {
  66.     if(a.getLenght() != b.getLenght())
  67.         return false;
  68.     else {
  69.         bool flag = true;
  70.         for(int i = 0; i < a.getLenght(); ++i) {
  71.             if(a[i] != b[i]) {
  72.                 flag = false;
  73.                 return false;
  74.             }
  75.         }
  76.         if(flag)
  77.             return true;
  78.     }
  79. }
  80. template <typename T>
  81. bool Equal(Array<T> &a, Array<double> &b) {
  82.     if(a.getLenght() != b.getLenght())
  83.         return false;
  84.     else {
  85.         bool flag = true;
  86.         for(int i = 0; i < a.getLenght(); ++i) {
  87.             if(fabs(a[i] - b[i]) > 0.1) {
  88.                 flag = false;
  89.                 return flag;
  90.             } else if(fabs(Average(a) - Average(b)) > 0.5) {
  91.                 flag = false;
  92.                 return flag;
  93.             }
  94.         }
  95.         return flag;
  96.     }
  97. }
  98. int main()
  99. {
  100.  
  101.   int n;
  102.   double r;
  103.  
  104.   cin>>r;
  105.   cin>>n;
  106.  
  107.   Array<int> anArray(n);
  108.   Array<double> adArray(n);
  109.   Array<int> intArray2(n);
  110.  
  111.   for (int nCount = 0; nCount < n; nCount++)
  112.   {
  113.  
  114.       cin>>anArray[nCount];
  115.       adArray[nCount] = anArray[nCount] + r;
  116.   }
  117.  
  118.   BubbleSort(anArray);
  119.  
  120.   intArray2 = anArray;
  121.  
  122.   cout<<"The arrays: "<<endl;
  123.   cout<<anArray;
  124.   cout<<endl<<"and "<<endl;
  125.   cout<<intArray2;
  126.   cout<<endl;
  127.   cout<<((Equal(anArray,intArray2))?" ARE":" ARE NOT")<<" same!"<<endl;
  128.   cout<<"The Average of the array adArray is: "<<Average(adArray)<<endl;
  129.  
  130.   cout<<"The arrays: "<<endl;
  131.   cout<<anArray;
  132.   cout<<endl<<"and "<<endl;
  133.   cout<<adArray;
  134.   cout<<endl;
  135.   cout<<((Equal(anArray,adArray))?" ARE":" ARE NOT")<<" same!";
  136.  
  137.  
  138. return 0;
  139. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement