Sabtu, 18 Juni 2011

Combinations and Permutations

COMBINATION
The difference between permutation and combination problem lies in "the order or the position of" preparation of a group of objects. In a permutation problem of sequence or position to be very important, whereas in combination are not concerned with the order or position of the group object.
On the permutation sequence of objects XYZ; XZY; ZYX is different, but for the combination of these sequences are considered equal. Thus the combination of the method of electing the object in question without taking into consideration the sequence of the object. To calculate the number of combinations of objects can be formulated: nCx = (n!) / (x! (nx)!), Where n: number of all objects that exist, and x: the number of objects in combination.
Value of x <n and if x = n formulation into nCn = 1.


PERMUTATIONS Permutation is a compilation of existing objects into a specific sequence. Things to note in the permutation is that the objects that exist must be "indistinguishable" from each other. Permutation can be formulated: nPx = (n!) / (n-x)! ; Where n = number of all objects, and x = number of objects dipermutasikan. Values ​​of n and x each must be greater than zero. If the value of x <n is called a permutation of Part Object. If the value of x = n, then it is called permutation Whole Object, so the formula can be simplified into: nPx = n! .

The following is the sourcecode of the program combinations and permutations:

#include <iostream.h>
#include <stdio.h>
#include <conio.h>
long int fak (long int x);
void main()
{
int n,r, Prts,pilihan,p;
char ulang;
do
{
clrscr();
cout<<"KOMBINASI DAN PERMUTASI"<<endl;
cout<<"1. KOMBINASI"<<endl;
cout<<"2. PERMUTASI"<<endl;
cout<<"3. KELUAR"<<endl;
cout<<"Pilihan Anda : ";cin>>pilihan;
switch (pilihan)
{
case 1 :
cout<<" Program Menghitung Kombinasi";
cout<<endl;
cout<<" Masukkan Nilai n : "; cin>>n;
cout<<" Masukkan Nilai r : "; cin>>r;
if (n>r){
Prts = fak(n)/(fak(n-r)*fak (r));
cout<<endl;
printf(" Kombinasinya adalah: %d",Prts);
cout<<endl; }
else if (n<r){
cout<<"data tak valid";}
cout<<endl ;
break;
case 2 :
cout<<" Program Menghitung Permutasi";
cout<<endl;
cout<<" Masukkan Nilai n : "; cin>>n;
cout<<" Masukkan Nilai r : "; cin>>r;
if (n>r){
Prts = fak (n)/fak(n-r);
cout<<endl;

printf(" Permutasinya adalah: %d",Prts);
cout<<endl; }
else if (n<r){
cout<<"data tak valid";}
cout<<endl ;
break;
case 3 :
cout<<"KELUAR"<<endl;
break;
default:
cout<<"Menu tidak tersedia"<<endl;
break;
}
cout<<"Kembali ke Menu Utama (y/n)?";cin>>ulang;
}while(ulang == 'y');
}
getch();
long int fak (long int n)
{
int f;
if (n<=1)
{
f=1;
}
else
{
f=n*fak(n-1);
}
return (f);
}
 
The following display after the writing on the Borland C + + :
Results after the run:
Combinations Program

Permutations Program

0 komentar:

Posting Komentar