Lab 1
This commit is contained in:
		
						commit
						3bd57bd8f2
					
				
							
								
								
									
										8
									
								
								lab_1/1b-1.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										8
									
								
								lab_1/1b-1.cpp
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,8 @@ | ||||
| #include <iostream> | ||||
| #include "1b.h" | ||||
| //
 | ||||
| int main() | ||||
| { | ||||
|     std::cout << "Is 5 even? "<< is_even(5) << "\n"; | ||||
|     std::cout << "Is 56462 even? "<< is_even(56462) << "\n"; | ||||
| } | ||||
							
								
								
									
										12
									
								
								lab_1/1b-2.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										12
									
								
								lab_1/1b-2.cpp
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,12 @@ | ||||
| #include "1b.h" | ||||
| bool is_even(int num) | ||||
| { | ||||
|     if (num%2 == 0) | ||||
|     { | ||||
|         return true; | ||||
|     } | ||||
|     else | ||||
|     { | ||||
|         return false; | ||||
|     } | ||||
| } | ||||
							
								
								
									
										1
									
								
								lab_1/1b.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										1
									
								
								lab_1/1b.h
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1 @@ | ||||
| bool is_even(int num); | ||||
							
								
								
									
										4
									
								
								lab_1/2.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										4
									
								
								lab_1/2.py
									
									
									
									
									
										Normal file
									
								
							| @ -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 | ||||
							
								
								
									
										34
									
								
								lab_1/3.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										34
									
								
								lab_1/3.cpp
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,34 @@ | ||||
| #include <iostream> | ||||
| #include <string> | ||||
| 
 | ||||
| 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"; | ||||
|     } | ||||
| } | ||||
							
								
								
									
										24
									
								
								lab_1/4.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										24
									
								
								lab_1/4.cpp
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,24 @@ | ||||
| #include <iostream> | ||||
| 
 | ||||
| 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"; | ||||
| } | ||||
							
								
								
									
										21
									
								
								lab_1/5.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										21
									
								
								lab_1/5.cpp
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,21 @@ | ||||
| #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; | ||||
| } | ||||
							
								
								
									
										22
									
								
								lab_1/6.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										22
									
								
								lab_1/6.cpp
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,22 @@ | ||||
| #include <iostream> | ||||
| /*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"; | ||||
| } | ||||
							
								
								
									
										
											BIN
										
									
								
								lab_1/7-proof.pdf
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										
											BIN
										
									
								
								lab_1/7-proof.pdf
									
									
									
									
									
										Normal file
									
								
							
										
											Binary file not shown.
										
									
								
							
							
								
								
									
										47
									
								
								lab_1/7.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										47
									
								
								lab_1/7.cpp
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,47 @@ | ||||
| #include <iostream> | ||||
| #include <climits> | ||||
| //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; | ||||
|         } | ||||
|     } | ||||
| } | ||||
							
								
								
									
										15
									
								
								lab_1/makefile
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										15
									
								
								lab_1/makefile
									
									
									
									
									
										Normal file
									
								
							| @ -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 | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user