masterm1nd99

генн1

May 23rd, 2019
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.15 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. {
  10. private:
  11. int n; // broj na elementi na nizata
  12. T *elementi; // pokazhuvach kon elementi od tip T
  13.  
  14. public:
  15. Array<T> (int n)
  16. {
  17. elementi = new T[n];
  18. this->n=n;
  19. }
  20. void Erase ()
  21. {
  22. for(int i=0; i<n; i++)
  23. delete elementi[i];
  24. delete [] elementi;
  25. }
  26. T &operator[] (int i) // const
  27. {
  28. if(i>=0&&i<n)
  29. return elementi[i];
  30. }
  31.  
  32. int getLength()
  33. {
  34. return n;
  35. }
  36.  
  37. friend ostream& operator<< (ostream &out, const Array<T> &a)
  38. {
  39. for (int i=0; i<a.n; i++)
  40. if(i==a.n-1) out<<"Array["<<i<<"] = "<<a.elementi[i];
  41. else out<<"Array["<<i<<"] = "<<a.elementi[i]<<", ";
  42. return out;
  43. }
  44. };
  45.  
  46. template <typename T>
  47. void BubbleSort(Array<T> &t) // ги сорира елементите користејќи го BubbleSort алгоритмот
  48. {
  49.  
  50. T temp;
  51. for(int i=0; i<t.getLength(); i++)
  52. for(int j=i; j<t.getLength()-1; j++)
  53. if(t[i]>t[j])
  54. {
  55. temp=t[i];
  56. t[i]=t[j];
  57. t[j]=temp;
  58. }
  59. }
  60.  
  61. template <typename T>
  62. T Sum(Array<T> &t) // ги сумира елементите (да се претпостави дека операторот += e преоптоварен за елементите од класата).
  63. {
  64. T vk = 0;
  65. for(int i=0; i<t.getLength(); i++)
  66. vk+=t[i];
  67. return vk;
  68. }
  69.  
  70. template <typename T>
  71. double Average(Array<T> &t) // користејќи ја функцијата за сума пресметува просечна вредност на елементите
  72. {
  73. return Sum(t)/t.getLength();
  74. }
  75.  
  76. template <typename T,typename M>
  77. bool Equal (Array<T> &t, Array<M> &m) // проверува дали низите се еднакви
  78. {
  79. if(t.getLength()!=m.getLength()) return false;
  80. bool flag=false;
  81. for(int i=0; i<t.getLength(); i++)
  82. if(t[i]==m[i]) flag=true;
  83. else
  84. {
  85. flag=false;
  86. break;
  87. }
  88. return flag;
  89. }
  90.  
  91. template <typename T>
  92. bool Equal (Array <T> &t, Array<double> &d) // која ќе изврши пацијална специјализација и ќе смета дека:
  93. // два елементи се исти ако и само ако се разликуваат за помалку од 0.1
  94. // разликата од просечната вредност на низите мора да е помала од 0.5.
  95. {
  96. if(t.getLength()!=d.getLength()) return false;
  97. bool flag1=false,flag2=false;
  98. for(int i=0; i<t.getLength(); i++)
  99. if(abs(t[i]-d[i])<0.1) flag1=true;
  100. else
  101. {
  102. flag1=false;
  103. break;
  104. }
  105. if(abs(Average(t)-Average(d))<0.5)
  106. flag2=true;
  107. else flag2=false;
  108. if(flag1&&flag2) // akko se true dvete
  109. return true;
  110. else return false;
  111. }
  112.  
  113. int main()
  114. {
  115.  
  116. int n;
  117. double r;
  118.  
  119. cin>>r;
  120. cin>>n;
  121.  
  122. Array<int> anArray(n);
  123. Array<double> adArray(n);
  124. Array<int> intArray2(n);
  125.  
  126. for (int nCount = 0; nCount < n; nCount++)
  127. {
  128.  
  129. cin>>anArray[nCount];
  130. adArray[nCount] = anArray[nCount] + r;
  131. }
  132.  
  133. BubbleSort(anArray);
  134.  
  135. intArray2 = anArray;
  136.  
  137. cout<<"The arrays: "<<endl;
  138. cout<<anArray;
  139. cout<<endl<<"and "<<endl;
  140. cout<<intArray2;
  141. cout<<endl;
  142. cout<<((Equal(anArray,intArray2))?" ARE":" ARE NOT")<<" same!"<<endl;
  143. cout<<"The Average of the array adArray is: "<<Average(adArray)<<endl;
  144.  
  145. cout<<"The arrays: "<<endl;
  146. cout<<anArray;
  147. cout<<endl<<"and "<<endl;
  148. cout<<adArray;
  149. cout<<endl;
  150. cout<<((Equal(anArray,adArray))?" ARE":" ARE NOT")<<" same!";
  151.  
  152.  
  153. return 0;
  154. }
Add Comment
Please, Sign In to add comment