INF205/lab_1/5.cpp
2024-02-24 11:37:06 +01:00

21 lines
958 B
C++

#include <cmath>
#include <iostream>
double* get_unit_vec(double vector[3])
{
double magnitude = std::sqrt(std::pow(vector[0], 2) + std::pow(vector[1], 2) + std::pow(vector[2], 2));
double *result = new double[3]{vector[0] / magnitude,vector[1] / magnitude,vector[2] / magnitude};
return result;
}
int main()
{
std::cout << "Test the get_unit_vec funtion:\n";
double vector[3] = {4.0,6.0,8.0};
double *unit_vec = get_unit_vec(vector);
std::cout << "old x: " << vector[0] << " new x: " << unit_vec[0] << "\n";
std::cout << "old y: " << vector[1] << " new y: " << unit_vec[1] << "\n";
std::cout << "old z: " << vector[2] << " new z: " << unit_vec[2] << "\n";
std::cout << "old length: " << std::sqrt(std::pow(vector[0], 2) + std::pow(vector[1], 2) + std::pow(vector[2], 2)) <<" new length: " << std::sqrt(std::pow(unit_vec[0], 2) + std::pow(unit_vec[1], 2) + std::pow(unit_vec[2], 2)) << "\n";
delete[] unit_vec;
}