Monthly Archives: February 2012

Dijkstra Algorithm On C

Dijkstra algorithm or used to be called as greedy algorithm is an algorithm that is usually used  for searching best path on several points. for example, suppose if there are 6 cities in a country (A, B, C, D, E, F), of course from one to the others will be separated by far distance. if i will go from city A to city D, i will need far distance. So, to get the shortest path, i need to do optimization using Dijkstra algorithm.As i know that this algorithm is the best one on optimization. Usually Dijkstra is represented by using graph with weight on each point to represent the distance, or cost to reach it.

to implement of it, here is the source code


#include <stdio.h>
#define inf 999
#define n 7
#define mem 1
#define nonm 0
int main(){
int weight[n][n]={0,2,4,20,12,5,3,
 2,0,2,5,7,40,9,
 4,2,0,3,6,5,40,
 20,5,3,0,6,10,32,
 12,7,6,6,0,1,20,
 5,40,5,10,1,0,10,
 3,9,40,32,20,10,0};

int s,t,pre[n];
int dist[n],perm[n];
int cur,i,k,dc,z;
int smal,newd;
char asal,tujuan;

printf("Simpul asal = ");scanf("%c",&asal);

switch (asal)
 {
 case 'a':
 s=0;
 break;
 case 'b':
 s=1;
 break;
 case 'c':
 s=2;
 break;
 case 'd':
 s=3;
 break;
 case 'e':
 s=4;
 break;
 case 'f':
 s=5;
 break;
 case 'g':
 s=6;
 break;
 default :
 printf("ERROR !!! \n");
 }

printf("Simpul tujuan = ");scanf("%c",&tujuan);scanf("%c",&tujuan);
 switch (tujuan)
 {
 case 'a':
 t=0;
 break;
 case 'b':
 t=1;
 break;
 case 'c':
 t=2;
 break;
 case 'd':
 t=3;
 break;
 case 'e':
 t=4;
 break;
 case 'f':
 t=5;
 break;
 case 'g':
 t=6;
 break;
 default :
 printf("ERROR !!! \n");
 }
 for (i=0;i<=6;i++){
 perm[i]=nonm;
 dist[i]=inf;
 }
perm[s]=mem;
dist[s]=0;
cur=s;
 while (cur!=t)
 {
 smal=inf;
 dc=dist[cur];
 for(i=0;i<=6;i++){
 if (perm[i] == nonm){
 newd=dc+weight[cur][i];
 if (newd<dist[i]){
 dist[i]=newd;
 pre[i]=cur;
 }
 if (dist[i]<smal){
 smal=dist[i];
 k=i;
 }
 }
 }
 cur=k;
 perm[cur]=mem;
 }
 printf("PATH = %i\n",dist[t]);
return (0);
}

5th Share for Visual Basic

Next application is called comboAPP

This application shows how to use combo box and getting value from it.

Here is the interface view :

here is the source code


Public Class Form1

Private Sub cmbpil_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmbpil.SelectedIndexChanged

Select Case cmbpil.SelectedIndex

Case 1 : Label3.Text = cmbpil.SelectedItem

Case 2 : Label3.Text = cmbpil.SelectedItem

Case 3 : Label3.Text = cmbpil.SelectedItem

Case 4 : Label3.Text = cmbpil.SelectedItem

Case 5 : Label3.Text = cmbpil.SelectedItem

End Select

End Sub

End Class

4th Share for Visual Basic

The 4th application

It is called aplikasi4. This application show how to use the check box in Visual Basic

This is the interface view :

here is the source code


Public Class Form1

Private Sub btproses_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btproses.Click

If chk1.Checked Then

Label4.Text = "1"

Label5.Text = chk1.Text

ElseIf chk2.Checked Then

Label4.Text = "2"

Label5.Text = chk2.Text

ElseIf chk3.Checked Then

Label4.Text = "3"

Label5.Text = chk3.Text

ElseIf chk4.Checked Then

Label4.Text = "4"

Label5.Text = chk4.Text

End If

End Sub

End Class

3rd Share for Visual Basic

Keep on the third application

In this application I’d like to share you about login application. Here is just the simple one. Which is if username and password is matched with the list (already define before). So, it will show message box that success to login. On the other case, it will show message box that is wrong user name or password

Here is the interface view

here is the source code


Public Class Form1

&nbsp;

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btreset.Click

txtpass.Text = ""

txtuser.Text = ""

txtpass.Focus()

End Sub

&nbsp;

Private Sub btcancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btcancel.Click

Dim autentikasi As Boolean

autentikasi = MessageBox.Show("Apakah Anda Yakin Keluar ?", "Informasi", MessageBoxButtons.YesNo, MessageBoxIcon.Question)

If autentikasi = True Then

Close()

Else

&nbsp;

End If

End Sub

&nbsp;

Private Sub btlogin_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btlogin.Click

Dim user As String

Dim pass As String

user = "admin"

pass = "administrator"

If (user = txtuser.Text And pass = txtpass.Text) Then

MessageBox.Show("Selamat Anda Berhasil Masuk Sistem", "Informasi", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1, MessageBoxOptions.DefaultDesktopOnly)

Else

MessageBox.Show("User Name atau Password Salah", "Informasi", MessageBoxButtons.OK, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button1, MessageBoxOptions.DefaultDesktopOnly)

End If

End Sub

&nbsp;

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

txtpass.Focus()

End Sub

End Class

2nd Share for Visual Basic

Keep on to second application

It is called Simple calc

In this application, we will learn about getting value and calculate it to be information.

This is the interface view

Here is the source code

</pre>
Public Class Form1

Private val1 As Integer

Private val2 As Integer

Function getVal1() As Integer

val1 = txtVal1.Text

Return val1

End Function

Function getVal2() As Integer

val2 = txtval2.Text

Return val2

End Function

Private Sub btplus_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btplus.Click

lblhasil.Text = getVal1() + getVal2()

End Sub

Private Sub btmin_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btmin.Click

lblhasil.Text = getVal1() - getVal2()

End Sub

&nbsp;

Private Sub btmulti_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btmulti.Click

lblhasil.Text = getVal1() * getVal2()

End Sub

&nbsp;

Private Sub btper_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btper.Click

lblhasil.Text = getVal1() / getVal2()

End Sub

&nbsp;

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

txtVal1.Text = ""

txtval2.Text = ""

lblhasil.Text = ""

End Sub
<pre>End Class

Starting Again with Visual Basic 2008

Visual Basic is very simple programming language. It can be learned by everyone even though from the beginning. Learning visual basic we only need serious and  patient. If we have already know about algorithm well, god willing it will be faster learning.

For the first time all we need to do is to know about the tool. Here, the tool that we’ll use is Visual Basic 2008 Express Edition. It’s free to use. We only need to download it directly form Microsoft.

Alright, Our first application is called Set_Get_Text. In this application we will learn several components : Button, MessageBox and TextBox and label. In this application we will also learn how to get text from a text box and display it through label or  Textbox even message box.

This is the interface view

this is the source code


Public Class Form1

Public nama

Public Sub getText()

nama = txtnama.Text

End Sub

Private Sub btOke_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btOke.Click

getText()

lblNama.Text = nama

End Sub

Private Sub BtMessage_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtMessage.Click

getText()

MessageBox.Show("Isi TextBox = " + nama, "Informasi", MessageBoxButtons.OK, MessageBoxIcon.Information)

End Sub

End Class