Tuesday, 27 May 2014

Tutorial web server 2 pada igos

*Pertama masuk ke root
        -$ su
*Kemudian install apache, mysql-server, php
       # yum install apache mysql-server php
*Jalankan installan tersebut
       # service httpd start
*Masuk ke direktori
       # cd /var/www/html
*Melihat direktori yang ada sebelumnya
       # ls
*Membuat direktori baru (misal :
*Pertama masuk ke root
        -$ su
*Kemudian install apache, mysql-server, php
       # yum install apache mysql-server php
*Jalankan installan tersebut
       # service httpd start
*Masuk ke direktori
       # cd /var/www/html
*Melihat direktori yang ada sebelumnya
       # ls
*Membuat direktori baru (misal : 12018039)
       # mkdir 12018039
*Melihat direktori yang baru dibuat
       # ls
*Masuk kedirektori yang baru dibuat
       # cd 12018039
*Membuat file baru didalam direktori  (misal : info.php)
       # touch info.php
*Mengisi file yang baru dibuat
       # nano info.php
*Melihat file yang baru dibuat
       # ls
*Untuk me-rename nama file
       # mv info.php index.php
*Lihat lagi hasil setelah di-rename
       # ls
*Melihat hak akses
      # ls -l
*Untuk merubah hak akses
      #chmod 444 index.php
^Keterangan :
      r = read : 4
      w = write : 2
      x = excution : 1
)
       # mkdir 12018039
*Melihat direktori yang baru dibuat
       # ls
*Masuk kedirektori yang baru dibuat
       # cd 12018039
*Membuat file baru didalam direktori  (misal : info.php)
       # touch info.php
*Mengisi file yang baru dibuat
       # nano info.php
*Melihat file yang baru dibuat
       # ls
*Untuk me-rename nama file
       # mv info.php index.php
*Lihat lagi hasil setelah di-rename
       # ls
*Melihat hak akses
      # ls -l
*Untuk merubah hak akses
      #chmod 444 index.php
^Keterangan :
      r = read : 4
      w = write : 2
      x = excution : 1

TUTORIAL WEB SERVER 1 linux igos

praktikan@localhost ~ $ ls -l
total 20
drwxr-xr-x 3 praktikan praktikan 4096 Mei 19 09:05 Desktop
drwxr-xr-x 2 praktikan praktikan 4096 Apr 28 2012 Dokumen
drwx------ 2 praktikan praktikan 4096 Mar 24 10:49 modul keamanan komputer
-rw-r--r-- 1 root      root      1704 Mei  9 14:39 privkey.pem
drwxr-xr-x 2 praktikan praktikan 4096 Mei 21 10:59 Unduhan
praktikan@localhost ~ $ su
root@localhost /home/praktikan # ls -l
total 20
drwxr-xr-x 3 praktikan praktikan 4096 Mei 19 09:05 Desktop
drwxr-xr-x 2 praktikan praktikan 4096 Apr 28 2012 Dokumen
drwx------ 2 praktikan praktikan 4096 Mar 24 10:49 modul keamanan komputer
-rw-r--r-- 1 root      root      1704 Mei  9 14:39 privkey.pem
drwxr-xr-x 2 praktikan praktikan 4096 Mei 21 10:59 Unduhan
root@localhost /home/praktikan # service httpd start
Redirecting to /bin/systemctl start  httpd.service
root@localhost /home/praktikan # cd /var/www/html/
root@localhost /var/www/html # ls
12018108  12018130  12018130ve  12018138  12018202  info.php
root@localhost /var/www/html # 12018039
bash: 12018005: perintah tidak ditemukan
root@localhost /var/www/html # mkdi
bash: mkdi: perintah tidak ditemukan
root@localhost /var/www/html # mkdi
bash: mkdi: perintah tidak ditemukan
root@localhost /var/www/html # mkdir 12018039
root@localhost /var/www/html # ls
12018039  12018108  12018130  12018130ve  12018138  12018202  info.php
root@localhost /var/www/html # cd ironepp/
bash: cd: ironepp/: Tidak ada berkas atau direktori seperti itu
root@localhost /var/www/html # cd 12018039/
root@localhost /var/www/html/12018039 # touch index.php
root@localhost /var/www/html/12018039 # ls
index.php
root@localhost /var/www/html/12018039 # nano index.php
root@localhost /var/www/html/12018039 # nano index.php
root@localhost /var/www/html/12018039 # nano index.php
root@localhost /var/www/html/12018039 # nano index.php
root@localhost /var/www/html/12018039 # nano index.php
root@localhost /var/www/html/12018039 # ls -l
total 8
-rw-r--r-- 1 root root 29 Mei 24 11:26 aku
-rw-r--r-- 1 root root 28 Mei 24 11:31 index.php
root@localhost /var/www/html/12018039 # chmod 644
chmod: hilang operand setelah `644'
Try 'chmod --help' for more information.
root@localhost /var/www/html/12018039 # chmod 755
chmod: hilang operand setelah `755'
Try 'chmod --help' for more information.
root@localhost /var/www/html/12018039 # ls -l
total 8
-rw-r--r-- 1 root root 29 Mei 24 11:26 aku
-rw-r--r-- 1 root root 28 Mei 24 11:31 index.php
root@localhost /var/www/html/12018039 # chmod 755 index.php
root@localhost /var/www/html/12018039 # chmod

