Quiz 03

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.

distance-between-two-points-formula

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

Quiz03.jpg

I did a for loop, with an integer variable called t, which was initialized to 1. This t would get a value of t +1 each time the loop was done, until it reached the last number with a velue less than n, the number input by the user.

The for loop would repeat the operation  f = fI + fII  n times.

fI was initialized before as 0, and fII as 1, the initial values of the Fibonacci series. So if we run this program to say, number 3, the first value that f would get on the first loop would be 1, the next value that f would get, after running the loop again would be 2.

The operations being done all the way to the third number of the series would be the following:

f = 0 + 1 = 1 // The initial value of fI is 0 and of fII is 1, thus the answer to fI + fII  would be 1.

f = 1 + 1 = 2 // Notice that the values of  fI and fII have changed. This happens because fI gets the value of fII, and fII gets the value of f, each time the loop is run. fI = fII;
fII = f;

The operations would stop right there because the number of operations done (t) have to be less than the value of n. On this example, n = 3, and t has already reached the last integer value before 3, which is 2.

If n = 5, the operations would continue as the following:

f = 0 + 1 = 1

f = 1 + 1 = 2

f = 1 + 2 = 3

f = 2 + 3 = 5

And again, the program would stop here since n (value of 5) has to be less than t (wich already reached 4).

If you type 0 as n, the program would give you the value of 0, because the program would not run, since t was initialized to 1, and t has to be less 0. 1 is not less than 0.

The real treak here was to intitialize the fI variable to fII and fII to the solution of the operation, all of this inside the for loop.

 

Quiz03.jpg

 

Here is my code for Quiz 03.

Haga click para ver el pase de diapositivas.

CC BY-SA 4.0 Quiz 03 by netosanchezb is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.