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.

Jumat, 24 Juni 2011

Program C++ tentang Pointer


Hmmm ... now we will learn about pointers. My friend told me pointers were easy and they are many like this ... hehehe .. Pointer is one type of structured data. By using a pointer, a variable can be created or deleted during the execution of the program, => variable (object) dynamically. To create a dynamic data structure: a link list, queue, stack, tree => size to be flexible but just try my friends clay directly ... example program. Okee;)

Algoritma :
Deklarasi
data                       : integer
*berikut                 : pointer

Deklarasi
read(data)
node*baru
baru=new node
baru->data=5
baru->berikut=NULL
write (baru->data)
//untuk menyisipkan data selanjutnya buat node baru dengan cara yang sama.

Berikut kode programnya :

#include <iostream.h>
#include <conio.h>

class node{
public:
int data;
node*berikut;
};

void main()
{
//langkah 1
node*baru;
baru=new node;
baru->data=5;
baru->berikut=NULL;
cout << "isi data node baru adalah "<<baru->data<<endl;

//langkah 2
node*lain;
lain=new node;
lain->data=6;
lain->berikut=NULL;
cout << "isi data node lain adalah "<<lain->data<<endl;

//langkah 3
baru->berikut=lain;
cout << "isi node lain dicetak dari node baru adalah ";
cout << baru->berikut->data<<endl;

//langkah 4
node*kepala=baru;
cout << "mencetak node pertama dari pointer kepala ";
cout << kepala->data<<endl;
cout << "mencetak node kedua dari pointer kepala : ";
cout << kepala->berikut->data<<endl;

//lagkah 5: jalankan pointer
cout << "menggunakan perulangan untuk mencetak setiap data pada rantai\n";
node*jalan=kepala;
int i=1;
while(jalan!=NULL)
{
cout << "data ke"<<i<<">"<<jalan->data<<endl;
i++;
jalan=jalan->berikut;
}
//langkah 6: bukti bahwa pointer kepala tidak kehilangan data
cout << "mencetak node pertama dari pointer kepala : ";
cout << kepala->data<<endl;
cout << "mencetak node kedua dari pointer kepala : ";
cout << kepala->berikut->data<<endl;
}


explanation:
The above program is an implementation of a pointer in this program we insert new data in front, behind or in the middle. Friends can learn the program above.


Template by:

Free Blog Templates