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 n;
- public:
- Array<T>(int n) {
- this->n = n;
- arr = new T[n];
- }
- void Erase() {
- for(int i = 0; i < n; ++i)
- delete [] arr;
- arr = new T[n];
- }
- T &operator [](const int i) {
- if(i >= 0&&i < n)
- return arr[i];
- }
- int getLenght() {
- return n;
- }
- friend ostream& operator <<(ostream& out, const Array<T> &a) {
- for(int i = 0; i < a.n; ++i) {
- if(i != a.n-1)
- out << "Array[" << i << "] = " << a.arr[i] << ", ";
- else
- out << "Array[" << i << "] = " << a.arr[i];
- }
- return out;
- }
- };
- template <typename T>
- void BubbleSort(Array<T> &a) {
- T tmp;
- for(int i = 0; i < a.getLenght(); ++i) {
- for(int j = i; j < a.getLenght() - 1; ++j) {
- if(a[i] > a[j]) {
- tmp = a[i];
- a[i] = a[j];
- a[j] = tmp;
- }
- }
- }
- }
- template <typename T>
- T Sum(Array<T> &a) {
- T result = 0;
- for(int i = 0; i < a.getLenght(); ++i)
- result += a[i];
- return result;
- }
- template <typename T>
- T Average(Array<T> &a) {
- return Sum(a) / a.getLenght();
- }
- template <typename T, typename M>
- bool Equal(Array<T> &a, Array<M> &b) {
- if(a.getLenght() != b.getLenght())
- return false;
- else {
- bool flag = true;
- for(int i = 0; i < a.getLenght(); ++i) {
- if(a[i] != b[i]) {
- flag = false;
- return false;
- }
- }
- if(flag)
- return true;
- }
- }
- template <typename T>
- bool Equal(Array<T> &a, Array<double> &b) {
- if(a.getLenght() != b.getLenght())
- return false;
- else {
- bool flag = true;
- for(int i = 0; i < a.getLenght(); ++i) {
- if(fabs(a[i] - b[i]) > 0.1) {
- flag = false;
- return flag;
- } else if(fabs(Average(a) - Average(b)) > 0.5) {
- flag = false;
- return flag;
- }
- }
- return flag;
- }
- }
- 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<<endl<<"and "<<endl;
- cout<<intArray2;
- cout<<endl;
- 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<<endl<<"and "<<endl;
- cout<<adArray;
- cout<<endl;
- cout<<((Equal(anArray,adArray))?" ARE":" ARE NOT")<<" same!";
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement