Minggu, 04 Desember 2011

struktur data program menghitung nilai indek array versi 6

0 komentar
source code c++:
#include <iostream.h>
#include <conio.h>

class MyArray {
    friend ostream& operator<<(ostream&, const MyArray&);
public:
    MyArray(int n=10);
    void kurang_satu();
    void cetak_array();   
private:
    int n;
    int A[10];   
};

ostream& operator<<(ostream& output, const MyArray& Mine) {
    for (int i=0; i<Mine.n; i++)
        output << "Nilai indeks ke-[" << i << "] adalah " << Mine.A[i] << endl;
    getch(); 
    return output;
}

MyArray::MyArray(int ukuran) {
    n = ukuran;
    for (int i=0; i<n; i++)
        A[i] = i+1;
}       

void MyArray::kurang_satu() {
    for (int i=0; i<n; i++)
        A[i]--;   
}

void MyArray::cetak_array() {
    for (int i=0; i<n; i++)
        cout << "Nilai indeks ke-[" << i << "] adalah " << A[i] << endl;
    getch(); 
}

int main() {
    MyArray X;
    cout << "Nilai array asal : " << endl;
    X.cetak_array();
    cout << "\nPakai operator overloading :\n";
    cout << X;
    X.kurang_satu();
    cout << "Setelah dikurangi 1 menjadi : " << endl;
    X.cetak_array();
}
hasil:
Continue reading →

struktur data program menghitung nilai indek array versi 5

0 komentar
source code c++:
#include <iostream.h>
#include <conio.h>
class MyArray {
public:
    MyArray();
    void kurang_satu();
    void cetak_array();   
private:
    int n;
    int A[10];   
};

MyArray::MyArray() {
    n = 10;
    for (int i=0; i<n; i++)
        A[i] = i+1;
}       

void MyArray::kurang_satu() {
    for (int i=0; i<n; i++)
        A[i]--;   
}

void MyArray::cetak_array() {
    for (int i=0; i<n; i++)
        cout << "Nilai indeks ke-[" << i << "] adalah " << A[i] << endl;
    getch(); 
}

int main() {
    MyArray X;
    cout << "Nilai array asal : " << endl;
    X.cetak_array();
    X.kurang_satu();
    cout << "Setelah dikurangi 1 menjadi : " << endl;
    X.cetak_array();
}
hasil:
Continue reading →

struktur data program menghitung nilai indek array versi 4

0 komentar
source code c++:
#include <iostream.h>
#include <conio.h>
// coba pakai const

void kurang_satu(int A[], int n) {
    for (int i=0; i<n; i++)
        A[i]--;   
}

void cetak_array(int A[], int n) {
    for (int i=0; i<n; i++)
        cout << "Nilai indeks ke-[" << i << "] adalah " << A[i] << endl;
    getch(); 
}

int main() {
    int A[10] = {1,2,3,4,5,6,7,8,9,10};
    int n = 10;
    cout << "Nilai array asal : " << endl;
    cetak_array(A, n);
    kurang_satu(A, n);
    cout << "Setelah dikurangi 1 menjadi : " << endl;
    cetak_array(A, n);
}
hasil:
Continue reading →

struktur data program menghitung nilai indek array versi 3

0 komentar
source code c++:
#include <iostream.h>
#include <conio.h>

void cetak_array(int A[], int n, int kurang) {
    for (int i=0; i<n; i++)
        cout << "Nilai indeks ke-[" << i << "] adalah " << A[i]-kurang << endl;
    getch(); 
}

int main() {
    int A[10] = {1,2,3,4,5,6,7,8,9,10};
    int n = 10;
    cout << "Nilai array asal : ";
    cetak_array(A, n, 0);
    cout << "Setelah dikurangi 1 menjadi : " << endl;
    cetak_array(A, n, 1);
    cout << "Nilai array dicetak kembali" << endl;
    cetak_array(A, n, 0);
    cout << "Ternyata tidak berubah. Perhatikan perbedaannya " << endl;   
    for (int i=0; i<n; i++)
        cout << "Nilai indeks ke-[" << i << "] adalah " << A[i]-- << endl;       
    cout << "Nilai array dicetak kembali" << endl;
    cetak_array(A, n, 0);
}
hasil:
Continue reading →

struktur data program menghitung nilai indek array versi 2

0 komentar
source code c++:
#include <iostream.h>
#include <conio.h>

void cetak_array(int A[], int n) {
    for (int i=0; i<n; i++)
        cout << "Nilai indeks ke-[" << i << "] adalah " << A[i] << endl;
    getch(); 
}

int main() {
    int A[10] = {1,2,3,4,5,6,7,8,9,10};
    int n = 10;
    cout << "Nilai array asal : ";
    cetak_array(A, n);
    cout << "Setelah dikurangi 1 menjadi : " << endl;
    for (int i=0; i<n; i++)
        cout << "Nilai indeks ke-[" << i << "] adalah " << A[i]-1 << endl;       
    getch();
    cout << "Nilai array dicetak kembali" << endl;
    cetak_array(A, n);
    cout << "Ternyata tidak berubah. Perhatikan perbedaannya " << endl;   
    for (int i=0; i<n; i++)
        cout << "Nilai indeks ke-[" << i << "] adalah " << A[i]-- << endl;       
    cout << "Nilai array dicetak kembali" << endl;
    cetak_array(A, n);
}
hasil:
Continue reading →

struktur data program menghitung nilai indek array versi 1

0 komentar
source code c++:
#include <iostream.h>
#include <conio.h>

int main() {
    int A[10] = {1,2,3,4,5,6,7,8,9,10};
    int n = 10;
    cout << "Nilai array asal : ";
    for (int i=0; i<n; i++)
        cout << "Nilai indeks ke-[" << i << "] adalah " << A[i] << endl;
    getch();
    cout << "Setelah dikurangi 1 menjadi : " << endl;
    for (int i=0; i<n; i++)
        cout << "Nilai indeks ke-[" << i << "] adalah " << A[i]-1 << endl;       
    getch();
    cout << "Nilai array dicetak kembali" << endl;
    for (int i=0; i<n; i++)
        cout << "Nilai indeks ke-[" << i << "] adalah " << A[i] << endl;
    cout << "Ternyata tidak berubah. Perhatikan perbedaannya " << endl;
    getch();
    for (int i=0; i<n; i++)
        cout << "Nilai indeks ke-[" << i << "] adalah " << A[i]-- << endl;       
    cout << "Nilai array dicetak kembali" << endl;
    for (int i=0; i<n; i++)
        cout << "Nilai indeks ke-[" << i << "] adalah " << A[i] << endl;       
    getch();
}
hasil:
Continue reading →

Tugas struktur data stack(kelompok)

0 komentar
source code c++:
#include <string>
#include <iostream>


using namespace std;
class stak{
 public:
        stak();
        void baru();
        void bagi1();
        void bagi2();
        void cetak1();
        void cetak_hasil();
 private:
        int top;
        char stackA[9];
        char stackC[9];
        char stackB[9];
        char x;};

stak::stak(){
        top=-1;
        cout<<"Tugas Kelompok Stack\n\n";}

void stak::baru(){
        cout<<"Stack A\n";
        for (int i=0; i<6; i++){
        cout<<"Masukkan stack ["<<i+1<<"] : ";
        cin>>x;
        top++;
        stackA[top]=x;}}

void stak::bagi2(){
     cout<<"\nKarakter yang di pop";
     cout<<"\nStack B\n";
     int i=0;
     for(i=2;i>=0;i--){
     x=stackA[i];
     stackB[i+1]=x;
     cout<<"stack ke ["<<i+1<<"] : "<< x<<endl;}}
    
void stak::bagi1(){
     cout<<"\nKarakter yang di pop";
     cout<<"\nStack C\n";
     int i=0;
     for(i=top;i>=3;i--){
     x=stackA[i];
     stackC[i-2]=x;
     cout<<"stack ke ["<<i-2<<"] : "<< x<<endl;}}

void stak::cetak1(){
        int i=0;
        for (i= top; i>=0; i--)
        cout<<"stack ke ["<<i+1<<"] : "<<stackA[i]<<endl;}

void stak::cetak_hasil(){
     cout<<"\nHasil pembagian stack";
     cout<<"\n\tStack B\n";
        int i=0;
        for (i= 3; i>=1; i--){
        cout<<"stackB ke ["<<i<<"] : "<<stackB[i]<<endl;}
        cout<<"\n\tStack C\n";
        int j=0;
        for (j= 3; j>=1; j--){
        cout<<"stackC ke ["<<j<<"] : "<<stackC[j]<<endl;}}
       
int main (){
        int input;
        stak a;
        a.baru();
        //a.cetak1();
        a.bagi1();
        a.bagi2();
        a.cetak_hasil();
         system("PAUSE");
         return EXIT_SUCCESS;}

hasil:
Continue reading →

struktur data program array2d

0 komentar
source code c++:
// test 2D array class

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


void main(void)
{

   try {
      int i, j;
      Array2D<int> X(2,2);
      for (i = 0; i < 2; i++)
         for (j = 0; j < 2; j++)
            X[i][j] = 0;

      cout << "X is" << endl;
      cout << X << endl;

       X.IsiMatrix(1,2,3,4);
      cout << "X is" << endl;
      cout << X << endl;

     }
   catch (...) {
      cerr << "An exception has occurred" << endl;}

      getch();
}
hasil:
Continue reading →

struktur data program link list container excample 5-1

0 komentar
source code c++:
#include <iostream>
#include <list>
#include <iterator>
#include <algorithm>
#include <conio.h>
using namespace std;

int main()
{
list<int> intList1, intList2, intList3, intList4;            //Line 1

    ostream_iterator<int> screen(cout," ");       //Line 2

    intList1.push_back(23);               //Line 3
    intList1.push_back(58);               //Line 4
    intList1.push_back(58);               //Line 5
    intList1.push_back(58);               //Line 6
    intList1.push_back(36);               //Line 7
    intList1.push_back(15);               //Line 8
    intList1.push_back(93);               //Line 9
    intList1.push_back(98);               //Line 10
    intList1.push_back(58);               //Line 11

    cout<<"Line 12: intList1: ";               //Line 12
    copy(intList1.begin(),intList1.end(),screen);//Line 13
    cout<<endl;                       //Line 14

    intList2 = intList1;                   //Line 15

    cout<<"Line 16: intList2: ";               //Line 16
    copy(intList2.begin(),intList2.end(),screen);//Line 17
    cout<<endl;                       //Line 18

    intList1.unique();                   //Line 19

    cout<<"Line 20: After removing the consecutive "
           <<"duplicates,"<<endl
           <<"         intList1: ";               //Line 20
    copy(intList1.begin(),intList1.end(),screen);//Line 21
    cout<<endl;                       //Line 22

    intList2.sort();                   //Line 23

    cout<<"Line 24: After sorting, intList2: ";  //Line 24
    copy(intList2.begin(),intList2.end(),screen);//Line 25
    cout<<endl;                       //Line 26

    intList3.push_back(13);               //Line 27
    intList3.push_back(23);               //Line 28
    intList3.push_back(25);               //Line 29
    intList3.push_back(136);               //Line 30
    intList3.push_back(198);               //Line 31
   
    cout<<"Line 32: intList3: ";               //Line 32
    copy(intList3.begin(),intList3.end(),screen);//Line 33
    cout<<endl;                       //Line 34

    intList4.push_back(-2);               //Line 35
    intList4.push_back(-7);               //Line 36
    intList4.push_back(-8);               //Line 37
   
    cout<<"Line 38: intList4: ";               //Line 38
    copy(intList4.begin(),intList4.end(),screen);//Line 39
    cout<<endl;                       //Line 40

    intList3.splice(intList3.begin(),intList4);  //Line 41

    cout<<"Line 42: After moving the elements of "
            <<"intList4 into intList3,"<<endl
            <<"         intList3: ";               //Line 42
    copy(intList3.begin(),intList3.end(),screen);//Line 43
    cout<<endl;                       //Line 44

    intList3.sort();                   //Line 45

    cout<<"Line 46: After sorting, intList3: ";  //Line 46
    copy(intList3.begin(),intList3.end(),screen);//Line 47
    cout<<endl;                       //Line 48

    intList2.merge(intList3);               //Line 49

    cout<<"Line 50: After merging intList2 and intList3, "
            <<"intList2: "<<endl<<"         ";       //Line 50
    copy(intList2.begin(),intList2.end(),screen);//Line 51
    cout<<endl;                       //Line 52

    intList2.unique();                   //Line 53

    cout<<"Line 54: After removing the consecutive "
        <<"duplicates, intList2: "<<endl
       <<"         ";                   //Line 54
    copy(intList2.begin(),intList2.end(),screen);//Line 55
    cout<<endl;                       //Line 56
getch();
    return 0;
}
hasil:
Continue reading →

struktur data program Queue

0 komentar
source code c++:

#include<iostream.h>
#include<conio.h>
#define maks 5
class Queue{
friend ostream& operator<<(ostream&,const Queue&);
public:
Queue();
int penuh(int);
int kosong(int);
void cetak();
void enqueue(char);
char dequeue();
private:
char A[maks];
int banyak;
};
ostream& operator<<(ostream& out,const Queue& s)
{ cout<<"\nIsi Queue:";
for(int i=0;i<s.banyak;i++)
out<<s.A[i]<<"";
}
Queue::Queue(){
banyak=0;
for(int i=0;i<maks;i++)
A[i]='o';
}
int Queue::penuh(int s)
{ return s==maks?1:0;}
int Queue::kosong(int s)
{ return s==0?1:0;}
void Queue::cetak()
{ cout<<"\nIsi Queue:";
for(int i=0; i<banyak;i++)
cout<<A[i]<<"";}
void Queue::enqueue(char x)
{ cout<<"\nElemen:"<<x<<"masuk antrian";
if(penuh(banyak))cout<<"queue penuh";
else if(A[0]=='0'){
A[0]=x;
banyak++;}
else{
for(int i=banyak;i>=0; i--)
A[i+1]=A[i];
A[0]=x;
banyak++;
}
}
char Queue::dequeue(){
char temp=A[--banyak];
cout<<"\ndequeue elemen -->"<<temp;
A[banyak]='0';
return temp;
}
int main(){
Queue q;
for(char c='a'; c<'d';c++){
q.enqueue(c);
cout<<q;
}
char p=q.dequeue();
q.cetak();
cout<<"\n\nCetak pakai overloading:"<<q;
getch();
return 0;
}
hasil:







