Warning: The magic method Slickr_Flickr_Plugin::__wakeup() must have public visibility in /home/kenbauer/public_kenscourses/tc101fall2015/wp-content/plugins/slickr-flickr/classes/class-plugin.php on line 152
‘#mastery21’ Articles at TC101 Fall 2015, Page 2
Introduction to Programming Python and C++

Tag Archives: #mastery21

#Masteries 21 & 22

Mastery 21. Use of recursion for repetitive algorithms… Mastery 22. When to use what type of repetition in a program… And here my video:

#Mastery21 – Use of recursion for repetitive algorithms

Hello this is about using recursion in algorithms. A recursion refers to the action of when a function calls itself. It is kind of like creating a loop.

96

Normal
0

21

false
false
false

ES-TRAD
X-NONE
X-NONE

/* Style Definitions */
table.MsoNormalTable
{mso-style-name:”Tabla normal”;
mso-tstyle-rowband-size:0;
mso-tstyle-colband-size:0;
mso-style-noshow:yes;
mso-style-priority:99;
mso-style-parent:””;
mso-padding-alt:0cm 5.4pt 0cm 5.4pt;
mso-para-margin:0cm;
mso-para-margin-bottom:.0001pt;
mso-pagination:widow-orphan;
font-size:12.0pt;
font-family:Calibri;
mso-ascii-font-family:Calibri;
mso-ascii-theme-font:minor-latin;
mso-hansi-font-family:Calibri;
mso-hansi-theme-font:minor-latin;
mso-fareast-language:EN-US;}

Why? Calling the function inside the function creates a series of events which lead to the result that is the same  using a lop with while or for.

 

  • In recursion, a function calls itself but you shouldn’t assume these two functions are same function. 
  • Local variables are defined inside a function and has scope only inside that function. In recursion, a function call itself.

 

Recursion makes it easier to express ideas in which the result of the recursive needs to complete the task.

WATCH MY VIDEO https://youtu.be/i9wo3N8tNzk

Here are masteries 17 and 21. Actually easy for me. I learned…

Here are masteries 17 and 21. Actually easy for me. I learned how to use “switch” to do this mastery and it was easy to use and useful for me. Enjoy: https://www.youtube.com/watch?v=zr2WxayvjoI

Mastery 21 – Recursion

http://www.python-course.eu/recursive_functions.php

#Mastery21

Use of recursion for repetitive algorithms

Mastery 21

On this mastery I will show you what is recursion in C++ and how to use it.

Recursion is a form where a function calls itself. When you call the same function, you are doing a loop, and you have to give a condition in order to make this loop stop.

Let’s create the same program we did on mastery 20 but now using recursion and a function.

So, do as follows:

  1. Create a function of type long (this allows us to have bigger outputs of the numbers) and add a n variable to it.
  2. Create an if and else if condition as we learn on masteries 15 & 16
  3. If n equals to 0, factorial = 1. (That’s a rule)
  4. Else if, n multiplied by factorial of (n-1). [This is the recursion part of the program, we are calling the function inside the function n times and subtracting 1 every time the loop is done, and it will stop until n equals to 0.]
  5. Inside int main (), declare a variable and ask the user for it, then simply call the function.

Your program should look something like this:

image

Now let’s test the program. Factorial of 5 should be equal to 120.

image

And that’s about it, you just learned how to use recursion with a function, congrats!

Mastery 21: Use of recursion for repetitive algorithms

Hey, here is the video: https://youtu.be/3k9CgjcuUwg
Thanks for watching ! n.n

MASTERY 21: Use of recursion for repetitive algorithms

(Credit of the image goes to https://flic.kr/p/4AvpZH) Mastery #21 is about using recursion and here is a Power Point explaining what is recursion and how to use it. I hope you find it useful!!! MASTERY 21

MASTERY 21: Use of recursion for repetitive algorithms

Mastery #21 is about using recursion and here is a Power Point explaining what is recursion and how to use it.
I hope you find it useful!!!
MASTERY 21

Recursion

       In some programs we need to get a result that consists on repeating the same algorithm certain amount of times. For obtaining the result we need to obtain the result of the same problem in smaller instances.

For example:

       We want to obtain the result of certain number raised to another number. This is the same as multiplying the first number by itself during the second number of times. 


      But also, this is the same as multiplying the first number times the first number raised to second number minus 1.



For example:

     5^3 is the same as:

     5*5*5

     Which is the same as:

     5*5^2 which is the same as: 5*5*5^1 which is the same as 5*5*5*5^1

Now we just need to know what 5^1 is equal to.


     What we just did is what we call recursion. We want to obtain the result of a problem by solving the same problem in smaller instances. It can be applied to code in this way:



       And it gives us the same answer:


       Another example is a number in the Fibonacci series.


       This series start with the value “0”, followed by “1”. After that, every number is equal to the sum of the two previous numbers. This means that the third number will be equal to 0+1 which is 1. And the fourth number will be 1+1 which is 2.


      This can be solved used recursion.


      If we get asked for the tenth number in the Fibonacci series, this will be the sum of the ninth and the eight numbers.
     And the ninth number is the sum of the eight and the seventh.
     The eight is the sum of the seventh and the sixth…
    And so on until we get to the first two number, that are the ones that we actually know.

Recursion


Here is the series so you can prove the answer is correct

( 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89 )

What should you work on?

Week #12 and more partial exams for you.

For this week's readings:
C++ (TC1017) should either be looking at support for your project, ImageMagick C++ libraries are a good start.
Python (TC1014) should be finishing chapter 11 (Dictionaries).