frusso1337

generic array c++

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