Jumat, 08 Juli 2011

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

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];
 }