Jumat, 08 Juli 2011

Algoritma dan Program Mencari Permutasi dan Kombinasi

Algoritma mencari permutasi

Deklarasi
n,r,permutasi   : int
Deskripsi
Masukkan ( n )
Masukkan ( r )If (n > r )
Permutasi=fak (n)/fak(n-r)
Tulis ( nilai permutasi )
Else if ( n < r )
Tulis ( data tidak valid )
End if
End if
End

2.     Algoritma mencari kombinasi

Deklarasi
n,r,kombinasi   : int
Deskripsi
Masukkan ( n )
Masukkan ( r )
If ( n > r )
Kombinasi=fak(n)/(fak(n-r)*fak (r))
Tulis ( nilai kombinasi )
Else if ( n < r )
Tulis ( data tidak valid )
End if
End if
End

Untuk Implementasi dalam Program C++ adalah sebagai berikut :

01#include <iostream.h>
02#include <stdio.h>
03#include <conio.h>
04long int fak (long int x);
05int main()
06 
07{
08int n,r, Prts,pilihan,p;
09char ulang;
10do
11{
12 
13cout<<"PERHITUNGAN KOMBINASI DAN PERMUTASI"<<endl;
14cout<<"1. KOMBINASI"<<endl;
15cout<<"2. PERMUTASI"<<endl;
16cout<<"3. EXIT"<<endl;
17cout<<endl<<endl;
18cout<<"Pilihan Anda : ";cin>>pilihan;
19switch (pilihan)
20{
21case 1 :
22cout<<" Program Menghitung Kombinasi(nCr)";
23cout<<endl;
24cout<<" Masukkan Nilai n : "; cin>>n;
25cout<<endl;
26cout<<" Masukkan Nilai r : "; cin>>r;
27cout<<endl;
28if (n>r){
29Prts = fak(n)/(fak(n-r)*fak (r));
30cout<<endl;
31printf(" Kombinasinya adalah: %d",Prts);
32cout<<endl; }
33else if (n<r){
34cout<<"data tak valid";}
35cout<<endl ;
36break;
37case 2 :
38cout<<" Program Menghitung Permutasi(nPr)";
39cout<<endl;
40cout<<" Masukkan Nilai n : "; cin>>n;
41cout<<endl;
42cout<<" Masukkan Nilai r : "; cin>>r;
43cout<<endl;
44if (n>r){
45Prts = fak (n)/fak(n-r);
46cout<<endl;
47 
48cout<<" Permutasinya adalah: "<<Prts;
49cout<<endl; }
50else if (n<r){
51cout<<"data tak valid";}
52cout<<endl ;
53break;
54case 3 :
55cout<<"Exit"<<endl;
56break;
57default:
58cout<<"Menu tidak tersedia"<<endl;
59break;
60}
61cout<<"Kembali ke Menu Utama (y/n)?";cin>>ulang;
62}while(ulang == 'y');
63}
64 
65long int fak (long int n)
66{
67int f;
68if (n<=1)
69{
70f=1;
71}
72else
73{
74f=n*fak(n-1);
75}
76return (f);
77}

Algoritma dan Program untuk menentukan Graf / Simpul

Deklarasi :
procedure DLS (inputv: Point, level:integer)
kamus
p : point
q : antrian
Deskripsi :  
write(v)dikunjungi[v] ← true {array untuk menampung simpul yang sudah dikunjungi}level ß 1
while not level > 5 do
{ kunjungi semua simpul di level 1,panggil algoritma DFS}
if A[v,p] = 1 then {simpul v dan simpul p bertetangga}
if  not dikunjungi[p] then
DLS (p, level)
End if
End if
level ß level +1
end while
end

Implementasi Program dalam Bahasa C++

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

