commit 3bd57bd8f2ad21740fc0c04f61e75d5b5624f11c Author: Trygve Date: Sat Feb 24 11:37:06 2024 +0100 Lab 1 diff --git a/lab_1/1b-1.cpp b/lab_1/1b-1.cpp new file mode 100644 index 0000000..c0be0ad --- /dev/null +++ b/lab_1/1b-1.cpp @@ -0,0 +1,8 @@ +#include +#include "1b.h" +// +int main() +{ + std::cout << "Is 5 even? "<< is_even(5) << "\n"; + std::cout << "Is 56462 even? "<< is_even(56462) << "\n"; +} \ No newline at end of file diff --git a/lab_1/1b-2.cpp b/lab_1/1b-2.cpp new file mode 100644 index 0000000..7292ead --- /dev/null +++ b/lab_1/1b-2.cpp @@ -0,0 +1,12 @@ +#include "1b.h" +bool is_even(int num) +{ + if (num%2 == 0) + { + return true; + } + else + { + return false; + } +} \ No newline at end of file diff --git a/lab_1/1b.h b/lab_1/1b.h new file mode 100644 index 0000000..fc5b5b2 --- /dev/null +++ b/lab_1/1b.h @@ -0,0 +1 @@ +bool is_even(int num); \ No newline at end of file diff --git a/lab_1/2.py b/lab_1/2.py new file mode 100644 index 0000000..0ba3b61 --- /dev/null +++ b/lab_1/2.py @@ -0,0 +1,4 @@ +def scalar_product(x, y, dimension): + sum = 0.0 + for i in range(dimension): sum += x[i]*y[i] + return sum \ No newline at end of file diff --git a/lab_1/3.cpp b/lab_1/3.cpp new file mode 100644 index 0000000..bc13fe9 --- /dev/null +++ b/lab_1/3.cpp @@ -0,0 +1,34 @@ +#include +#include + +int print_fibo_until(int x) +{ + int n = 1; + int fibo_n = 1; + int fibo_previus = 0; + + while(x >= fibo_n) + { + std::cout << std::to_string(n) + std::to_string(fibo_n) + "\n"; + int fibo_next = fibo_n + fibo_previus; + n++; + fibo_previus = fibo_n; + fibo_n = fibo_next; + } + return (x==fibo_previus); +} + +int main() +{ + int y = 17711; + if(print_fibo_until(y)) + { + std::cout << y; + std::cout << " is a Fibonacci number\n"; + } + else + { + std::cout << y; + std::cout << " is not a Fibonacci number\n"; + } +} \ No newline at end of file diff --git a/lab_1/4.cpp b/lab_1/4.cpp new file mode 100644 index 0000000..fcd06c9 --- /dev/null +++ b/lab_1/4.cpp @@ -0,0 +1,24 @@ +#include + +int main() +{ + std::cout << "Check size of datatypes:\n"; + bool b; + std::cout << "bool:" + std::to_string(sizeof(b)) + "\n"; + char c; + std::cout << "char:" + std::to_string(sizeof(c)) + "\n"; + short s; + std::cout << "short:" + std::to_string(sizeof(s)) + "\n"; + int i; + std::cout << "int:" + std::to_string(sizeof(i)) + "\n"; + long l; + std::cout << "long:" + std::to_string(sizeof(l)) + "\n"; + long long ll; + std::cout << "long long:" + std::to_string(sizeof(ll)) + "\n"; + float f; + std::cout << "float:" + std::to_string(sizeof(f)) + "\n"; + double d; + std::cout << "double:" + std::to_string(sizeof(d)) + "\n"; + long double ld; + std::cout << "long double:" + std::to_string(sizeof(ld)) + "\n"; +} \ No newline at end of file diff --git a/lab_1/5.cpp b/lab_1/5.cpp new file mode 100644 index 0000000..2b876ad --- /dev/null +++ b/lab_1/5.cpp @@ -0,0 +1,21 @@ +#include +#include + +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; +} \ No newline at end of file diff --git a/lab_1/6.cpp b/lab_1/6.cpp new file mode 100644 index 0000000..927fac7 --- /dev/null +++ b/lab_1/6.cpp @@ -0,0 +1,22 @@ +#include +/*bool f(unsigned n) +{ + if(n == 'x') return true; + else return f(n - 256); +} +Under what conditions does the following function terminate and return a value? + +it terminates when you pass in the char 'x', otherwise you get a segmentationfault +*/ + +bool f(unsigned n) +{ + if(n == 'x') return true; + else return false; +} + +int main() +{ + std::cout << "f('a')= "<< f('a') << "\n"; + std::cout << "f('x')= "<< f('x') << "\n"; +} \ No newline at end of file diff --git a/lab_1/7-proof.pdf b/lab_1/7-proof.pdf new file mode 100644 index 0000000..93f4810 Binary files /dev/null and b/lab_1/7-proof.pdf differ diff --git a/lab_1/7.cpp b/lab_1/7.cpp new file mode 100644 index 0000000..624db2c --- /dev/null +++ b/lab_1/7.cpp @@ -0,0 +1,47 @@ +#include +#include +//original function: +long f(int n) +{ + if(n == 0) return 0; + else if(n < 0) return -f(-n); + else return f(n-1) + 3*n*(n-1); +} + +//My implementation: +long f2(int n) +{ + long result = 0; + if(n<0) + { + for (long i = 0; i<=n*-1; i++) + { + result += -(3*i*i-3*i); + } + } + else + { + for (long i = 0; i<=n; i++) + { + result += 3*i*i-3*i; + } + } + return result; +} + +int main() +{ + //brute force check 😅: + for (int i = -20000; i != INT_MAX ; i++){ + if(f(i) != f2(i)) + { + std::cout << i << " WRONG!!😦 " << f(i) << " != "<< f2(i) << "\n"; + /* Its seems like my implementation works well, until it reches the number 26756, there it + outputs 19154179862460 instead of 19149884895164 and the error only gets worse from there. + I have no idea why this happens, and i dont have time to figure it out 🙃 + Please enlighten me if you can! + */ + break; + } + } +} \ No newline at end of file diff --git a/lab_1/makefile b/lab_1/makefile new file mode 100644 index 0000000..ba405dd --- /dev/null +++ b/lab_1/makefile @@ -0,0 +1,15 @@ +tasks: + g++ 1b-1.cpp 1b-2.cpp 1b.h -o task1b + g++ 3.cpp -o task3 + g++ 4.cpp -o task4 + g++ 5.cpp -o task5 + g++ 6.cpp -o task6 + g++ 7.cpp -o task7 +run: + $(info Name: Trygve) + ./task1b + ./task3 + ./task4 + ./task5 + ./task6 + ./task7 \ No newline at end of file