Monday, 26 May 2014

Grafkom praktikum 5

/* Praktikum 05
* Membuat objek lingkaran sederhana
*/
#include <iostream>
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include <glut.h>
#include<math.h>
using namespace std;
typedef unsigned char uchar;
// number of line segments
static int num_lines = 3;
static int num_lines1 = 20;
// callback prototypes
void disp(void);
void keyb(uchar k, int x, int y);
void reshape(int x, int y);
// main
int main(int argc, char **argv){
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE);
glutInitWindowSize(400,400);
glutInitWindowPosition(100,100);
glutCreateWindow("circle.cpp");
glClearColor(0.0,0.0,0.0,1.0);
glutDisplayFunc(disp);
glutKeyboardFunc(keyb);
glutReshapeFunc(reshape);
glutMainLoop();
return 0;
}
// disp
void disp(void){
double angle;
glClear(GL_COLOR_BUFFER_BIT);
glBegin(GL_LINE_LOOP);
glColor3f(0.0,1.0,0.0);
for(int i =0;i<num_lines;i++){
angle = i*2*3.14/num_lines;
glVertex2f(cos(angle),sin(angle));
}
glEnd();
glBegin(GL_LINE_LOOP);
glColor3f(1.0,0.0,0.0);
for(int i =0;i<num_lines1;i++){
angle = i*2*3.14/num_lines1;
glVertex2f(cos(angle),sin(angle));
}
glEnd();

glColor3f(1.0,0.0,1.0);
glBegin(GL_POLYGON);

for(int i =0;i<num_lines1;i++){

angle = i*2*3.14/num_lines1;
glVertex2f((cos(angle)/2),(sin(angle)/2));
}

glEnd();
glutSwapBuffers();
}
// keyb
void keyb(uchar k, int x, int y){
switch (k){
case 'q':
exit(0);
break;
case '+':
if(num_lines< 99){
num_lines++;
num_lines1--;
cout << "Circle consists of " << num_lines << " lines " << endl;
glutPostRedisplay();
}
break;
case '-':
if(num_lines >3){
num_lines--;
num_lines1++;
cout << "Circle consists of " << num_lines << " lines " << endl;
glutPostRedisplay();
}
break;
}
}
// reshape
void reshape(int x,int y){
if(x<y)
glViewport(0,(y-x)/2,x,x);
else
glViewport((x-y)/2,0,y,y);
}

Grafkom praktikum 6

#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include <glut.h>
//#include <glu.h>
#include <iostream>

float _angle = 45.0f;
//draws the 3D scene
void mydisplay()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); //menghapus layar
    glMatrixMode(GL_MODELVIEW); //switch to setting the camera perspective
    glLoadIdentity();//reset the camera
    //glTranslatef(0.5f, 0.0f, 0.0f);//translasi
    //glScalef(2.0f, 2.0f, 3.0f);//skala
    //glRotatef(_angle, 0.0f, 0.0f, 1.0f);
    glPushMatrix();//save the transformations performed thus far
    glBegin(GL_POLYGON);
    glVertex3f(-0.1f, -0.1f, 0.0f);
    glVertex3f(-0.1f, 0.1f, 0.0f);
    glVertex3f(0.1f, 0.1f, 0.0f);
    glVertex3f(0.1f, -0.1f, 0.0f);
    glEnd();
    glPopMatrix();//undo the move to the center of the trapezoid
    glutSwapBuffers();//send the 3D scene to the screen
    glFlush();
}



void update(int value){
    _angle += 9.0f;
    if(_angle > 360){
        _angle -= 360;
    }
    glutPostRedisplay();//tell glut that the display changed
    //Tell glut to call update
    glutTimerFunc(25, update, 0);
}
int main(int argc, char** argv)
{
glutInitWindowSize(400,400);
printf("Contoh Sederhana Kotak ");
glutCreateWindow("Praktikum");
glutDisplayFunc(mydisplay);
glutTimerFunc(25, update,0);
glutMainLoop();
return 0;
}

Contoh praktikum 7 SA read_graph

#include<iostream.h>
#include<stdlib.h>
#define MAX 20
#define INFINITY 9999
class dijkstra
{
private:
int n;
int graph[MAX][MAX];
int colour[MAX];
int start;
int distance[MAX];
int predecessor[MAX];
enum {green,yellow,red};
public:
void read_graph();
void initialize();
int select_min_distance_lable();
void update(int);
void output();
void function();

};
void dijkstra::read_graph()
{
cout<<"Enter the no. of nodes in the graph ::";
cin>>n;
cout<<"Enter the adjacency matrix for the graph ::\n";
int i,j;
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
cin>>graph[i][j];
for(i=1;i<=n;i++)
colour[i]=green;
cout<<"Enter the start vertex ::";
cin>>start;
}
void dijkstra::initialize()
{
for(int i=1;i<=n;i++)
{
if(i==start)
distance[i]=0;
else
distance[i]=INFINITY;
}
for(int j=1;j<=n;j++)
{
if(graph[start][j]!=0)
predecessor[j]=start;
else
predecessor[j]=0;
}
}
int dijkstra::select_min_distance_lable()

{
int min=INFINITY;
int p=0;
for(int i=1;i<=n;i++)
{
if(colour[i]==green)
{
if(min>=distance[i])
{
min=distance[i];
p=i;
}
}
}
return p;
}
void dijkstra::update(int p) // p is a yellow colour node
{
cout<<"\nupdated distances are ::\n";
for(int i=1;i<=n;i++)
{
if(colour[i]==green)
{
if(graph[p][i]!=0)
{
if(distance[i]>graph[p][i]+distance[p])
{
distance[i]=graph[p][i]+distance[p];
predecessor[i]=p;
}
}
}
cout<<distance[i]<<'\t';
}
}
void dijkstra::output()

{
cout<<"****** The final paths and the distacnes are ******\n\n";
for(int i=1;i<=n;i++)
{
if(predecessor[i]==0 && i!=start)
{
cout<<"path does not exists between "<<i<<" and the
start vertex "
<<start<<endl;
exit(1);
}
cout<<"path for node “<<i<<” is ::\n";
int j=i;
int array[MAX];
int l=0;
while(predecessor[j]!=0)
{
array[++l]=predecessor[j];
j=predecessor[j];
}
for(int k=l;k>=1;k=-k)
cout<<array[k]<<"->";
cout<<i<<endl;
cout<<"distance is "<<distance[i]<<endl<<endl<<endl;
}
}
void dijkstra::function()
{
cout<<"\n*****************************************************************
*****\n";
cout<<"This program is to implement dijkstra’s algorithm using colour
codes \n";
cout<<"*******************************************************************

***\n\n";
read_graph();
initialize();
//repeate until all nodes become red
int flag=0;
int i;
cout<<"\n\n******** The working of the algorithm is **********\n\n";
for(i=1;i<=n;i++)
if(colour[i]!=red)
flag=1;
cout<<"The initial distances are ::\n";
for(i=1;i<=n;i++)
cout<<distance[i]<<'\t';
cout<<endl;
while(flag)
{
int p=select_min_distance_lable();
cout<<"\nThe min distance lable that is coloured yellow is "<<p;
colour[p]=yellow;
update(p);
cout<<"\nnode "<<p<<" is coloured red "<<endl;
colour[p]=red;
flag=0;
for(i=1;i<=n;i++)
if(colour[i]!=red)
flag=1;
cout<<endl<<endl<<endl;
}
output();
}

void main()
{
dijkstra d;
d.function();
}

