Today we had to do Quiz number 3. Problem number one was about the distance between two points on a cartesian plane. Number two was way harder. We had to make a program that would calculate the Fibonacci series of numbers till the number that the user inputs.
Problem number one was easy for me. I just used the equation of the distance between two points, which is the Pitagorean Theorem applied.
My translation to C++ language was: d = sqrt(pow((c-a),2)+ pow((d-b),2)), a, b, c and d being parameters of my function, input by the user.
The second problem was the one that troubled me. The most important thing while solving any kind of problem, be it C++ or physics is to really understand what you are being asked. So, that was my frst step, to understand what a Fibonnacci series is. It is the sum of a number plus the next one, starting on 0. So the series would look like this:
0,1,1,2,3,5,8,13…
0+1 = 1, 1+1 = 2, 2+1 = 3, 3+2 = 5, 5+3 = 8, 8+5 = 13, 13+8 = 21;
To translate this to a command in C++ was hard for me. But I did it the next way:
#include <iostream>
using namespace std;
int n, fI = 0, fII = 1, f;
int fibonacci(int n){
for (int t = 1; t < n ; t++){
f = fI + fII ;
fI = fII;
fII = f;
}
return f;
}
int main(){
cout << “Provide the nth number of the Fibonacci list that you want to print: “<< endl;
cin >> n;
cout << “Your number is: ” << fibonacci(n);
}
I created a function called fibonacci, with 1 parameter (the number of the series that the user wanted to output). Inside the
Continue reading “Quiz 03” →