Advertisement
Guest User

Untitled

a guest
May 22nd, 2019
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.08 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. T *arr;
  12. int num;
  13. public:
  14. Array<T>(int n=0)
  15. {
  16. arr = new T[n];
  17. num=n;
  18. }
  19.  
  20. void Erase()
  21. {
  22. delete []arr;
  23. }
  24.  
  25. T &operator[](int i)
  26. {
  27. return arr[i];
  28. }
  29.  
  30. int GetLength()
  31. {return num;}
  32.  
  33. friend ostream& operator<<(ostream &out,Array &a)
  34. {
  35. for(int i=0;i<a.num-1;i++){
  36. out<<"Array["<<i<<"] = "<<a[i]<<", ";
  37. }
  38. out<<"Array["<<a.num-1<<"] = "<<a[a.num-1]<<"\n";
  39. return out;
  40. }
  41. bool operator==(Array<T> &t)
  42. {
  43. int flag=0;
  44. for(int i=0;i<num;i++)
  45. {
  46. if(arr[i]!=t[i])
  47. {flag=1;
  48. break;}
  49. }
  50.  
  51. if(flag==0)
  52. return true;
  53. else
  54. return false;
  55. }
  56.  
  57. };
  58.  
  59.  
  60.  
  61. template <typename T>
  62. void BubbleSort(Array<T> &t)
  63. {
  64.  
  65. T temp;
  66. for(int i=0; i<t.GetLength(); i++)
  67. for(int j=i; j<t.GetLength()-1; j++)
  68. if(t[i]>t[j])
  69. {
  70. temp=t[i];
  71. t[i]=t[j];
  72. t[j]=temp;
  73. }
  74. }
  75.  
  76. template <typename T>
  77.  
  78. T Sum(Array<T> &a)
  79. {
  80. T suma=0;
  81.  
  82. for(int i=0;i<a.GetLength();i++)
  83. {
  84. suma+=a[i];
  85. }
  86.  
  87. return suma;
  88. }
  89. template <typename T>
  90. T Average(Array<T> &a)
  91. {
  92. T aver;
  93. aver=Sum(a)/a.GetLength();
  94. return aver;
  95. }
  96.  
  97. template <typename T,typename M>
  98.  
  99. bool Equal(Array<T> &first,Array<M> &second)
  100. {
  101. return first==second;
  102. }
  103.  
  104.  
  105.  
  106. template <typename T>
  107. bool Equal (Array <T> &first, Array<double> &second )
  108. {
  109. if(first.GetLength()!=second.GetLength())
  110. return false;
  111.  
  112. bool flag1=0,flag2=0;
  113.  
  114. for(int i=0; i<first.GetLength(); i++)
  115. if(abs(first[i]-second[i])<0.1)
  116. flag1=1;
  117. else
  118. {
  119. flag1=0;
  120. break;
  121. }
  122. if(abs(Average(first)-Average(second))<0.5)
  123. flag2=1;
  124. else flag2=0;
  125. if(flag1==1&&flag2==1)
  126. return true;
  127. else
  128. return false;
  129. }
  130. int main()
  131. {
  132.  
  133. int n;
  134. double r;
  135.  
  136. cin>>r;
  137. cin>>n;
  138.  
  139. Array<int> anArray(n);
  140. Array<double> adArray(n);
  141. Array<int> intArray2(n);
  142.  
  143. for (int nCount = 0; nCount < n; nCount++)
  144. {
  145.  
  146. cin>>anArray[nCount];
  147. adArray[nCount] = anArray[nCount] + r;
  148. }
  149.  
  150. BubbleSort(anArray);
  151.  
  152. intArray2 = anArray;
  153.  
  154. cout<<"The arrays: "<<endl;
  155. cout<<anArray;
  156. cout<<"and "<<endl;
  157. cout<<intArray2;
  158. cout<<((Equal(anArray,intArray2))?" ARE":" ARE NOT")<<" same!"<<endl;
  159. cout<<"The Average of the array adArray is: "<<Average(adArray)<<endl;
  160.  
  161. cout<<"The arrays: "<<endl;
  162. cout<<anArray;
  163. cout<<"and "<<endl;
  164. cout<<adArray;
  165. cout<<((Equal(anArray,adArray))?" ARE":" ARE NOT")<<" same!";
  166.  
  167.  
  168. return 0;
  169. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement