Translate the following code to Python:
double scalar_product(double x[], double y[], double dimension) { double sum = 0.0; for(int i = 0; i < dimension; i++) sum += x[i]*y[i]; return sum; }
Rewrite in C++. Note that like in every complete C/C++ code, there will need to be a int main() function.
# output Fibonacci numbers smaller or equal to x # return True if x is a Fibonacci number, False if it is not # def print_fibo_until(x): n = 1 fibo_n = 1 fibo_previous = 0 while x >= fibo_n: print(n, fibo_n, sep="\t", end="\n") fibo_next = fibo_n + fibo_previous n += 1 fibo_previous = fibo_n fibo_n = fibo_next return x == fibo_previous y = 17711 if print_fibo_until(y): print(y, "is a Fibonacci number") else: print(y, "is not a Fibonacci number")
Use the keyword sizeof to determine, for your system, what size the following data types have: bool, char, short, int, long, and long long; float, double, and long double.
Write a function that normalizes a three-dimensional vector, converting it into the unit vector pointing in the same direction. The vector should be passed as a double array, i.e., as a static array of double-precision floating-point values.
Under what conditions does the following function terminate and return a value?
bool f(unsigned n) { if(n == 'x') return true; else return f(n - 256); }
Replace this code by a simple one that always terminates, returns true where the given f(n) returns true, and returns false where the given f(n) does not terminate.
Analysing the following code, determine the return value of f(n) as a function of n, proving by induction that this is how the code behaves.
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); }
Write a code that is equivalent to the one given above, but more computationally efficient.
(submit through Canvas by end of 13th February 2024)