Jumat, 08 Juli 2011

To swap the numbers using the concept of function template

ALGORITHM:
STEP 1: Start the program.
STEP 2: Declare the template class.
STEP 3: Declare and define the functions to swap the values.
STEP 4: Declare and define the functions to get the values.
STEP 5: Read the values and call the corresponding functions.
STEP6: Display the results.
STEP 7: Stop the program.
PROGRAM:
01#include<iostream.h>
02#include<conio.h>
03 
04template<class t>
05 
06void swap(t &x,t &y)
07{
08   t temp=x;
09   x=y;
10   y=temp;
11}
12 
13void fun(int a,int b,float c,float d)
14{
15   cout<<"\na and b before swaping :"<<a<<"\t"<<b;
16   swap(a,b);
17   cout<<"\na and b after swaping  :"<<a<<"\t"<<b;
18   cout<<"\n\nc and d before swaping :"<<c<<"\t"<<d;
19   swap(c,d);
20   cout<<"\nc and d after swaping  :"<<c<<"\t"<<d;
21}
22 
23void main()
24{
25    int a,b;
26    float c,d;
27    clrscr();
28    cout<<"Enter A,B values(integer):";
29    cin>>a>>b;
30    cout<<"Enter C,D values(float):";
31    cin>>c>>d;
32    fun(a,b,c,d);
33    getch();
34}

Output:
Enter A, B values (integer): 10 20
Enter C, D values (float): 2.50 10.80
A and B before swapping: 10 20
A and B after swapping: 20 10
C and D before swapping: 2.50 10.80
C and D after swapping: 10.80 2.50

To write a program to find the multiplication values and the cubic values using inline function. July 8, 2011

ALGORITHM:
Step 1: Start the pogram.
Step 2: Declare the class.
Step 3: Declare and define the inline function for multiplication and cube.
Step 4: Declare the class object and variables.
Step 5: Read two values.
Step 6: Call the multiplication and cubic functions using class objects.
Step 7: Return the values.
Step 8: Display.
Step 9: Stop the program.
PROGRAM:
01#include<iostream.h>
02#include<conio.h>
03 
04class line
05{
06   public:
07              inline float mul(float x,float y)
08              {
09                            return(x*y);
10              }
11              inline float cube(float x)
12              {
13                            return(x*x*x);
14              }
15};
16 
17void main()
18{
19              line obj;
20              float val1,val2;
21              clrscr();
22              cout<<"Enter two values:";
23              cin>>val1>>val2;
24              cout<<"\nMultiplication value is:"<<obj.mul(val1,val2);
25              cout<<"\n\nCube value is          :"<<obj.cube(val1)<<"\t"<<obj.cube(val2);
26              getch();
27}

Output:
Enter two values: 5 7
Multiplication Value is: 35
Cube Value is: 25 and 343

To find out the student details using multiple inheritance

ALGORITHM:
Step 1: Start the program.
Step 2: Declare the base class student.
Step 3: Declare and define the function get() to get the student details.
Step 4: Declare the other class sports.
Step 5: Declare and define the function getsm() to read the sports mark.
Step 6: Create the class statement derived from student and sports.
Step 7: Declare and define the function display() to find out the total and average.
Step 8: Declare the derived class object,call the functions get(),getsm() and display().
Step 9: Stop the program.
PROGRAM:

01#include<iostream.h>
02#include<conio.h>
03 
04class student
05{
06    protected:
07       int rno,m1,m2;
08    public:
09                void get()
10              {
11                            cout<<"Enter the Roll no :";
12                            cin>>rno;
13                            cout<<"Enter the two marks   :";
14                            cin>>m1>>m2;
15              }
16};
17class sports
18{
19    protected:
20       int sm;                   // sm = Sports mark
21    public:
22                void getsm()
23              {
24                 cout<<"\nEnter the sports mark :";
25                 cin>>sm;
26 
27              }
28};
29class statement:public student,public sports
30{
31    int tot,avg;
32    public:
33    void display()
34              {
35                 tot=(m1+m2+sm);
36                 avg=tot/3;
37                 cout<<"\n\n\tRoll No    : "<<rno<<"\n\tTotal      : "<<tot;
38               cout<<"\n\tAverage    : "<<avg;
39              }
40};
41void main()
42{
43   clrscr();
44   statement obj;
45   obj.get();
46   obj.getsm();
47   obj.display();
48   getch();
49}
Output:
Enter the Roll no: 100
Enter two marks
90
80
Enter the Sports Mark: 90
Roll No: 100
Total : 260
Average: 86.66

