L4_T1

mail@pastecode.io avatar
unknown
c_cpp
2 years ago
1.1 kB
1
Indexable
Never
#include<iostream>
#include<cstdlib>

using namespace std;

const int MAX = 100;
const int MAX_RAND = 10;

void InitArray(double arr[MAX], int n);
void PrintArray(double arr[MAX], int n);
double DotProduct(double v1[MAX], double v2[MAX], int n);

int main()
{
    int n;
    
    cout << "Enter the number of dimensions of vectors (<100)" << endl;
    cin >> n;
    
    double v1[MAX];    
    double v2[MAX];
    
    InitArray(v1, n);
    InitArray(v2, n);
    
    PrintArray(v1, n);
    PrintArray(v2, n);
    
    cout << "The dot product of the vectors is: " << DotProduct(v1, v2, n);

    return 0;
}

void InitArray(double arr[MAX], int n)
{
    for(int i = 0; i < n; i++)
    {
        arr[i] = rand() % MAX_RAND;
    }
    cout << "The array is initialized" << endl;
}
void PrintArray(double arr[MAX], int n)
{
    
    for(int i = 0; i < n; i++)
    {
        cout << ' ' << arr[i];
    }
    cout << endl;
}
double DotProduct(double v1[MAX], double v2[MAX], int n)
{
    double dotProduct = 0;
    
    for(int i = 0; i < n; i++)
    {
        dotProduct += v1[i] * v2[i];
    }
    return dotProduct;
}