Conbtoh program Quick sort

 kali ini saya memposting contoh program Quick short di pertemuan ke-9 praktikum,..
semoga bermanfaat :)

#include <iostream.h> 
#include <conio.h> 
     
    void tampilkan_larik(int data[], int n) 
    { 
     int i; 
     for (i=1;i<=n;i++) 
     cout<<data[i]<<" "; 
     cout<<"\n"; 
    } 
     
    int partisi (int data[], int awal, int akhir) 
    { 
     int x,i,j,simpan; 
     
     //x=data[awal]; 
     i=awal; 
     j=akhir; 
     
       while(1) 
       { 
       while(data[i]<data[awal]) 
       i=i+1; 
     
        while(data[j]>data[awal]) 
         j=j-1; 
     
        if (i<j) 
        { 
        //tukarkan data 
       simpan=data[i]; 
        data[i]=data[j]; 
       data[j]=simpan; 
        } 
       else 
          return j; 
       } 
    } 
     
    void quick_sort(int data[], int awal, int akhir) 
    { 
     int q; 
     if(awal<akhir) 
     { 
      q=partisi(data,awal,akhir); 
      quick_sort(data,awal,q); 
      quick_sort(data, q+1,akhir); 
     } 
    } 
     
    int main() 
    { 
     int i,j,n,data[100]; 
     
       cout<<"masukkan banyak data= ";cin>>n; 
       for(i=1;i<=n;i++) 
       { 
        cout<<"data ke-"<<i<<" = ";cin>>data[i]; 
       } 
     
     cout<<"Data sebelum diurut: "<<endl; 
     for(j=1;j<=n;j++) 
     { 
      cout<<data[j]<<" "; 
     } 
     quick_sort(data,1,n); 
     
     //hasil pengurutan 
     cout<<endl; 
     cout<<endl; 
     cout<<"hasil pengurutan:\n"; 
     tampilkan_larik(data,n); 
     getch(); 
    } 

Grafika kom (7)