Program to convert lowercase to uppercase

ALGORITHM:
STEP 1: Start the program.
STEP 2: Declare the variables.
STEP 3: Read the file name.
STEP 4: open the file to write the contents.
STEP 5: writing the file contents up to reach a particular condition.
STEP6: write the file contents as uppercase.
STEP7: open the file to read the contents.
STEP 8: Stop the program.
PROGRAM:

01#include<fstream.h>
02#include<stdio.h>
03#include<ctype.h>
04#include<string.h>
05#include<iostream.h>
06#include<conio.h>
07void main()
08{
09              char c,u;
10              char fname[10];
11              clrscr();
12              ofstream out;
13              cout<<"Enter File Name:";
14              cin>>fname;
15              out.open(fname);
16              cout<<"Enter the text(Enter # at end)\n";
17  //write contents to file
18              while((c=getchar())!='#')
19              {
20                            u=c-32;
21                            out<<u;
22              }
23              out.close();
24              ifstream in(fname);        //read the contents of file
25              cout<<"\n\n\t\tThe File contains\n\n";
26              while(in.eof()==0)
27              {
28                            in.get(c);
29                            cout<<c;
30              }
31              getch();
32 
33}

Output:
Enter File Name: two.txt
Enter contents to store in file (enter # at end)
oops programming
The File Contains
OOPS PROGRAMMING

Selasa, 24 Mei 2011

Jenis sorting


Metode Partisi Array dalam Quick sort.

Implementasi quick sort:

Algoritma :
            Subrutin quick_sort(L,p,r)
            jika p < r maka
            q = partisi (L,p,r)
            quick_sort(L,p,q)
            quick_sort(L,q,t,r)
            AKHIR-JIKA
            AKHIR-SUBRUTIN

Untuk mengurutkan isi keseliruhan larik L, diperlukan pemanggilan sebagai berikut :
                          quick_sort (L,0,jimlah_elemen (L)-1)
                        subrutin partisi sendiri sebagai berikut :
                                    Subritin partisi (L,p,r)
                        X L [p]
                        i p
                        Jr
                        ULANG SELAMA BENAR
                                    ULANG SELAMA L[ j]  > x
J  j-1
AKHIR-ULANG
ULANG SELAMA L[ i] < x
            ii + 1
AKHIR-ULANG
Jika r < J maka
// Tukaran L[ i] dengan L [ j]
Tmp = L[ i]
L[ i]  L [ j]
L [ j]  tmp
Sebaliknya
Nilai –balik j
Akhir-jika
Akhir-ulang
Akhir-subrutin


Metode Insertion Sort

Mirip dengan cara orang mengurutkan kartu, selembar demi selembar kartu diambil dan disisipkan (insert) ke tempat yang seharusnya. Pengurutan dimulai dari data ke-2 sampai dengan data terakhir, jika ditemukan data yang lebih kecil, maka akan ditempatkan (diinsert) diposisi yang seharusnya. Pada penyisipan elemen, maka elemen-elemen lain akan bergeser ke belakang


Source Code Insertion Sort :
void insertion_sort(int data[])
{
int temp;
for(int i=1;i<n;i++)
{
temp=data[i];
j=i-1;
while(data[j]>temp&&j>=0)
{
data[j+1]=data[j];
j–;
}
data[j+1]=temp;
}
}

Implementasi program Insertion Sort dalam bahasa C++ :

01#include <iostream.h>
02#include <conio.h>
03
04int data[10],data2[10];
05int n;
06
07void tukar(int a, int b)
08{
09 int t;
10 t = data[b];
11 data[b] = data[a];
12 data[a] = t;
13}
14
15void insertion_sort()
16{
17 int temp,i,j;
18 for(i=1;i<=n;i++)
19 {
20  temp = data[i];
21  j = i -1;
22  while(data[j]>temp && j>=0)
23  {
24   data[j+1] = data[j];
25   j--;
26  }
27 data[j+1] = temp;
28 }
29}
30int main()
31{
32 cout<<"===PROGRAM INSERTION SORT==="<<endl;
33
34 //Input Data
35 cout<<"Masukkan Jumlah Data : ";
36 cin>>n;
37 for(int i=1;i<=n;i++)
38 {
39  cout<<"Masukkan data ke "<<i<<" : ";
40  cin>>data[i];
41  data2[i]=data[i];
42 }
43
44 insertion_sort();
45
46 cout<<"\n\n";
47 //tampilkan data
48 cout<<"Data Setelah di Sort : ";
49 for(int i=1; i<=n; i++)
50 {
51  cout<<" "<<data[i];
52 }
53 cout<<"\n\nSorting Selesai";
54 getch();
55 return 0;
56}


Buble Sort

Metode buble merupakan metode sorting termudah. Diberi nama “Bubble” karena proses pengurutan secara berangsur-angsur bergerak/berpindah ke posisinya yang tepat, seperti gelembung yang keluar dari sebuah gelas bersoda. Bubble Sort mengurutkan data dengan cara membandingkan elemen sekarang dengan elemen berikutnya.
Pengurutan Ascending :Jika elemen sekarang lebih besar dari elemen berikutnya maka kedua elemen tersebut ditukar.
Pengurutan Descending: Jika elemen sekarang lebih kecil dari elemen berikutnya, maka kedua elemen tersebut ditukar.
Algoritma ini seolah-olah menggeser satu per satu elemen dari kanan ke kiri atau kiri ke kanan, tergantung jenis pengurutannya, asc atau desc. Ketika satu proses telah selesai, maka bubble sort akan mengulangi proses, demikian seterusnya sampai dengan iterasi sebanyak n-1.
Kapan berhentinya?  Bubble sort berhenti jika seluruh array telah diperiksa dan tidak ada pertukaran lagi yang bisa dilakukan, serta tercapai perurutan yang telah diinginkan
Contoh:
Urutkan data dari:
22 10 15 3 8 2
Pengecekan dapat dimulai dari data paling awal atau paling akhir. Pada contoh di bawah ini pengecekan dimulai dari data paling akhir. Data paling akhir dibandingkan dengan data di depannya, jika ternyata lebih kecil maka tukar. Dan pengecekan yang sama dilakukan sampai dengan data yang paling awal.

Kembali data paling akhir di bandingkan dengan data di depannya jika ternyata lebih kecil maka tukar, tetapi kali ini pengecekan di lakukan sampai data paling awal yaitu 2 karena data tersebut pasti merupakan data terkecil (didapat dari hasil pengurutan pada langkah ke 1)

Proses pengecekan pada langkah 3 dst. sama dengan langkah sebelumnya.

sourch code buble sort versi 1
untuk urutan ascending
 
void bubble_sort(int data[])
{
for (int i=1;i<n;i++)
{
for(int j=n-1;j>=1;j–)
{
if (data[j]<data[j-1])
tukar(&data[j],&data[j-1); //ascending
}
}
}


buble sort versi 2
untuk urutan descending
void bubblesort2(int data[])
{
for (int i=1;i {
for(int j=0;jdata[j+1])
tukar(&data[j],&data[j+1); //descending
}
}
}

Implementasi dalam bahasa Pemrograman C++

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

int data[10],data2[10];
int n;

void tukar(int a, int b)
{
 int t;
 t = data[b];
 data[b] = data[a];
 data[a] = t;
}

void bubble_sort()
{
 for(int i=1;i<=n;i++)
 {
  for(int j=n; j>=i; j–)
  {
   if(data[j] < data[j-1]) tukar(j,j-1);
  }
 }
}
int main()
{
 cout<<"===PROGRAM BUBBLE SORT==="<<endl;

 //Input Data
 cout<<"Masukkan Jumlah Data : ";
 cin>>n;
 for(int i=1;i<=n;i++)
 {
  cout<<"Masukkan data ke "<<i<<" : ";
  cin>>data[i];
  data2[i]=data[i];
 }

Rabu, 27 April 2011

Menghitung Aljabar,menentukan nilai terbesar dari bilngan bulat dan Kalkulator Sederhana

Program Untuk Menghitung Aljabar



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


class Aljabar {
    friend ostream& operator<<(ostream&, const Aljabar&);
    friend istream& operator>>(istream&, Aljabar&);
public:   
    Aljabar();
    void hitung(){      
        q = m / n;         // mendapatkan nilai q
        r = m % n;         // mendapatkan nilai r 
    }    
private:
     int m,n;          // input 
     int q,r;          // output 
};


Aljabar::Aljabar() {
     cout << "Membaca input nilai n dan m dengan ketentuan n<m\n";
     cout << "dan menampilkan output berbentuk m = q.n + r.\n\n";
}


istream& operator>>(istream& in, Aljabar& masukan) {    
    cout << "Masukkan nilai m = "; 
    in >> masukan.m;
    cout << "Masukkan nilai n = "; 
    in >> masukan.n;
    masukan.hitung();
    return in;
}
    
ostream& operator<<(ostream& out, const Aljabar& keluaran) {
    out << "Nilai q adalah = " << keluaran.q << endl;
    out << "Nilai r adalah = " << keluaran.r << endl << endl; 
    out << "Jadi, " << keluaran.m << " = " << keluaran.q << " x ";
    out << keluaran.n << " + " << keluaran.r;
    return out;
}


main() {
     Aljabar X;
     cin >> X;     
     cout << X;
     getch();
     return 0;
}

Program menentukan nilai terbesar dari bilngan bulat

#include <iostream.h>
#include <conio.h>
class Banding {
    friend istream& operator>>(istream&, Banding&);
public:
    Banding() {}; 
    void bandingkan() {
       if (A > B) 
            cout << "Bilangan terbesar : " << A;
       else 
            cout << "Bilangan terbesar : " << B;
    }        
private:
    int A, B;
};

istream& operator>>(istream& in, Banding& bilangan){
     cout << "Bilangan pertama  = "; 
     in >> bilangan.A;
     cout << "Bilangan kedua    = "; 
     in >> bilangan.B;
     return in;
};

main() {
     Banding bilangan;
     cin >> bilangan;
     bilangan.bandingkan();
     getch();
     return 0;
}

Program Kalkulator Sederhana C++

#include <cstdlib>
#include <iostream>
#include <math.h>

using namespace std;

    class kalkulator{
    public :
          void tampilan ();
          void penjumlahan ();
          void pengurangan ();
          void perkalian ();
          void pembagian ();
          void perpangkatan ();
          void akar_kuadrat ();
          void exponensial ();
          void sinus ();
          void cosinus ();
          void tangen ();
          void exit ();
          
    private :
            int x, y, z, w;
            float jumlah ;
             };
    void kalkulator::tampilan (){           
          
          
          cout  <<  "    ******************************************\n\n" ;
          cout  <<"IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII\n";
          cout  <<"II 1. Penjumlahan       II   6. Akar_Kuadrat     II\n";
          cout  <<"II 2. Pengurangan       II   7. Exponensial      II\n";
          cout  <<"II 3. Perkalian         II   8. Sin (x)          II\n";
          cout  <<"II 4. Pembagian         II   9. Cos (x)          II\n";
          cout  <<"II 5. Perpangkatan      II  10. Tan (x)          II\n";
          cout  <<"II               ****************                II\n";
          cout  <<"II               ** 11. Keluar **                II\n";    
          cout  <<"IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII\n\n";
          }
    void kalkulator::penjumlahan (){
          cout<<"masukan nilai x = " ;
          cin>> x;
          cout<<"masukan nilai y = " ;
          cin>>y;
          cout<<"masukan nilai z = " ;
          cin>>z;
          jumlah = x+y+z;
          cout<<"hasilnya adalah = "<<jumlah <<"\n"<<endl;
          }
     void kalkulator::pengurangan (){
          system("cls");
          int pil;
          cout<<"Operasi pengurangan\n";
          cout<<"\nTekan tombol 1...!>>>>  ";
          cin>>pil;
          switch(pil){
                      case 1 :
          cout<<"masukan nilai x = " ;
          cin>> x;
          cout<<"masukan nilai y = " ;
          cin>> y;
          jumlah = x-y;
          cout<<"hasilnya adalah = "<<jumlah <<"\n";break;//endl;
          }
          }
     void kalkulator::perkalian (){
          system("cls");
          int pil;
          cout<<"1.Perkalian 2 bilangan\n2.Perkalian 3 bilangan\n3.perkalian 4 bilangan";
          cout<<"\nMasukkan pilihan : ";
          cin>>pil;
          switch(pil){
                      case 1:
                      cout<<"\nmasukkan nilai x : ";
                      cin>>x;
                      cout<<"masukkan nilai y : ";
                      cin>>y;
                      cout<<"hasilnya adalah  = "<<x*y<<"\n\n";break;
                      case 2:
                      cout<<"\nmasukkan nilai x : ";
                      cin>>x;
                      cout<<"masukkan nilai y : ";
                      cin>>y;
                      cout<<"masukkan nilai z : ";
                      cin>>z;
                      cout<<"hasilnya adalah  = "<<x*y*z<<"\n\n";break;
                      case 3:
                      cout<<"\nmasukkan nilai x : ";
                      cin>>x;
                      cout<<"masukkan nilai y : ";
                      cin>>y;
                      cout<<"masukkan nilai z : ";
                      cin>>z;
                      cout<<"masukkan nilai w : ";
                      cin>>w;
                      cout<<"hasilnya adalah  = "<<x*y*z*w<<"\n\n";break;
                      }
                      }
                      
          void kalkulator::pembagian (){
          cout<<"masukan nilai x = ";
          cin>> x;
          cout<<"masukan nilai y = ";
          cin>>y;
          jumlah= x/y;
          cout<<"hasilnya adalah = "<<jumlah<<"\n"<<endl;
          } 
     void kalkulator::perpangkatan (){
          system("cls");
          int pil;
          cout<<"1.Pangkat 2\n2.Pangkat 3";
          cout<<"\nMasukkan pilihan : ";
          cin>>pil;
          switch(pil){
                      case 1:
                      cout<<"\nmasukkan nilai x : ";
                      cin>>x;
                      cout<<"hasil perpangkatannya adalah  = "<<x*x<<"\n\n";break;
                      case 2:
                      cout<<"\nmasukkan nilai x : ";
                      cin>>x;
                      cout<<"hasil perpangkatannya adalah  = "<<x*x*x<<"\n\n";break;
                      }
                      }
     
     void kalkulator::akar_kuadrat (){
          cout<<"masukan nilai x = ";
          cin>> x;
          jumlah=sqrt(x);
          cout<<"hasilnya adalah = "<<jumlah<<"\n"<<endl;
          } 
     void kalkulator::exponensial (){
          cout<<"masukan nilai x = ";
          cin>> x;
          jumlah=exp(x);
          cout<<"hasilnya adalah = "<<jumlah<<"\n"<<endl;
          } 
      void kalkulator::sinus (){
          cout<<"Sin dari  = ";
          cin>> x;
          jumlah = sin (x);
          cout<<"hasilnya adalah = "<<jumlah<<"\n"<<endl;
          }   
      void kalkulator::cosinus (){
          cout<<"Cos dari  = ";
          cin>> x;
          jumlah = cos (x);
          cout<<"hasilnya adalah = "<<jumlah<<"\n"<<endl;
          }   
      void kalkulator::tangen (){
          cout<<"Tangen dari = ";
          cin>> x;
          jumlah = tan (x);
          cout<<"hasilnya adalah = "<<jumlah<<"\n"<<endl;
          }   
      void kalkulator::exit(){
          system("cls");
          cout<<"@@@<^;^ Terima kasih telah menggunakan program kalkulator sederhana^;^ @@@\n\n";
          system("PAUSE");
          }
    int main (){
         int p;
         kalkulator a;
         a.tampilan();
         cout<<"Masukan Pilihan Anda : ";
         cin>>p;
       switch (p){
       case 1 :a.penjumlahan();break;
       case 2 :a.pengurangan() ;break;
       case 3 :a.perkalian  () ;break;
       case 4 :a.pembagian  () ;break;
       case 5 :a.perpangkatan  () ;break;
       case 6 :a.akar_kuadrat ();break;
       case 7 :a.exponensial ();break;
       case 8 :a.sinus();break;
       case 9 :a.cosinus();break;
       case 10:a.tangen ();break;
       case 11:a.exit();return 0;
       default:
               cout<<"Maaf pilihan yang Anda masukan salah,,,!!!\n";
               cout<<"silahkan masukan pilihan yang terdapat pada menu\n";  
       }
        system("PAUSE");
        system("cls");
        return main();
        return (EXIT_SUCCESS);
       }