Jumat, 08 Juli 2011

BUBBLE SORT

Bubble sort is a sorting program is one of c + +.Bubble sort is a method that bases the exchange 2 pieces sorted elements to achieve the desired state.
This program :


#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);
  }
 }
}
void main()
{
 cout<<" BUBBLE SORT PROGRAM"<<endl;

 // enter data
 cout<<"Enter the Number of Data : ";
 cin>>n;
 for(int i=1;i<=n;i++)
 {
  cout<<"Enter number to- "<<i<<" : ";
  cin>>data[i];
  data2[i]=data[i];
 }

 bubble_sort();

 cout<<"\n\n";
 // data show
 cout<<"Data after Sort : ";
 for(int i=1; i<=n; i++)
 {
  cout<<" "<<data[i];
 }
 getch();
}

INSERTION SORT

Insertion sort is to sort the data of random numbers, by moving the position of the smallest to the beginning, which is done continuously until rsebut serial numbers.
let's look at the program:

#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 insertion_sort()
{
 int temp,i,j;
 for(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;
 }
}
void main()
{
 cout<<PROGRAM INSERTION SORT<<endl;

 //data Input
 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];
 }

 insertion_sort();

 cout<<"\n\n";
 //data show
 cout<<"Data Setelah di Sort : ";
 for(int i=1; i<=n; i++)
 {
  cout<<" "<<data[i];
 }
 cout<<"\n\nSorting Selesai";
 getch();
}

 

SHIFTS ARRAY

Below is the program shifts the array .. shifting the array is the example given the numbers 1 2 3 4 5 6 slide into 6 1 2 3 4 5 .... let's look at the program


#include <iostream>
#include <stdlib.h>
using namespace std;
class geser{
friend ostream& operator<<(ostream&, const geser&);
friend istream& operator>>(istream&, geser&);
public:
geser();
void cetak();
void geser_kanan();
private:
char A[6];
int posisi;
};
geser::geser(){
for(int i=0;i<6;i++)
A[i]=’O';
}
void geser::cetak(){
for(int i=0;i<6;i++)
cout<<A[i]<<” “;
}
ostream& operator<<(ostream& out, const geser& x){
for(int i=0;i<6;i++)
out<<x.A[i]<<” “;
out<<endl;
return out;
}
istream& operator>>(istream& in, geser& x){
int posisi;
for (int posisi=1; posisi<=6; posisi++){
cout<<”posisi array ke- : “;
in>>x.posisi;
if(posisi >= 0 && posisi <= 6){cout<<”elemen arraynya :”;
in>>x.A[posisi-1];
}
}
return in;
}
void geser::geser_kanan(){
int n=6;
int temp=A[n-1];
for(int i=n-1;i>=0;i–)
A[i+1]=A[i];
A[0]=temp;
}
int main(int argc, char *argv[])
{
geser x;
cout<<”program menggeser nilai array \n”;
cin>>x;
cout<<endl;
cout<<”Isi Array saat ini : “<<x;
x.geser_kanan();
cout<<”Array setelah di geser : “<<x;
cout<<endl;
system(“PAUSE”);
return 0;
}

PROGRAMS SEEK OR NOT CONNECTED GRAF

This week I want to talk about the graph. a little explanation about the graph as follows:

For example, graph is about 4 The set V (Vertex) that its elements are called vertices (or points or nodes or points). The set E (Edge) which is not sequential pairs of vertices, its members called a segment (rib or side).
the program below to find out this graph is connected or not

