Kumpulan Program Sederhana

Minggu, 24 April 2011

Rekursif

Recursive subroutine is an ability to call itself. On some issues, like it very dberguna ability to facilitate solutions. However, recursion also has a weakness, which allows the overflow on the stack (stack is no longer able to handle the request because the calling subroutine runs out of memory). while the meaning of the stack is an area of memory used for local variables and to allocate the memory when a function is called.



     example of a recursive program that is:
           
Yn with n integer greater than zero can be calculated recursively by using the reference as follows:
  • Yn, for n = 1  
  • Y X Yn-1, for n 1  
form algorithm:

             Subroutine rank (y, n)
             IF n = 1 THEN
             BEHIND THE VALUE-y
             OTHERWISE
             VALUE BEYOND y-x rank (y, n-1)
             END-IF
             END-subroutine:

in program:
#include <iostream.h>
#include <conio.h>

 long int pangkat (unsigned int y, unsigned int n)
{
   if (n == 1)
       return y;
  else
      return y * pangkat (y, n-1);
 }

 int main ()
{
  int y, n;
 long int hasil;
 cout <<"mengitung y pangkat ^n \n";
 cout <<" y =";
 cin >> y;
 cout<<" n =";
 cin >> n;
 hasil = pangkat (y,n)
 cout << y <<"^" << n << " =" <<hasil;

getch ();
return 0;
}

Tidak ada komentar:

Posting Komentar