Advertisement
PyTimur

Untitled

May 23rd, 2022
642
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. template <class T, int k>
  5. class Vector{
  6.     int n = k;
  7.     T* a;
  8. public:
  9.     Vector() {
  10.         a = new T[n];
  11.         for (int i = 0; i < n; ++i) { a[i] = 0; }
  12.     }
  13.     Vector(T* a){
  14.         this->a = new T[n];
  15.         for (int i = 0; i < n; ++i){ this->a[i] = a[i]; }
  16.     }
  17.     ~Vector(){ delete [] a; }
  18.     void Input(){
  19.         for (int i = 0; i < n; ++i){ cin >> a[i]; }
  20.     }
  21.     void Print(){
  22.         cout << "(";
  23.         for (int i = 0; i < n-1; ++i) { cout << a[i] << ","; }
  24.         cout << a[n-1] << ")" << endl;
  25.     }
  26.     void Multi(int t) {
  27.         for (int i = 0; i < n; ++i){ a[i] = a[i]*t; }
  28.     }
  29.     void Sum(Vector<T,k> &B){
  30.         if (n != B.n) { throw 1; }
  31.         for (int i = 0; i < n; ++i){ a[i] = a[i] + B.a[i]; }
  32.     }
  33. };
  34.  
  35.  
  36. int main(){
  37.     Vector <float, 5>A;
  38.     A.Input();
  39.     A.Print();
  40.     A.Multi(10);
  41.     A.Print();
  42.     float a[5] = {1.2,0.3,5.6,7.8,1.9};
  43.     Vector <float,5>B(a);
  44.     B.Input();
  45.     A.Sum(B);
  46.     A.Print();
  47.     return 0;
  48. }
  49.  
Advertisement
RAW Paste Data Copied
Advertisement