int main(){
int i,j,n;
char a[10];
cout<<"Program Pertemuan Simpul Graf\n";
cout<<"Banyak simpul: ";
cin>>n;

for(i=0;i<n;i++){
cout<<"Simpul ke-"<<i+1<<" : ";
cin>>a[i];}

cout<<"Hasilnya : ";
for(i=0;i<n;i++){
for(j=i+1;j<n;j++){
cout<<a[i]<<a[j];
cout<<", ";
}
}
getch();
 

Mencari deret Aritmatika

Algoritma :
Deklarasi
x : integer
y : integer
n : integer
i : integer

Deskripsi
Read x
Read y
Read n
for i write x+(i*y)
end for
end

Program Biasa :
01#include <iostream.h>
02#include <conio.h>
03int main(){
04int x,y,n;
05cout<<"Deret Aritmatika\n";
06cout<<"Masukkan batas deret aritmatika: ";
07cin>>n;
08cout<<"Masukan Bilangan pertama : ";
09cin>>x;
10cout<<"Masukan Beda: ";
11cin>>y;
12for (int i=0;i<n;i++)
13cout<<x+(i*y)<<" ";
14getch ();
15return 0;}
16 
17Program Class :
18#include <iostream.h>
19#include <conio.h>
20class aritmatika{
21public:
22void masukan();
23void proses();
24private:
25int x,y,n;};
26void aritmatika::masukan(){
27cout<<"Deret Aritmatika\n";
28cout<<"Masukkan batas deret aritmatika: ";
29cin>>n;
30cout<<"Masukan Bilangan pertama : ";
31cin>>x;
32cout<<"Masukan Beda: ";
33cin>>y;}
34void aritmatika::proses(){
35for (int i=0;i<n;i++)
36cout<<x+(i*y)<<" ";}
37int main(){
38aritmatika x;
39x.masukan();
40x.proses();
41getch ();
42return 0;}
43 
44Program Jeliot :
45import jeliot.io.*;
46public class MyClass {
47public static void main() {
48aritmatika x = new aritmatika();
49x.masukan();
50x.proses();}}
51class aritmatika{
52void masukan(){
53System.out.print("Deret Aritmatika\n");
54System.out.print("Masukkan batas deret aritmatika: ");
55n = Input.readInt();
56System.out.print("Masukan Bilangan pertama : ");
57x = Input.readInt();
58System.out.print("Masukan Beda: ");
59y = Input.readInt();}
60void proses(){
61for (int i =0;i<n;i++){
62System.out.print(x+(i *y));
63System.out.print(" ");}}
64private int x;
65private int y;
66private int n;}

Penjelasan :

Program di atas adalah program yang digunakan untuk mencari deret bilangan aritmatika. Apa itu bilangan aritmatika ? bilangan aritmatika adalah bilangan yang dimulai dengan angka, sebut saja x yang akan menjadi nilai pertamanya angka selanjutnya adalah angka x ditambah dengan selisih, angka selanjutnya adalah angka x yang sudah ditambah selisih kemudian ditambah dengan selisihnya lagi. Contoh 1,4,7,10 dalam kasus ini selisihnya 3. Langsung saja ke program di atas kita gunakan fungsi for atau perulangan untuk mengulang rumus agar dapat diperoleh hasil tiap deretnya, dimana rumusnya adalah x+(i*y), dimana x adalah bilangan pertama sebagai dasar, y sebagai selisihnya dan i sebagai bilangan pengkali dengan angka selisih, baru ditambah dengan nilai awal tadi.

Deret Aritmatika

Deret aritmatika dalam bidang matematika adalah urutan bilangan di mana bilangan berikutnya merupakan penambahan bilangan sebelumnya dengan suatu bilangan tertentu. Contohnya adalah 3,5,7,9,11,13, ….. Deret aritmatika ini dapat dinyatakan dengan rumus sebagai berikut:
a,a + b,a + 2b,a + 3b,…
Dalam hal ini suku ke-n:
 Jumlah semua suku:




Misalnya, jumlah semua suku dari suatu suku dengan bentuk an = 3 + 
(n-1)(5) sampai suku ke-50 adalah :



Untuk algoritma mencari deret aritmatika adalah sebagai berikut:

Deklarasia, b, n, hasil, jumlah : integer
Deskripsi
Read ( a, b, n )
hasil = a +(b*(n-1))
jumlah = (a + hasil )* 1/2*n
write (Suku ke %i )
write ( jumlah suku pertama )
Sedangkan penerapan dalam bahasa C++ adalah seperti berikut:

01#include <cstdlib>
02#include <iostream>
03 
04using namespace std;
05class deret{
06      public:
07             void aritmatika();
08 
09      private:
10              int a,b,n,hasil;
11};
12 
13void deret::aritmatika(){
14     cout<<"Masukkan Suku Pertama : ";
15     cin>>a;
16     cout<<"Masukkan Beda : ";
17     cin>>b;
18     cout<<"Masukkan Banyak Suku : ";
19     cin>>n;
20     hasil = a +(b*(n-1));
21     cout<<"Suku ke n = "<<hasil;
22     cout<<endl;
23}
24 
25int main(int argc, char *argv[])
26{
27    deret X;
28    X.aritmatika();
29    system("PAUSE");
30    return EXIT_SUCCESS;
31}

Operator overloading,operator tambah C++

Berikut ini adalah salah satu contoh program overloading operator tambah dalam bahasa C++.
#include<iostream.h>
#include<iostream.h>

class Buah
{
private:
int apel;
int jeruk;
public:
Buah(int jum_apel=0,int jum_jeruk=0);
void info_buah();
Buah operator+(Buah b2);
Buah operator+(int tambahan);
};
void main()
{
clrscr();
Buah buah1 (20,5);
Buah buah2;

cout<}
Buah Buah::operator+(Buah b2)
{
Buah tmp;
tmp.apel=apel+b2.apel;
tmp.jeruk=jeruk+b2.jeruk;
return(tmp);
}
Buah Buah::operator+(int tambahan)
{
Buah tmp;
tmp.apel=apel+tambahan;
tmp.jeruk=jeruk+tambahan;
return(tmp);
}<

This Example Programs Calculate Prime Number Using Constructor

ALGORITHM:
STEP 1: Start the program.
STEP 2: Declare the class as Prime with data members,
Member functions.
STEP 3: Consider the argument constructor Prime() with integer
Argument.
STEP 4: To cal the function calculate() and do the following steps.
STEP 5: For i=2 to a/2 do
STEP 6: Check if a%i==0 then set k=0 and break.
STEP 7: Else set k value as 1.
STEP 8: Increment the value i as 1.
STEP 9: Check whether the k value is 1 or 0.
STEP 10:If it is 1 then display the value is a prime number.
STEP 11:Else display the value is not prime.
STEP 12:Stop the program.

C++ PROGRAM:
01#include<iostream.h>
02#include<conio.h>
03class prime
04{
05                int a,k,i;
06              public:
07              prime(int x)
08              {
09                            a=x;
10              }
11              void calculate()
12              {
13                 k=1;
14                {
15                     for(i=2;i<=a/2;i++)
16 
17       if(a%i==0)
18                     {
19                              k=0;
20                              break;
21                     }
22                     else
23                    {
24                            k=1;
25                  }
26                }
27              }
28 
29void show()
30              {
31                if(k==1)
32                  cout<< “\n\tA is prime Number. ";
33                else
34                  cout<<"\n\tA is Not prime.";
35              }
36};
37 
38void main()
39{
40    clrscr();
41    int a;
42    cout<<"\n\tEnter the Number:";
43    cin>>a;
44    prime obj(a);
45    obj.calculate();
46    obj.show();
47    getch();
48}
Sample Output:
Enter the number: 7
Given number is Prime Number

To calculate factorial of a given number using copy constructor.


ALGORITHM:
STEP 1: Start the program.
STEP 2: Declare the class name as Copy with data members and member functions.
STEP 3: The constructor Copy() with argument to assign the value.
STEP 4: To cal the function calculate() do the following steps.
STEP 5: For i=1 to var do
STEP 6: Calculate fact*i to assign to fact.
STEP 7: Increment the value as 1.
STEP 8: Return the value fact.
STEP 9: Print the result.
STEP 10: Stop the program.

PROGRAM:
01#include<iostream.h>
02#include<conio.h>
03class copy
04{
05              int var,fact;
06              public:
07 
08                copy(int temp)
09                {
10                 var = temp;
11                }
12 
13                double calculate()
14                {
15                            fact=1;
16                            for(int i=1;i<=var;i++)
17                            {
18                            fact = fact * i;
19                            }
20                            return fact;
21                }
22};
23void main()
24{
25    clrscr();
26    int n;
27    cout<<"\n\tEnter the Number : ";
28    cin>>n;
29    copy obj(n);
30    copy cpy=obj;
31    cout<<"\n\t"<<n<<" Factorial is:"<<obj.calculate();
32    cout<<"\n\t"<<n<<" Factorial is:"<<cpy.calculate();
33    getch();
34}
Output:
Enter the Number: 5
Factorial is: 120
Factorial is: 120

Perform exception handling for Divide by zero Exception

perform exception handling for Divide by zero Exception.
ALGORITHM:
Step 1: Start the program.
Step 2: Declare the variables a,b,c.
Step 3: Read the values a,b,c,.
Step 4: Inside the try block check the condition.
a. if(a-b!=0) then calculate the value of d and display.
b. otherwise throw the exception.
Step 5: Catch the exception and display the appropriate message.
Step 6: Stop the program.

PROGRAM:
01#include<iostream.h>
02#include<conio.h>
03void main()
04{
05   int a,b,c;
06   float  d;
07   clrscr();
08   cout<<"Enter the value of a:";
09   cin>>a;
10   cout<<"Enter the value of b:";
11   cin>>b;
12   cout<<"Enter the value of c:";
13   cin>>c;
14 
15   try
16   {
17              if((a-b)!=0)
18              {
19                 d=c/(a-b);
20                 cout<<"Result is:"<<d;
21              }
22              else
23              {
24                 throw(a-b);
25              }
26   }
27 
28   catch(int i)
29   {
30              cout<<"Answer is infinite because a-b is:"<<i;
31   }
32 
33   getch();
34}
Output:
Enter the value for a: 20
Enter the value for b: 20
Enter the value for c: 40

To perform exception handling with multiple catch.

ALGORITHM:
Step 1: Start the program.
Step 2: Declare and define the function test().
Step 3: Within the try block check whether the value is greater than zero or not.
a. if the value greater than zero throw the value and catch the corresponding exception.
b. Otherwise throw the character and catch the corresponding exception.
Step 4: Read the integer and character values for the function test().
Step 5: Stop the program.

PROGRAM:
01#include<iostream.h>
02#include<conio.h>
03void test(int x)
04{
05   try
06   {
07              if(x>0)
08                 throw x;
09        else
10                 throw 'x';
11   }
12
13   catch(int x)
14   {
15              cout<<"Catch a integer and that integer is:"<<x;
16   }
17
18   catch(char x)
19   {
20              cout<<"Catch a character and that character is:"<<x;
21   }
22}
23
24void main()
25{
26   clrscr();
27   cout<<"Testing multiple catches\n:";
28   test(10);
29   test(0);
30   getch();
31}
Output:
Testing multiple catches
Catch a integer and that integer is: 10
Catch a character and that character is: x

To find the mean value of a given number using friend function.

ALGORITHM:
STEP 1: Start the program.
STEP 2: Declare the class name as Base with data members and member functions.
STEP 3: The function get() is used to read the 2 inputs from the user.
STEP 4: Declare the friend function mean(base ob) inside the class.
STEP 5: Outside the class to define the friend function and do the following.
STEP 6: Return the mean value (ob.val1+ob.val2)/2 as a float.
STEP 7: Stop the program.

PROGRAM:
01#include<iostream.h>
02#include<conio.h>
03class  base
04{
05    int val1,val2;
06   public:
07    void get()
08    {
09       cout<<"Enter two values:";
10       cin>>val1>>val2;
11    }
12    friend float mean(base ob);
13};
14float mean(base ob)
15{
16   return float(ob.val1+ob.val2)/2;
17}
18void main()
19{
20    clrscr();
21    base obj;
22    obj.get();
23    cout<<"\n Mean value is : "<<mean(obj);
24    getch();
25}  

Output:
Enter two values: 10, 20
Mean Value is: 15

To calculate the area of circle, rectangle and triangle using function overloading. July 8, 2011

ALGORITHM:
STEP 1: Start the program.
STEP 2: Declare the class name as fn with data members and member functions.
STEP 3: Read the choice from the user.
STEP 4: Choice=1 then go to the step 5.
STEP 5: The function area() to find area of circle with one integer argument.
STEP 6: Choice=2 then go to the step 7.
STEP 7: The function area() to find area of rectangle with two integer argument.
STEP 8: Choice=3 then go to the step 9.
STEP 9: The function area() to find area of triangle with three arguments, two as Integer and one as float.
STEP 10: Choice=4 then stop the program.

PROGRAM:
01#include<iostream.h>
02#include<stdlib.h>
03#include<conio.h>
04#define pi 3.14
05class fn
06{
07      public:
08        void area(int); //circle
09        void area(int,int); //rectangle
10        void area(float ,int,int);  //triangle
11};
12 
13void fn::area(int a)
14{
15      cout<<"Area of Circle:"<<pi*a*a;
16}
17void fn::area(int a,int b)
18{
19      cout<<"Area of rectangle:"<<a*b;
20}
21void fn::area(float t,int a,int b)
22{
23      cout<<"Area of triangle:"<<t*a*b;
24}
25 
26void main()
27{
28     int ch;
29     int a,b,r;
30     clrscr();
31     fn obj;
32     cout<<"\n\t\tFunction Overloading";
33     cout<<"\n1.Area of Circle\n2.Area of Rectangle\n3.Area of Triangle\n4.Exit\n:”;
34     cout<<”Enter your Choice:";
35     cin>>ch;
36 
37     switch(ch)
38     {
39              case 1:
40                cout<<"Enter Radious of the Circle:";
41                cin>>r;
42                obj.area(r);
43                break;
44              case 2:
45                cout<<"Enter Sides of the Rectangle:";
46                cin>>a>>b;
47                obj.area(a,b);
48                break;
49              case 3:
50                cout<<"Enter Sides of the Triangle:";
51                cin>>a>>b;
52                obj.area(0.5,a,b);
53                break;
54              case 4:
55                exit(0);
56     }
57getch();
58}      

Output:
Function Overloading
1. Area of Circle
2. Area of Rectangle
3. Area of Triangle
4. Exit
Enter Your Choice: 2
Enter the Sides of the Rectangle: 5 5
Area of Rectangle is: 25
1. Area of Circle
2. Area of Rectangle
3. Area of Triangle
4. Exit
Enter Your Choice: 4