let's look at the program..

 
    #include <cstdlib>
    #include <iostream>


    using namespace std;

  int main(int argc, char *argv[])

    {

    bool ketemu,nolsemua;

    int matrix[10] [10];

    int i,j,jumlah_simpul,jumlah_sisi,asal,tujuan;

    //initialization of matrix

    cout<<”jumlah simpul:”;

    cin>>jumlah_simpul;


    cout<<”jumlah_sisi:”;

    cin>>jumlah_sisi;

    for (i=1;i<=jumlah_simpul;i++)

    for (j=1;j<=jumlah_simpul;j++)

    matrix[i][j]=0;

    //fill in the matrix conform the graph input

    for (i=1;i<=jumlah_sisi;i++){

    cout<<”simpul asal:”;

    cin>>asal;

    cout<<”simpul tujuan:”;

    cin>>tujuan;

    matrix[asal][tujuan]=1;

    matrix[tujuan][asal]=1;

    }

    //checking graph

    i=1;nolsemua=false;

    while (i<=jumlah_simpul && !nolsemua){

    j=1;ketemu=false;

    while (j<=jumlah_simpul && !ketemu){

    if (matrix[i][j]==1)

    ketemu=true;

    else

    j++;
    }
    if (!ketemu)
    nolsemua=true;
    else
    i++;
    }
    if(nolsemua)
    cout<<”graf tidak terhubung”<<endl;
    else
    cout<<”graf terhubung”<<endl;
        system(“PAUSE”);
        return EXIT_SUCCESS
    } 

Selasa, 05 Juli 2011

DIMENSIONS TO BE CHANGING ARRAY 1 ARRAY 2 DIMENSIONS

Array is a type of structured data that is useful to store large amounts of data are the same type. Parts that make up the array called array elements, each element can be accessed separately through the array index. whereas Is a variable that stores a set of data that has the same type and elements that will be accessed only through an index or subscript. below I present one-dimensional arrays into two-dimensional array..


#include <cstdlib>
#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{
   
    int n, baris, kolom;
int data[20];
int output[40][40];
int e=0;

cout<<"PROGRAM CHANGE DIMENSIONS TO ARRAY 1 ARRAY 2 DIMENSIONS";
cout<<endl;
cout<<"Masukan banyaknya bilangan :";
cin>>n;

for (int i=0; i<n;i++)//the process of entering numbers
{
cout<<"data ke-"<<i<<" = ";
cin>>data[i];
}

for (int j=0; j<n;j++)//the process of displaying an array of one-dimensional
{
cout<<data[j];
}
cout<<endl<<"\n";
cout<<"masukan banyaknya kolom: ";
cin>>kolom;
cout<<"masukan banyaknya baris: ";
cin>>baris;

for (int i=0;i<baris;i++)//change process into two-dimensional array of one-dimensional
{
for (int j=0;j<kolom;j++)
{
output[i][j]=data[e];
e++;
}
}
   
for (int i=0;i<baris;i++) //the process of displaying a two dimensional array
{
for (int j=0;j<kolom;j++)
{
cout<<output[i][j]<<"   ";

}
cout<<endl;
}

    system("PAUSE");
    return EXIT_SUCCESS;
}

Senin, 04 Juli 2011

TRANSPOSE PROGRAM

Transpose actually very easy to learn .. As long as we understand from the beginning ..
Transpose a matrix is the change from column to row.

let's look at the program

#include <iostream.h>
#include <conio.h>
  void main()
    {
   clrscr();
    int a[10][10],m,n,i,j;
    cout<<"Enter number of rows: ";
    cin>>m;
    cout<<"Enter number of coloumns: ";
    cin>>n;

    if(m!=n)
    {
    cout<<"Matrix not square so Transpose not possible :(";
    }
    else
    {
    cout<<endl<<"Enter elements of matrix: "<<endl;
    for(i=0;i<m;i++)
    {
    for(j=0;j<n;j++)
    {
    cout<<"Enter element a"<<i+1<<j+1<<": ";
    cin>>a[i][j];
    }
    }
   cout<<endl<<"Displaying Matrix: "<<endl<<endl;
 for(i=0;i<m;i++)
 {
  for(j=0;j<n;j++)
   {
   cout<<a[i][j]<<" ";
   }
  cout<<endl<<endl;
   }
   cout<<endl<<"Displaying Matrix Transpose: "<<endl<<endl;
   for(i=0;i<m;i++)
   {
   for(j=0;j<n;j++)
  {
   cout<<a[j][i]<<" ";
   }
   cout<<endl<<endl;
   }
  }
  getch();
  }