Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions homework/inner-product/arithmeticAverage.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#include "arithmeticAverage.hpp"
#include <algorithm>
#include <cmath>
#include <numeric>

double ArithmeticAverage(const std::vector<int>& vec_first, const std::vector<int>& vec_second) {
if (vec_first.empty() && vec_second.empty())
return 0;
int sum_first = std::accumulate(vec_first.begin(), vec_first.end(), 0);
int sum_second = std::accumulate(vec_second.begin(), vec_second.end(), 0);

int sum = sum_first + sum_second;
int size = vec_first.size() + vec_second.size();
return static_cast<double>(sum) / size;
}

double Distance(const std::vector<int>& vec_first, const std::vector<int>& vec_second) {
if (vec_first.size() != vec_second.size())
return 0;
if (vec_first.empty())
return 0;
double sum = 0;
for (size_t i = 0; i < vec_first.size(); i++) {
sum += std::pow(vec_second[i] - vec_first[i], 2);
}
return std::sqrt(sum);
}
5 changes: 5 additions & 0 deletions homework/inner-product/arithmeticAverage.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#pragma once
#include <vector>

double ArithmeticAverage(const std::vector<int>& vec_first, const std::vector<int>& vec_second);
double Distance(const std::vector<int>& vec_first, const std::vector<int>& vec_second);
11 changes: 11 additions & 0 deletions homework/inner-product/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#include <iostream>
#include <vector>
#include "arithmeticAverage.hpp"

int main() {
std::vector<int> first{-5, 4, 2, -5, 2, 4, 6, 7};
std::vector<int> second{3, 5, -2, 6, 7, -3, 6, 9};
auto result = ArithmeticAverage(first, second);
std::cout << result;
return 0;
}
Loading