Lab12_T1

mail@pastecode.io avatar
unknown
c_cpp
2 years ago
1.2 kB
3
Indexable
Never
#include <iostream>
#include <cmath>
#include <vector>
#include <stdio.h>

using namespace std;

template <class Type>

void InitArray(vector<Type> (&A))
{
    srand(time(0));
    for (int i = 0; i < A.size(); i++)
    {
        A[i] = (rand()%100) / 10.0;
    }
}
template <class Type>
ostream &operator<<(ostream &stream, vector<Type> (&A))
{
    for (int i = 0; i < A.size(); i++) 
    {
        stream << A[i] << " ";
    }
    stream<<std::endl;
    return stream;
}
template <class Type>
vector<Type>& operator +(vector<Type> (&A), int n)
{
    for (int i = 0; i < n; i++) 
    {
        A.push_back(0);
    }
    return A;
}

template <class Type>
Type DotProduct(vector<Type> (&v1), vector<Type> (&v2), int dimension)
{
    Type result = 0;
    for (int i = 0; i < dimension; i++)
    {
        result += v1[i] * v2[i];
    }
    return result;
}


int main()
{
    int dimension;
    cout << "Enter dimensions: ";
    cin >> dimension;
    
    vector<float> v1(dimension);
    vector<float> v2(dimension);
    InitArray(v1);
    InitArray(v2);
    
    cout << v1 << v2;
    cout << "The dot product of 2 vectors is: " << DotProduct(v1, v2, dimension);
    return 0;
}