Sabtu, 25 Juni 2011

Program C++ Deret Fibonacci


At this time we will learn to make a program about the Fibonacci series. Previously I will tell you about the Fibonacci series.
The Fibonacci sequence was discovered by Leonardi Pisano or better known as Leonardo Fibonacci (derived from Filius Bonaccio or child of Bonaccio, the designation for the father whose real name is Guglielmo), in the 12th century in Italy. Basically, the Fibonacci series is a simple number sequence starting from 0 and 1 and the subsequent interest is the sum of the two previous numbers. Fibonacci sequence is recursive because it uses terms in the series to calculate the rate thereafter. By definition, then the tribes on the Fibonacci series is:

0 1 1 2 3 5 8 13 21 34 55 89 144 and so on.
The ratio of a pair of successive interest in the Fibonacci series will converge to an irrational number 1.618 or phi (Φ). Phi is an irrational constant value 1.61803399 ... in the can from the ratio kenvergensi tribe against tribe in the Fibonacci series sbelumnya. In the Fibonacci series, a tribe is the sum of the two tribes before. Known ratio of two consecutive rate converges to a value, consider the value of the variable p. So the sequence of interest is very large, eg 3 consecutive rate is denoted as a, b, and c, then applies:


c / b = b / a = p; with c = a + b

-> (A + b) / b = b / a;

-> A ^ 2 + ab = b ^ 2;

-> A ^ 2 + ab-b ^ 2 = 0; press. quadratic

-> Then obtained a / b = (1 + √ 5) / 2 or a / b = (1 - √ 5) / 2

-> If calculated, (1 + √ 5) / 2 is equivalent to 1.618 ... while (1 - √ 5) / 2 is equivalent to 0.618 .... Since a1, 618 ...


Thus the number phi has properties, a number which is the number itself resiproknya minus 1. (1/phi = phi-1).
Numbers Phi is said by experts as the divine proportion or the proportion of noble or dalah term more popularly known sebagain golden ratio. Divine proportion was as if God put it into his creation to prove his greatness by the beauty of nature. There are so many examples of the appearance of golden ratio in the universe, ranging from the finger that we use for typing, outer space until there.
 
Algoritma
Deklarasi
n                          : integer             (input)
fibbonaci             : integer             (output)
Deskripsi
read(n)
if (n = 1) or (n = 2) then
fibbonaci <= 1
else
fibbonaci <= fibbonaci(n-1) + fibbonaci(n-2)
endif
write(fibbonaci)

Berikut kode programnya :

#include <cstdlib>
#include <iostream>

using namespace std;
class fibonacci{
      friend istream& operator>>(istream&, fibonacci&);
      friend ostream& operator<<(ostream&, fibonacci&);
      public:
             fibonacci();
             void proses();
             void jumlah();
      private:
              int x[100];
              int a,hasil;
              };
      istream& operator>>(istream& mlebu, fibonacci& s){
               cout<<"Masukkan Banyak Suku :"; mlebu>>s.a;
               }
      ostream& operator<<(ostream& metu, fibonacci& v){
               metu<<"Suku Fibonacci :"<<v.a<<endl;
               metu<<"Deret Fibonacci :";
               for(int i=0; i<v.a; i++){
                       metu<<v.x[i]<<",";
                       }
               metu<<endl;
               metu<<"Hasil Jumlah Deret Fibonacci :"<<v.hasil<<endl;
               }
      fibonacci::fibonacci(){
                             cout<<"\t\t<< PROGRAM MENCETAK DAN MENGHITUNG DERET FIBONACCI >>"<<endl;
                             }
      void fibonacci::proses(){
           if(a==1) hasil=1;
           else if(a==2) hasil=1;
           else
           x[0]=1;
           x[1]=1;
           for(int i=2; i<a; i++){
                   x[i]=x[i-1]+x[i-2];
                   }
                   }
      void fibonacci::jumlah(){
           hasil=0;
            for(int i=0; i<a; i++){
                  hasil=hasil+(x[i]);
                  }
                  }
int main(int argc, char *argv[])
{
    fibonacci x;
    cin>>x;
    x.proses();
    x.jumlah();
    cout<<x;
    system("PAUSE");
    return EXIT_SUCCESS;
}
  
explanation:
Above program is a program to calculate the Fibonacci sequence is by using a for loop and the selection / conditioning if .. else. And the Fibonacci sequence is one example of a recursive program by having the two terms are penyetop cases and cases of a function call itself can be seen in the algorithm above.

1 komentar:

newbie guba mengatakan...

kurang jelas penjelasanya..
info seputar bola, prediksi bola ter-update..
berita bola TERBARU DAN TERPERCAYA hanya ada di www.bola368.net

Poskan Komentar

Template by:

Free Blog Templates