Kamis, 10 November 2011

Program C++ Permutasi dan Kombinasi

  Agus Nur Ikhsan       Kamis, 10 November 2011
Well ... Now we will discuss about the permutations and combinations which are part of combinatorics. combination combinatorics in mathematics means the set of objects that are not concerned with the order. The combination of different permutations of the importance of the object sequence. Meanwhile, the permutation is a rearrangement of a collection of objects in a different order than the original sequence. For example, the words in the previous sentence can be rearranged as "is a permutation of a sequence of distinct sequence that was originally a collection of objects in the rearrangement." The process of returning the objects in a standard order (as per the provisions) is called sorting.
hmmm .. friends can see the algorithms and programs below:

Algoritma
Algoritma Permutasi
Deklarasi
n, r                 : integer (input)
x,y                  : integer
p                     : integer (output)

Deskripsi
read(n,r)
x = 1
y = 1
for i <= 1 to n do
x = x*i
for j ß 1 to n-r do
                   y = y*j
endfor
endfor
p = x/y
write (p)

Algoritma Kombinasi
Deklarasi
n, r                 : integer (input)
x,y,z               : integer
c                     : integer (output)

Deskripsi
read(n,r)
x = 1
y = 1
z = 1
a = n – r
for i ß 1 to n do
x = x*i
for j ß 1 to r do
                   y = y*j
for k ß 1 to a do
                         z = z*k
endfor
endfor
endfor
           c = x / (z*y)
write (c)

Berikut kode programnya :

#include <cstdlib>
#include <iostream>

using namespace std;
class matdis{
      friend istream& operator>>(istream&, matdis&);
      friend ostream& operator<<(ostream&, matdis&);
      public:
             matdis();
             int permutasi();
             int kombinasi();
             void pilihan();
             void exit();
      private:
              int n,r,x,y,z,a,p,c;
              };
      matdis::matdis(){
                       cout<<"\t==PROGRAM PERMUTASI DAN KOMBINASI=="<<endl;
                       cout<<"\t====SILAHKAN PILIH PILIHAN ANDA===="<<endl;
                       cout<<"\t========== 1. PERMUTASI ==========="<<endl;
                       cout<<"\t========== 2. COMBINASI ==========="<<endl;
                       cout<<"\t========== 0. EXIT      ==========="<<endl;
                       cout<<"\t========== TERIMA KASIH ==========="<<endl;
                       };
                      
      int matdis::permutasi(){
           cout<<"Program Menghitung Permutasi"<<endl;
           cout<<"Masukkan n harus lebih besar dari r :"<<endl;
           cout<<"Masukkan n :"; cin>>n;
           cout<<"Masukkan r :"; cin>>r;
           x=1;
           y=1;
           for(int i=1; i<=n; i++){
                   x=x*i;
                   }
           for(int j=1; j<=(n-r); j++){
                   y=y*j;
                   }
           p=x/y;
           cout<<"Nilai n ="<<n<<endl;
           cout<<"Nilai r ="<<r<<endl;
           cout<<"Nilai n! ="<<x<<endl;
           cout<<"Hasil (n-r)! ="<<y<<endl;
           cout<<"Hasil Permutasi "<<"("<<n<<","<<r<<") : "<<p<<endl;
           pilihan();
           return p;
           }
          
      int matdis::kombinasi(){
           cout<<"Program Menghitung Kombinasi"<<endl;
           cout<<"Masukkan n harus lebih besar dari r :"<<endl;
           cout<<"Masukkan n :"; cin>>n;
           cout<<"Masukkan r :"; cin>>r;
           x=1;
           y=1;
           z=1;
           a=(n-r);
           for(int i=1; i<=n; i++){
                   x=x*i;
                   }
           for(int j=1; j<=r; j++){
                   y=y*j;
                   }
           for(int k=1; k<=a; k++){
                   z=z*k;
                   }
           c=x/(z*y);
           cout<<"Nilai n ="<<n<<endl;
           cout<<"Nilai r ="<<r<<endl;
           cout<<"Nilai n! ="<<x<<endl;
           cout<<"Hasil r! ="<<y<<endl;
           cout<<"Nilai (n-r)! ="<<z<<endl;
           cout<<"Hasil Kombinasi "<<"("<<n<<","<<r<<") : "<<c<<endl;
           pilihan();
           return c;
           }
      void matdis::pilihan(){
                int pil;
                cout<<endl;
                cout<<"masukan pilihan anda = ";
                cin>>pil;
                switch(pil){
                 case 1 : permutasi ();break;
                 case 2 : kombinasi ();break;
                 case 0 : exit (); break;
                 default :
                         cout<<"Maaf anda salah tulis"<<endl;
                         cout<<"Pilih kembali Pilihan "<<endl;
                         pilihan();
                          break;      
                 }}
      void matdis::exit(){
                          cout << "TERIMA KASIH"<<endl;
                          }

int main(int argc, char *argv[])
{
    matdis x;
    x.pilihan();
    system("PAUSE");
    return EXIT_SUCCESS;
}


explanation:
The above program is a program to compute combinations and permutations. Which we also need to know about the factorial because it is closely connected with combinatorics. In this program uses a for loop to solve this problem. Thank you and may be useful.
logoblog

Thanks for reading Program C++ Permutasi dan Kombinasi

Previous
« Prev Post

Tidak ada komentar:

Posting Komentar