Senin, 19 Mei 2014

TITIK TERDEKAT C++

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

using namespace std;
struct Titik{
               int x, y;
       };
       
void TitikTerdekat(Titik p[], int n, Titik &p1, Titik &p2){
    double d,dmin,dmax,temp;
    int i,j;
    dmin=9999;
    for(int i=1; i<=n; i++){
             for(int j=i+1; j<=n; j++){
             temp = sqrt( pow(( p[i].x - p[j].x ),2) + pow(( p[i].y - p[j].y ),2) );
             if(d<dmin){
                        dmin=d;
                        p1=p[i];
                        p2=p[j];
                        }
             }
             }
    }
   
int main(int argc, char *argv[]){
    Titik p[100];
    int n;
    Titik p1,p2;
    cout<<"Masukkan banyak titik : ";
    cin>>n;
    cout<<"Masukkan titik - titiknya : "<<endl;
    for(int i=0; i<n; i++){
            cout<<"Titik ["<<i+1<<"] : "<<endl;
            cout<<"x : "; cin>>p[i].x;
            cout<<"y : "; cin>>p[i].y;
            }
    TitikTerdekat(p,n,p1,p2);
    cout<<endl;
    cout<<"Dua titik terdekat : "<<endl;
    cout<<"Titik pertama = ( "<<p1.x<<" , " <<p1.y<<" )"<<endl;
    cout<<"Titik kedua = ( "<<p2.x<<" , " <<p2.y<<" )";
    cout<<endl;
   
    system("PAUSE");
    return 0;
    }



PROGRAM TUKAR C++

#include <cstdlib>
#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{
    cout<<"Program Tukar Bilangan" << endl;
    int a, b, temp;
    cout<<"Masukkan nilai A : ";
    cin>>a;
    cout<<"Masukkan NIlai B : ";
    cin>>b;

    temp = a;
    a = b;
    b = temp;
    cout<<"Setelah Ditukar : "<<endl;
    cout<<"Nilai A : "<<a<<endl;
    cout<<"Nilai B : "<<b<<endl;
    cout<<endl;

    system("PAUSE");
    return EXIT_SUCCESS;
}



PROGRAM MERGESHORT C++

#include <cstdlib>
#include <iostream>

using namespace std;

void Merge(int* A, int kiri,int tengah, int kanan){
   
    int B[kiri+kanan];
    int i,kidal1,kidal2;
   
    kidal1=kiri;
    kidal2=tengah+1;
    i=kiri;
   
    while (kidal1<=tengah && kidal2 <= kanan){
        if(A[kidal1] <= A[kidal2]){
            B[i]=A[kidal1];
            kidal1++;
        }
        else{
            B[i]=A[kidal2];
            kidal2++;
        }
        i++;
    }
   
    while ( kidal1 <= tengah ){
        B[i] = A[kidal1];
        kidal1++;
        i++;
        }

    while ( kidal2 <= kanan ){
        B[i] = A[kidal2];
        kidal2++;
        i++;
        }

    for (int i=kiri;i<= kanan;i++){
        A[i]=B[i];
    }
}

void MergeSort (int* A, int i, int j){
   
    if (i<j){
        int k= ((i+j)/2);
        MergeSort(A, i, k);
        MergeSort(A, k+1, j);
        Merge(A, i, k, j);
    }
}

int main(int argc, char *argv[])
{
    int n,i,j;
    
    cout<<"masukkan Banyak data : ";
    cin>>n;
    i=1;
    j=n;
    int A[n];
    for (int i=1;i<=n;i++){
        cout<<"masukan data ke-"<<i<<" : ";
        cin>>A[i];
    }
    cout<<"data sebelum diurutkan : ";
    for (int i=1;i<=j;i++){
        cout<<A[i]<<" ";
    }
    MergeSort(A,i,j);
    cout<<endl;
    cout<<"data setelah diurutkan : ";
    for (int x=1;i<=j;i++){
        cout<<A[i]<<" ";
    }
    cout<<endl;
   
    system("pause");
    return 0;
}



PROGRAM MENCARI MAX,MIN DAN RATA-RATA DALAM PASCAL

program MaxdanMin;
uses wincrt;
        var a : array[1..100] of integer;
        i,n,max,min,jum:integer;
        rata : real;
begin
        write('masukkan banyak data :');
        read(n);
        for i:=1 to n do
        begin
                write('data ke-',i, '= ');
                read(a[i]);
                jum := jum + a[i];
                end;

                rata:=jum/n;

                max := a[1];
                min := a[1];
        for i:=2 to n do
        begin
                if max < (a[i]) then max:=(a[i]);
                if min > (a[i]) then min:=(a[i]);
                end;

        writeln('jumlah : ',jum);
        writeln('nilai max :',max);
        writeln('nilai min :',min);
        writeln('rata - rata :',rata:0:2);
        readkey;
        end.