Continue reading →

struktur data program link list

0 komentar
source code c++:
#include <cstdlib>
#include <iostream>

using namespace std;

class node{
      friend class list;
      friend ostream& operator<<(ostream&, const list&);
      public:
             node(char &t,node *p):info(t),berikut(p){}
      protected:
                char info;
                node *berikut;
      };
   

      class list{
            friend ostream& operator<<(ostream&,const list&);
            public:
                   list():kepala(0){}
                   ~list();
                   void sisip(char t);
                   int hapus(char& t);
                   int hapusbelakang(char& t);
                   int kosong(){return(kepala==0);}
                   void cetak();
            protected:
                      node *kepala;
                      node *nodebaru(char& t, node*p){
                              node *q=new node(t,p);return q;}
            };
   
         
 
      ostream& operator<<(ostream& out, const list& k){
               for(node* p=k.kepala;p;p=p->berikut)
               out<<p->info<<" ->";
               out<<"*\n";
               return out;
               }
            

      list::~list(){
      node* temp;
      for(node* p=kepala;p;){
          
            temp=p;
            p=p->berikut;
            delete temp;
            }
                       }
                    
      void list::sisip(char t){
           cout<<t<<" masuk list : ";
           node *p=nodebaru(t,kepala);
           kepala=p;
           }
        
      int list::hapus(char& t){
          if(kosong())return 0;//penghapusan gagal
          t=kepala->info;
          node* p=kepala;
          kepala=kepala->berikut;
          cout<<t<<endl;
          delete p;

          return 1;//penghapusan sukses
          }
       int list::hapusbelakang(char& t){
          if(kosong())return 0;//penghapusan gagal
          t=kepala->info;
          node*p=kepala;
          kepala=kepala->berikut;
          delete p;
          return 1;//penghapusan sukses
          } 
   
       
      void list::cetak(){
           for(node* p=kepala;p;p=p->berikut)
           cout<<p->info<<" ->";
           cout<<"*\n";
           }


int main(int argc, char *argv[])
{
    list x;
    char data;
    x.sisip('a');
    cout<<x;
    x.sisip('b');
    cout<<x;
    x.sisip('c');
    cout<<x;
    x.sisip('d');
    cout<<x;
    for(int i=0;i<5;i++){
            x.hapus(data);
            cout<<data<<" dihapus dari list: ";
            cout<<x;
            }
 
    system("PAUSE");
    return EXIT_SUCCESS;
}
hasil:
Continue reading →

struktur data program stack

0 komentar
source code:

#include<iostream.h>
#include<conio.h>
#define maks 5
class Stack{
friend ostream& operator<<(ostream&,const Stack&);
public:
Stack();
int penuh(int);
int kosong(int);
void cetak();
void push(char);
char pop();
private:
char A[maks];
int banyak;
};
ostream& operator<<(ostream& out,const Stack& s)
{ cout<<"\nIsi stack:";
for(int i=0;i<s.banyak;i++)
out<<s.A[i]<<"";
}
Stack::Stack(){
banyak=0;
for(int i=0;i<maks;i++)
A[i]='o';
}
int Stack::penuh(int s)
{ return s==maks?1:0;}
int Stack::kosong(int s)
{ return s==0?1:0;}
void Stack::cetak()
{ cout<<"\nIsi Stack:";
for(int i=0; i<banyak;i++)
cout<<A[i]<<"";}
void Stack::push(char x)
{ cout<<"\nElemen masuk:"<<x;
if(penuh(banyak))cout<<"Stack penuh";
else if(A[0]=='0'){
A[0]=x;
banyak++;}
else{
for(int i=banyak;i>=0; i--)
A[i+1]=A[i];
A[0]=x;
banyak++; }
}
char Stack::pop()
{ cout<<"\npop stack,elemen yang di-pop:"<<A[0];
char temp=A[0];
for(int i=0; i<banyak; i++)A[i]=A[i+1];
A[banyak]='0';
banyak--;
return temp;
}
int main(){
Stack stack;
for(char c='a'; c<'d';c++){
stack.push(c);
stack.cetak();
}
char p=stack.pop();
stack.cetak();
cout<<"\n\nCetak pakai overloading:"<<stack;
getch();
return 0;
}
hasil:
Continue reading →

Labels