Ex1
This commit is contained in:
parent
7aa5957739
commit
9f12508a06
5
Makefile
5
Makefile
@ -11,6 +11,11 @@ sheet01:
|
|||||||
chmod +x build/sheet01
|
chmod +x build/sheet01
|
||||||
build/sheet01
|
build/sheet01
|
||||||
|
|
||||||
|
sheet02:
|
||||||
|
g++ src/sheet02.cpp -o ./build/sheet02
|
||||||
|
chmod +x build/sheet02
|
||||||
|
build/sheet02
|
||||||
|
|
||||||
pizza:
|
pizza:
|
||||||
g++ src/pizza.cpp -o ./build/pizza
|
g++ src/pizza.cpp -o ./build/pizza
|
||||||
chmod +x build/pizza
|
chmod +x build/pizza
|
||||||
|
@ -1,17 +1,71 @@
|
|||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <string>
|
//#include <string>
|
||||||
#include <cmath>
|
#include <cmath>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
double euclidean_length(const std::vector<double> &v);
|
||||||
|
double scalar_product(const std::vector<double> &v, const std::vector<double> &w);
|
||||||
|
std::vector<double> normalize(const std::vector<double> &v);
|
||||||
|
double euclidean_distance(const std::vector<double> &v, const std::vector<double> &w);
|
||||||
|
|
||||||
|
void print_dvector(const std::vector<double> &v);
|
||||||
|
|
||||||
|
|
||||||
int main()
|
int main()
|
||||||
{
|
{
|
||||||
int selection;
|
std::vector<double> a = {1, 2, 3};
|
||||||
std::cout << "";
|
std::vector<double> b = {4, 5, 6};
|
||||||
std::cout << "Please select an Exercise: ";
|
std::cout << "length of 'a': " << euclidean_length(a) << '\n';
|
||||||
std::cin >> selection;
|
std::cout << "scalar product of 'a' and 'b': " << scalar_product(a, b) << '\n';
|
||||||
|
print_dvector(normalize(a));
|
||||||
|
std::cout << "distance between 'a' and 'b': " << euclidean_distance(a, b) << '\n';
|
||||||
|
}
|
||||||
|
|
||||||
switch(selection)
|
void print_dvector(const std::vector<double> &v) {
|
||||||
|
for (const double &d : v) {
|
||||||
|
std::cout << d << '\n';
|
||||||
|
}
|
||||||
|
std::cout << '\n';
|
||||||
|
}
|
||||||
|
double euclidean_length(const std::vector<double> &v)
|
||||||
{
|
{
|
||||||
case 1:
|
double sum = 0;
|
||||||
break;
|
for(int i=0; i<v.size(); ++i)
|
||||||
|
{
|
||||||
|
sum += v[i]*v[i];
|
||||||
}
|
}
|
||||||
|
return std::sqrt(sum);
|
||||||
|
}
|
||||||
|
|
||||||
|
double scalar_product(const std::vector<double> &v, const std::vector<double> &w)
|
||||||
|
{
|
||||||
|
double product = 0;
|
||||||
|
if(v.size()!=w.size()) { return std::nan(""); }
|
||||||
|
for(int i=0; i<v.size(); ++i)
|
||||||
|
{
|
||||||
|
product += v[i]*w[i];
|
||||||
|
}
|
||||||
|
return product;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::vector<double> normalize(const std::vector<double> &v)
|
||||||
|
{
|
||||||
|
double length = euclidean_length(v);
|
||||||
|
std::vector<double> norm;
|
||||||
|
for(int i=0; i<v.size(); ++i)
|
||||||
|
{
|
||||||
|
norm.push_back(v[i]/length);
|
||||||
|
}
|
||||||
|
return norm;
|
||||||
|
}
|
||||||
|
|
||||||
|
double euclidean_distance(const std::vector<double> &v, const std::vector<double> &w)
|
||||||
|
{
|
||||||
|
if(v.size()!=w.size()) { return std::nan(""); }
|
||||||
|
double distance = 0;
|
||||||
|
for(int i=0; i<v.size(); ++i)
|
||||||
|
{
|
||||||
|
distance += (v[i]-w[i])*(v[i]-w[i]);
|
||||||
|
}
|
||||||
|
return std::sqrt(distance);
|
||||||
}
|
}
|
Loading…
x
Reference in New Issue
Block a user