Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cmath>
- #include <cstdlib>
- using namespace std;
- template <typename T>
- class Array
- {
- private:
- T *arr;
- int num;
- public:
- Array<T>(int n=0)
- {
- arr = new T[n];
- num=n;
- }
- void Erase()
- {
- delete []arr;
- }
- T &operator[](int i)
- {
- return arr[i];
- }
- int GetLength()
- {return num;}
- friend ostream& operator<<(ostream &out,Array &a)
- {
- for(int i=0;i<a.num-1;i++){
- out<<"Array["<<i<<"] = "<<a[i]<<", ";
- }
- out<<"Array["<<a.num-1<<"] = "<<a[a.num-1]<<"\n";
- return out;
- }
- bool operator==(Array<T> &t)
- {
- int flag=0;
- for(int i=0;i<num;i++)
- {
- if(arr[i]!=t[i])
- {flag=1;
- break;}
- }
- if(flag==0)
- return true;
- else
- return false;
- }
- };
- template <typename T>
- void BubbleSort(Array<T> &t)
- {
- T temp;
- for(int i=0; i<t.GetLength(); i++)
- for(int j=i; j<t.GetLength()-1; j++)
- if(t[i]>t[j])
- {
- temp=t[i];
- t[i]=t[j];
- t[j]=temp;
- }
- }
- template <typename T>
- T Sum(Array<T> &a)
- {
- T suma=0;
- for(int i=0;i<a.GetLength();i++)
- {
- suma+=a[i];
- }
- return suma;
- }
- template <typename T>
- T Average(Array<T> &a)
- {
- T aver;
- aver=Sum(a)/a.GetLength();
- return aver;
- }
- template <typename T,typename M>
- bool Equal(Array<T> &first,Array<M> &second)
- {
- return first==second;
- }
- template <typename T>
- bool Equal (Array <T> &first, Array<double> &second )
- {
- if(first.GetLength()!=second.GetLength())
- return false;
- bool flag1=0,flag2=0;
- for(int i=0; i<first.GetLength(); i++)
- if(abs(first[i]-second[i])<0.1)
- flag1=1;
- else
- {
- flag1=0;
- break;
- }
- if(abs(Average(first)-Average(second))<0.5)
- flag2=1;
- else flag2=0;
- if(flag1==1&&flag2==1)
- return true;
- else
- return false;
- }
- int main()
- {
- int n;
- double r;
- cin>>r;
- cin>>n;
- Array<int> anArray(n);
- Array<double> adArray(n);
- Array<int> intArray2(n);
- for (int nCount = 0; nCount < n; nCount++)
- {
- cin>>anArray[nCount];
- adArray[nCount] = anArray[nCount] + r;
- }
- BubbleSort(anArray);
- intArray2 = anArray;
- cout<<"The arrays: "<<endl;
- cout<<anArray;
- cout<<"and "<<endl;
- cout<<intArray2;
- cout<<((Equal(anArray,intArray2))?" ARE":" ARE NOT")<<" same!"<<endl;
- cout<<"The Average of the array adArray is: "<<Average(adArray)<<endl;
- cout<<"The arrays: "<<endl;
- cout<<anArray;
- cout<<"and "<<endl;
- cout<<adArray;
- cout<<((Equal(anArray,adArray))?" ARE":" ARE NOT")<<" same!";
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement