PICTURE OF AUTOR

THIS IS THE #QUIZ8 WHOSE OBJECTIVE IS CREATE AND CALL FUNCTIONS TO DO DIFFERENT TASKS AT DIFFERENT TIMES USING RECURSION AND LOOPS. COVERING #MASTERYTOPIC06 #MASTERYTOPIC07 . This IS ALSO to fulfill the #Mastery12 Use of recursion for repetitive algorithms

This #QUIZ08 makes first a survey where I gave some advice or feedback to ken bauer in how i am learning in this course. Next, in this quiz 8 makes a function that calculates  and returns the “nth” Fibonacci number where we define a function over the Fibonacci numbers mapping the naturals (starting with zero) to the Fibonacci series.

fibo2

Link of the picture:Link of the picture

So fibonacci(0) returns 0, fibonacci(1) returns 1, fibonacci(2) returns 1 and so on. Note that we are using the modern definition where the sequence starts with zero. I tried to implement this with two solutions: one with a loop and one with recursion. Which do I think is “better”, which looks more “elegant”, which is more “efficient”?

First let me show you the pictures of the quiz:

fibo3

fibo4fibo5

FIRST TO DO IN THIS #QUIZ08 with the recursion solution is writing a function called ‘fibonacci(int n)’ with an int parameter called n where this parameter is the value of the list in the serie.

Inside of this function, we have a condition a #Mastery10 use of the if statement. This command allows the condition if the value of n is equal to 0 or 1 when this condition will be true, the tasks inside of the statement will execute. In this case if the value of n is equal than 0 or 1 this will occur:

return n; //With this command you return the value of the int variable n in order to show it in THE MAIN FUNCTION

return n; //With this command you return the value of the int variable n in order to show it in THE MAIN FUNCTION

After that, we use a #Mastery11 Use of else with a conditional if. if the condition does not achieve on the other hand you will do this:

return fibonacci(n-2) + fibonacci(n-1);

With this command you return the value of the previus operations of «fibonacci» function inside the function where is decreasing the value of n by 2, plus the function where is jumping the value of n by 1each time that its called in order to call the fibonacci in THE MAIN FUNCTION .This is to fulfiled the #Mastery12 Use of recursion for repetitive algorithms.

if we assume that the these recursive calls work correctly, then it is clear that we get the right result by adding them together.

Next, The execution of fibonacci  in the main functions begins with n=0, and since n is zero, it outputs the value 0, and then calls itself from the main to increase the values of fibonacci’s serie 10 times with calling the function i times with cout<<fibonacci(i);.

for(int i=0; i<=10;i++)
{
cout<<fibonacci(i); //THIS ASSIGNATION HELPS TO CALL THE FUNCTION OF "fibonacci" TO MAKE THE CALL IN THE TASK OF
//THE fibonacci SERIE OF THE NUMBER WITH THE HELP OF THE COMAND I' #Mastery06 and #Mastery07
cout <<" "<< n;


}

FIRST TO DO IN THIS #QUIZ08 with the LOOP solution is writing a function called ‘fibonacciLoop(int n)’ with an int parameter called n where this parameter is the value of the list in the serie.

Inside of this loop function, we need to declarate 2 int variables in order to show the current value of n and to save in the other variable the last value that took n. So, the variables are n,nbackup and fibolop inialized in 1. First, we show the value of n that starts from zero, then I use my variable nbackup to save the current value of fibolop that is 1, so now nbackup has the value of 1, next I need to change the value of fibolop into fibolop=fibolop+n; plus the value of n, so now fibolop has the value of 1 because right now n=0, next we give n=nbackup; the value of nbackup to n, so n now has the value of n=1, next it shows the value 1 and then I use my variable nbackup to save the current value of fibolop that is 1, so nbackup=1, next we have to change the value of fibolop into fibolop=fibolop+n; plus the value of n, that will be fibolop=2 because right now n=1, then I give the value of n=nbackup, so n is n=1 because right now nbackup=1, next it shows the value 1 in the list, then we use my variable nbackup to save the current value of fibolop that is 2, so nbackup=2;, next  I need to change the value of fibolop into fibolop=fibolop+n; plus the value of n, that would be 2+1=3 because n right now is n=1 so fibolop is fibolop=3. After that, I give the value of nbackup=2 to n, so n now has the value n=2 we would have something like this

0,1,1,2

Next, I give nbackup the value of the current value of fibolop, if at this moment fibolop=3 then nbackup is 3 nbackup=3, then I change the value of fibolop into fibolop=fibolop+n; plus the value of n, that will be fibolop=3+2=5 because at this moment n=2, next I need to give the current value of nbackup=3 to n, so n has the value of n=3 we would have something like this after showing the current value of n=3

0,1,1,2,3

Next, I give the number of the current fibolop to the variable nbackup, nbackup=fibolop, so at this moment now fibolop is fibolop=5 so nbackup has a current value of nbackup=5, next I need to change the value of fibolop into fibolop=fibolop+n; plus the value of n, that will be fibolop=5+3=8 because n is n=3 in this moment, next n now changes because I give the value of nbackup=5 to n, so now n has the value of n=5 we would have something like this after showing the current value of n=5

0,1,1,2,3,5

Next, I give the number of the current value of fibolop to the variable nbackup, nbackup=fibolop, so at this moment now nbackup=8, next I need to change the value of fibolop into fibolop=fibolop+n; plus the value of n, that will be fibolop=8+5=13  because n is n=5 in this moment, next n now changes because I give the value of nbackup=8 to n, so now n is n=8 we would have something like this after showing the current value of n=8

0,1,1,2,3,5,8

Next I give the value of nbackup the value of the greatest value of fibolop=13 to the variable nbakup=13 because nbackup=fibolop, next I need to change the value of fibolop into fibolop=fibolop+n;plus the value of n, that will be fibolop=13+8=21 because n is n=8 in this moment, next n now changes because I give the value of nbackup=13 to n, so now n is n=13 we would have something like this after showing the current value of n=8

0,1,1,2,3,5,8,13

Next I give the value of nbackup the value of the greatest fibolop to the value nbackup because nbackup=fibolop, so now nbackup=21 because currently fibolop is=21, next fibolop will change and I need to change the value of fibolop into fibolop=fibolop+n;plus the value of n,that will be fibolop=21+13=34 because n at this moment is n=13, next n now changes because I give the value of nbackup=21 to n, so now n is n=21, we would have something like this after showing the current value of n=21

0,1,1,2,3,5,8,13,21

So fibolop is always in front of n, fibolop has the greatest value and we need to save this value in another and print it after of showing the last values of the serie.If you change the order of the operations, the serie WILL NOT WORK.

REMEMBER that all these operations will execute the number of times that the loops says And these instructions are happening at the begining of the program until the loop finishes.

Which do you think is “better”, which looks more “elegant”, which is more “efficient”?

I think is “better” the recursive soultion because it saves memory and space rather than using a loop. In the loop solution you need to declarate 3 variables more than the recursive solution. Therefore, the recursive solutions has less operations because you just call the function the number of times to the definition of fibonacci series

fibonacci(0) = 1
fibonacci(1) = 1
fibonacci(n) = fibonacci(n − 1) + fibonacci(n − 2);

Futhermore, the more “elegant” is the recursive solution because it is more elegant to have your main program and just called it to do the task and just printed and in the loop solution you need to declarate more variables which can be more code and does not appear more elegant.

Futhermore, the more “efficient” solution is the recursive solution because it is more efficient to have your main program and just called it to do the task and just printed and in the loop solution you need to declarate more variables which can be more code and does not appear more efficient. Therefore, In this part you need to have in the main function the number of times that the operation is called and in the loop solution you have a more restricted condition in the loop to be repeated.

LINK OF THE CODE IN GITHUB: quiz8.cpp

ALSO THE CODE IS HERE:

#include <iostream>//Library to call all the
//fuctions of inputs and outputs of data in languague
//C++ #MasteryTopic01

using namespace std; //In C++ we need a command
// that everytime goes with the instructions of input and outputs
//of data that is call std but with this command helps writing
//these std out of the main because the machine factorizes them

int fibonacciLoop(int n){ //This is the structure of a function where you have subrutines in order to make
//different tasks, in this case we have an INT function with the name "fibonacciLoop" and with int parameter n in order to
//have int results inside this function.
n=0; // This command allows giving the value of the parameter 'n' to 0

int fibolop=1;
int nbackup;
// 0,1,1,2,3,5,8,13,21
for(int i=0; i<=10; i++) //With this command for is utilized as a loop FOR execute all the
//instruccions that has this loop until that does not complete that the variable i
// will be less or equal than 10 while it is increasing one in one in this variable i
//inicialized from the value 0
{
cout<<" "; //comand of out data in form of text space
cout<<n; // This command allows enter data for inputs. In this case this enters the
//value of variable 'n'

nbackup=fibolop; //before this operation, the value of fibolop won't increase until this next operation, RIGHT NOW IT stays fibolop=1

fibolop=fibolop+n;

n=nbackup;

}


}

int fibonacci(int n){ //This is the structure of a function where you have subrutines in order to make
//different tasks, in this case we have an INT function with the name "fibonacci" and with int parameter n in order to
//have int results inside this function.
if(n==0||n==1){ //#Mastery10 use of the if statement //This command allows the condition if the value of n
//is equal to 0 or 1 when this condition will be true, the tasks inside of the statement will execute.
// In this case if the value of n is equal than 0 or 1 this will occur:
return n; //With this command you return the value of the int variable n in order to show it in THE MAIN FUNCTION

} else { //#Mastery11 Use of else with a conditional if: // if the condition does not achieve on the other hand you will do this:
return fibonacci(n-2) + fibonacci(n-1); //With this command you return the value of the previus operations of "fibonacci" function
//inside the function where is decreasing the value of n by 2, plus the function where is jumping the value of n by 1
// each time that its called in order to callthe fibonacci in THE MAIN FUNCTION .This to fulfiled the #Mastery12 Use of recursion
//for repetitive algorithms
}
}



int main()
{

int n;
int fibolop;

fibolop=fibonacciLoop(n); //THIS ASSIGNATION HELPS TO CALL THE FUNCTION OF "fibonacciLoop" TO MAKE THE TASK OF
//THE fibonacci SERIE OF THE NUMBER WITH THE HELP OF THE VARIABLE int 'fibolop' #Mastery06 and #Mastery07


cout<<endl;

for(int i=0; i<=10;i++)
{
cout<<fibonacci(i); //THIS ASSIGNATION HELPS TO CALL THE FUNCTION OF "fibonacci" TO MAKE THE CALL IN THE TASK OF
//THE fibonacci SERIE OF THE NUMBER WITH THE HELP OF THE COMAND I' #Mastery06 and #Mastery07
cout <<" "<< n;


}

cout<<endl;


return 0;
}
Anuncio publicitario

2 comentarios en “#Quiz08 See the algorithm of Fibonacci serie explained!

Deja una respuesta

Introduce tus datos o haz clic en un icono para iniciar sesión:

Logo de WordPress.com

Estás comentando usando tu cuenta de WordPress.com. Salir /  Cambiar )

Imagen de Twitter

Estás comentando usando tu cuenta de Twitter. Salir /  Cambiar )

Foto de Facebook

Estás comentando usando tu cuenta de Facebook. Salir /  Cambiar )

Conectando a %s