#include <windows.h> 
#include <stdio.h> 
#include <stdlib.h> 
#include <string.h>
#include <stdarg.h> 
#include <glut.h>
//#include <glu.h>


  
 void init(void)
{
   glClearColor (0.0, 0.0, 0.0, 0.0);
   glShadeModel (GL_FLAT);
}
void display(void)
{
   glClear (GL_COLOR_BUFFER_BIT);
   glColor3f (1.0, 1.0, 1.0);
   glLoadIdentity ();
   gluLookAt (0.5, 2.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
   glutWireCube(0.6);

   glFlush ();
}
void reshape (int w, int h)
{
   glViewport (0, 0, (GLsizei) w, (GLsizei) h);
   glMatrixMode (GL_PROJECTION);
   glLoadIdentity ();
   glFrustum (-1.0, 1.0, -1.0, 1.0, 2.0, 20.0);
   glMatrixMode (GL_MODELVIEW);
}
int main(int argc, char** argv)
{
   glutInit(&argc, argv);
   glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
   glutInitWindowSize (500, 500);
   //glutInitWindowPosition (100, 100);
   glutCreateWindow (argv[0]);
   init ();
   glutDisplayFunc(display);
   glutReshapeFunc(reshape);
   glutMainLoop();
   return 0;
}

grafika kom (prak8)

#include <windows.h> 
#include <stdio.h> 
#include <stdlib.h> 
#include <string.h>
#include <stdarg.h> 
#include <glut.h>
//#include <glu.h>


float _angle = 45.0f;
//draws the 3D scene
void initRendering() {
    glEnable(GL_DEPTH_TEST);
    glEnable(GL_COLOR_MATERIAL);
    glEnable(GL_LIGHTING);
    glEnable(GL_LIGHT0);
    glShadeModel(GL_SMOOTH);
}

void handleResize(int w, int h) {
    glViewport(0, 0, w, h);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluPerspective(45.0, (double)w / (double)h,7.0, 100.0);
    glMatrixMode (GL_MODELVIEW);
}

void drawScene() {
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();

   
    glTranslatef(0.0f, 0.0f, -8.0f);
    GLfloat ambientColor/*variabel*/[] = {0.0f, 0.0f, 0.0f, 1.0f};
    glLightModelfv(GL_LIGHT_MODEL_AMBIENT, ambientColor);/*pemanggilan variabel*/

    GLfloat lightColor0/*variabel*/[] = {1.0f, 1.0f, 1.0f, 2.0f};
    GLfloat lightPos0/*variabel*/[] = {3.0f, 10.0f, 2.0f, 4.0f};
   
    glLightfv(GL_LIGHT0, GL_DIFFUSE, lightColor0);/*pemanggilan variabel*/
    glLightfv(GL_LIGHT0, GL_POSITION, lightPos0);/*pemanggilan variabel*/
    gluLookAt (2.0, 4.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
    glRotatef(_angle, 0.0f, 1.0f, 0.0f);
    glScalef (0.4, 0.4, 0.4);
    glColor3f(0.0,1.0,0.0);
    glutSolidCube(2.0);
    glTranslatef(4.0,0.0,0.0);
    glutSolidCube(2.0);
    glTranslatef(-2.0,2.0,0.0);
    glutSolidCube(2.0);
    glColor3f(1.0,0.0,0.0);
    glTranslatef(0.0,2.0,0.0);
    glutSolidTeapot(1.0);
    glFlush ();
    glEnd();
    glutSwapBuffers();
   
}

void update(int value){
    _angle += 9.0f;
    if(_angle > 360){
        _angle -= 360;
    }
    glutPostRedisplay();//tell glut that the display changed
    //Tell glut to call update
    glutTimerFunc(25, update, 0);
}
int main(int argc, char** argv) {
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
    glutInitWindowSize(300, 300);
    glutCreateWindow("praktikum_8");
    initRendering();
    glClearColor(1,1,1,0);
    glutDisplayFunc(drawScene);
    glutReshapeFunc(handleResize);
    glutTimerFunc(25, update,0);
    glutMainLoop();
    return 0;
}

Grafika komputer (prak9)

#include <windows.h> 
#include <stdio.h> 
#include <stdlib.h> 
#include <string.h>
#include <stdarg.h> 
#include <glut.h>
//#include <glu.h>



void initRendering() {
    glEnable(GL_DEPTH_TEST);
    glEnable(GL_COLOR_MATERIAL);
    glEnable(GL_LIGHTING);
    glEnable(GL_LIGHT0);
    glEnable(GL_LIGHT1);
    glEnable(GL_NORMALIZE);
    glShadeModel(GL_SMOOTH);
}

void handleResize(int w, int h) {
    glViewport(0, 0, w, h);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluPerspective(45.0, (double)w / (double)h, 1.0, 200.0);
}

float _angle = 60.0f;
void drawScene() {
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    glTranslatef(0.0f, 0.0f, -8.0f);

    GLfloat ambientColor[] = {0.2f, 0.2f, 0.2f, 1.0f};
    glLightModelfv(GL_LIGHT_MODEL_AMBIENT, ambientColor);

    GLfloat lightColor0[] = {0.5f, 0.5f, 0.5f, 1.0f};
    GLfloat lightPos0[] = {4.0f, 0.0f, 8.0f, 1.0f};
    glLightfv(GL_LIGHT0, GL_DIFFUSE, lightColor0);
    glLightfv(GL_LIGHT0, GL_POSITION, lightPos0);
//radius
    GLfloat lightColor1[] = {0.5f, 0.2f, 0.2f, 1.0f};

    GLfloat lightPos1[] = {-1.0f, 0.5f, 0.5f, 0.0f};
    glLightfv(GL_LIGHT1, GL_DIFFUSE, lightColor1);
    glLightfv(GL_LIGHT1, GL_POSITION, lightPos1);

    glRotatef(_angle, 0.0f, 1.0f, 0.0f);
    glColor3f(1.0f, 1.0f, 0.0f);
    glBegin(GL_QUADS);
   
    //Front
    //glNormal3f(0.0f, 0.0f, 1.0f);
    //glNormal3f(-1.0f, 0.0f, 1.0f);
    glVertex3f(-1.5f, -1.0f, 1.5f);
    glNormal3f(1.0f, 0.0f, 1.0f);
    glVertex3f(1.5f, -1.0f, 1.5f);
    glNormal3f(1.0f, 0.0f, 1.0f);
    glVertex3f(1.5f, 1.0f, 1.5f);
    glNormal3f(-1.0f, 0.0f, 1.0f);
    glVertex3f(-1.5f, 1.0f, 1.5f);
    glEnd();
   
    glBegin(GL_QUADS);
   
    glVertex3f(1.5f, -1.0f, 1.5f);
    glVertex3f(1.5f, -1.0f, -1.5f);
    glVertex3f(1.5f, 1.0f, -1.5f);
    glVertex3f(1.5f, 1.0f, 1.5f);


    glEnd();
   
    glutSwapBuffers();
}

void update(int value) {
    _angle += 1.5f;
    if (_angle > 360) {
        _angle -= 360;
    }
   
    glutPostRedisplay();
    glutTimerFunc(25, update, 0);
}

int main(int argc, char** argv) {
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
    glutInitWindowSize(400, 400);
    glutCreateWindow("Lighting - videotutorialsrock.com");
    initRendering();
    glutDisplayFunc(drawScene);
    glutReshapeFunc(handleResize);   
    glutTimerFunc(25, update, 0);
   
    glutMainLoop();
    return 0;
}

praktikum SA ke 8 (minmax)

#include <cstdlib>
#include <iostream>

using namespace std;
void MinMaks2(int A[], int i, int j, int &min, int &maks)
{
     int min1, min2, maks1, maks2, k;
   
     if (i=j)
     {
             min = A[i];
             maks = A[i];
             }
     else
     {
         if (i = j-1)
         {
               if (A[i] < A[j])
               {
                        maks = A[j];
                        min = A[i];
                        }
               else
               {
                   maks = A[i];
                   min = A[j];
                   }
                   }
         else
         {
             k = ((i+j)/2);
             MinMaks2(A, i, k, min1, maks1);
             MinMaks2(A, i+1, j, min2, maks2);
             }
             if (min1 < min2)
             {
                 min = min1;
                 }
               
             else
             {
                 min = min2;
                 }
                 }
             if (maks1 < maks2)
             {
                 maks = maks2;
                       }
             else
             {
                 maks = maks1;
                 }
                 }
               

int main(int argc, char *argv[])
{
    int A[] = {3,6,1,10,8,19,2,4};
    int i = 0;
    int j = 7;
    int min, maks;
    MinMaks2(A, i, j, min, maks);
    cout<<"min = "<<min<<endl;
    cout<<"maks = "<<maks<<endl;
    system("PAUSE");
    return EXIT_SUCCESS;
}

Tuesday, 20 May 2014

Algoritma discrap

#include <cstdlib>
#include <iostream>
#define MAX 20
#define INFINITY 9999

using namespace std;
class dijkstra
{
      private:
      int n;
      int graph[MAX][MAX];
      int colour[MAX];
      int start;
      int distance[MAX];
      int predecessor[MAX];
      enum {green,yellow,red};
public:
       void read_graph();
       void initialize();
       int select_min_distance_lable();
       void update(int);
       void output();
       void function();

};
void dijkstra::read_graph()
{
     cout<<"Enter the no. of nodes in the graph ::";
     cin>>n;
     cout<<"Enter the adjacency matrix for the graph ::\n";
     int i,j;
     for(i=1;i<=n;i++)
     for(j=1;j<=n;j++)
     cin>>graph[i][j];
     for(i=1;i<=n;i++)
     colour[i]=green;
     cout<<"Enter the start vertex ::";
     cin>>start;
}
void dijkstra::initialize()
{
     for(int i=1;i<=n;i++)
{
     if(i==start)
     distance[i]=0;
     else
     distance[i]=INFINITY;
}
     for(int j=1;j<=n;j++)
{
     if(graph[start][j]!=0)
     predecessor[j]=start;
else
    predecessor[j]=0;
}
}
    int dijkstra::select_min_distance_lable()

{
    int min=INFINITY;
    int p=0;
    for(int i=1;i<=n;i++)
{
            if(colour[i]==green)
{
            if(min>=distance[i])
{
            min=distance[i];
            p=i;
}
}
}
return p;
}
         void dijkstra::update(int p) // p is a yellow colour node
{
         cout<<"\nupdated distances are ::\n";
         for(int i=1;i<=n;i++)
{
         if(colour[i]==green)
{
         if(graph[p][i]!=0)
{
         if(distance[i]>graph[p][i]+distance[p])
{
         distance[i]=graph[p][i]+distance[p];
         predecessor[i]=p;
}
}
}
         cout<<distance[i]<<'\t';
}
}
void dijkstra::output()

{
                       cout<<"****** The final paths and the distacnes are ******\n\n";
                       for(int i=1;i<=n;i++)
{
                       if(predecessor[i]==0 && i!=start)
{
                       cout<<"path does not exists between "<<i<<" and thestart vertex "<<start<<endl;
                       exit(1);
}
          cout<<"path for node “<<i<<” is ::\n";
          int j=i;
          int array[MAX];
          int l=0;
          while(predecessor[j]!=0)
{
          array[++l]=predecessor[j];
          j=predecessor[j];
}
          for(int k=l;k>=1;k=-k)
          cout<<array[k]<<"->";
          cout<<i<<endl;
          cout<<"distance is "<<distance[i]<<endl<<endl<<endl;
}
}
void dijkstra::function()
{
              cout<<"\n**********************************************************************\n";
              cout<<"This program is to implement dijkstra’s algorithm using colourcodes \n";
              cout<<"**********************************************************************\n\n";
              read_graph();
              initialize();
//repeate until all nodes become red
          int flag=0;
          int i;
             cout<<"\n\n******** The working of the algorithm is **********\n\n";
             for(i=1;i<=n;i++)
             if(colour[i]!=red)
             flag=1;
             cout<<"The initial distances are ::\n";
             for(i=1;i<=n;i++)
             cout<<distance[i]<<'\t';
             cout<<endl;
             while(flag)
{
             int p=select_min_distance_lable();
             cout<<"\nThe min distance lable that is coloured yellow is "<<p;
             colour[p]=yellow;
             update(p);
             cout<<"\nnode "<<p<<" is coloured red "<<endl;
             colour[p]=red;
             flag=0;
             for(i=1;i<=n;i++)
             if(colour[i]!=red)
             flag=1;
             cout<<endl<<endl<<endl;
}
             output();
}
int main(int argc, char *argv[])
{   dijkstra d;
    d.function();

    system("PAUSE");
    return EXIT_SUCCESS;
}

Mencari nilai minmax

#include <cstdlib>
#include <iostream>

using namespace std;
void MinMaks2(int A[],int i,int j,int k,int min, int maks){
int min1,min2,maks1,maks2;

if(i==j){
min=A[i];
maks=A[i];
}
else if(i==j-1){
if (A[i]<A[i]){
maks=A[j];
min=A[i];
}
}
else{
maks=A[i];
maks=A[j];
}
}
else {
k =(i+j)/2;
MinMaks2(A, i, k, min1, maks1);
MinMaks2(A, k+1, j, min2, maks2);

if(min1 < min2){
min = min1;
}
else {
min = min2;

if(maks1 < maks2);
maks = maks1;
else {
maks = maks2;
}
}

int main(int argc, char *argv[])
{
int data[] = {3,6,1,20,8,19,2,4};
int min, maks;
int i = 0;
int j = 7;
MinMaks2(data, i,j, min, maks);
cout << "Nilai min = " << min << endl;
cout << "Nilai maks = " << maks <<endl;
system("PAUSE");
return EXIT_SUCCESS;
}
Agus
 

Mencari total keuntungan dengan metode knapsack

#include <cstdlib>
#include <iostream>

using namespace std;

 void SolveFractionalKnapsack (
 int p, int w, float W, int n, int x ,int i,  float TotalUntung, float Kapasitas,
 bool MasihMuatUtuh)

 for (i=1;i<n;i++){
 x1=0;
      Kapasitas =w;
      TotalUntung=0;
      i=1;
      MasihMuatUntung=true;
     
      while(i<=n) &and (MasihMuatUtuh);
      if w1<= Kapasitas;
      xi=1;
      TotalUntung=TotalUntung + p1;
      Kapasitas= Kapasitas -w1;
      i+i+1;
     
      else {
      MashMuatUntung=false;
      endif;
      endwhile;
      (i>n &or !=MasihMuatItuh)
      if (i<=n)
      x1=Kapasitas/w1;
      TotalUntung=TotalUntungt +x1*p1;
      endif;
}}


int main(int argc, char *argv[])
{  
    //int p[]=(60,16,15,10)
    //int w[]=(10,8,3,5)
    //float w=16;
    //int=4;
   
   
    system("PAUSE");
    return EXIT_SUCCESS;
}

Konversi Suhu

#include <iostream> #include <conio.h> //#include <cstdlib> //#include <iostream.h> void main( float Celcius, Kelvi...