TUI1E3
PENGANTAR ALGORITMA
Notasi Algoritmik
Teks Algoritma terdiri atas tiga bagian :
1. Judul
{ Ini adalah teks dalam bahasa Indonesia untuk memudahkan
pembacaan teks algoritma }
{ Spesifikasi teks algoritmik secara umum }
2. Kamus
{ Pada bagian ini, dilakukan pendefinisian nama kostanta, nama
variabel, spesifikasi prosedur, spesifikasi fungsi }
3. Algoritma
{ Pada bagian ini, semua teks yang tidak dituliskan di antara tanda
kurung kurawal harus dianggap sebagai notasi algoritmik }
2
3
Pada setiap bagian akan didefinisikan &
dituliskan Nama atau Komentar
3
4
5
6
Definisi
Bagian teks algoritma mendefinisikan:
K •Nama type
A •Nama konstanta
M •Nama informasi (nama variabel)
•Nama prosedur, sekaligus spesifikasi
U •Nama fungsi, sekaligus spesifikasi
S
6
Type
7
Definisi:
Pola representasi suatu data dalam komputer
Kegunaan:
Mendefinisikan objek yang akan diprogram
Analogi:
Tipe benda : buah, hewan, batuan, ikan dsb
Tipe data : string, integer, float dsb.
7
9
Type Dasar
a. Bulat (int)
b. Pecahan (double/float)
c. Karakter (char)
d. Gabungan Karakter ( string )
9
Type Numerik 10
2. Urutan prioritas :
1 – 2 * 3 div 4 mod 5 + 6
a. Kurung didahulukan = 1 – 6 div 4 mod 5 + 6
b. *, /, div, mod sejajar. = 1 – 1 mod 5 + 6
c. + dan – sejajar = 1–1+6
= 0+6
d. Jika muncul berurutan &
= 6
sejajar, mulai dari kiri
12
13
Jarak
Pernyataan :
Dibaca dua buah harga v (kecepatan, m/detik) dan t (waktu,
C detik), yang mewakili koefisien persamaan gerak lurus
T Spesifikasi:
O Input : v (kecepatan, m/detik), integer
H
dan t (waktu, detik), integer
Proses : menghitung S = v * t
Output : S (jarak dalam meter ), integer
13
14
Algoritma Jarak
Program Jarak
{ menghitung jarak yang ditempuh, dg input v dan
t}
Kamus
v : integer { kecepatan }
t : integer { waktu }
s : integer { jarak}
Algoritma
input v
input t
s=v*t
output(‘ Jarak yang ditempuh’,s)
14
15
Pengenalan Bahasa C
printf(“Da-da-da”);
16
Recall:
0 or 1
17
Machine Language
10100110 01110110
00100110 00000000
11111010 11111010
01001110 10100110
11100110 10010110
11001110 00101110
10100110 01001110
11111010 01100110
01001110 10000110
etc...
18
High-Level Language
10100110 01110110
#include <stdio.h> 00100110 00000000
11111010 11111010
int main() 01001110 10100110
{ 11100110 10010110
printf(“Da-da-da”); 11001110 00101110
10100110 01001110
return 0; 11111010 01100110
01001110 10000110
}
etc...
Mengapa C?
History of C
History of C
Struktur Program C
24
Tipe Data
25
1. Operator aritmetika
26
3. Operator relasi
28
3. Operator logika
29
Example: Da-da-da
Instructions: C Program:
#include <stdio.h>
int main()
{
output “Da-da-da” printf(“Da-da-da”);
return 0;
}
30
Example: Da-da-da
C#include
Program:
<stdio.h>
“Skeleton” int main()
{
printf(“Da-da-da”);
return 0;
}
31
Example: Da-da-da
Also: C Program:
#include <stdio.h>
return 0;
} }
Not recommended
32
Example: Da-da-da
Also: C Program:
#include <stdio.h>
return 0; return 0;
} }
Assumes int
33
Example: Da-da-da
Also: C Program:
#include <stdio.h>
return 0; return 0;
} }
Example: Da-da-da
C Program:
#include <stdio.h>
Example: Da-da-da
C Program:
#include <stdio.h>
int main()
{
Kurung Buka beginning printf(“Da-da-da”);
and end of a block of
instructions. return 0;
}
36
Example: Da-da-da
C Program:
#include <stdio.h>
Instruction (function
int main()
call) to output “Da- {
da-da” printf(“Da-da-da”);
return 0;
}
37
Example: Da-da-da
C Program:
#include <stdio.h>
return 0;
}
38
Simple C Phrases
Example 1: Float
Instructions: C Program:
#include <stdio.h>
int main()
{
float x;
let x be equal to 4.5
x = 4.5;
output the value of x
printf(“%f”, x);
return 0;
}
39
Simple C Phrases
Example 1: Float
C Program:
#include <stdio.h>
int main()
Declares that x is a {
float x;
variable which can be
assigned a numerical x = 4.5;
value.
printf(“%f”, x);
return 0;
}
40
Simple C Phrases
Example 1: Float
C Program:
#include <stdio.h>
int main()
Assigns the value 4.5 into {
the variable x. float x;
(Remember: “right value is
assigned to left variable”) x = 4.5;
printf(“%f”, x);
return 0;
}
41
Simple C Phrases
Example 1: Float
C Program:
#include <stdio.h>
int main()
{
float x;
Outputs the value of x.
(“ %f ” for float) x = 4.5;
printf(“%f”, x);
return 0;
}
Simple C Phrases
42
Example 2: Sum
Instructions: C Program:
#include <stdio.h>
int main()
{
float x;
float y;
input the value of x
input the value of y scanf(“%f”, &x);
scanf(“%f”, &y);
output “The sum is “
output the value of x + y printf(“The sum is “);
output a new line printf(“%f”, x + y);
printf(“\n”);
return 0;
}
Simple C Phrases
43
int main()
{
float x;
x and y are both float y;
numeric variables
scanf(“%f”, &x);
scanf(“%f”, &y);
return 0;
}
Simple C Phrases
44
int main()
{
float x;
float y;
scanf(“%f”, &x);
scanf(“%f”, &y);
Input the values of x
and y respectively printf(“The sum is “);
(note the ampersand!) printf(“%f”, x + y);
printf(“\n”);
return 0;
}
Simple C Phrases
45
int main()
{
float x;
float y;
scanf(“%f”, &x);
scanf(“%f”, &y);
return 0;
}
Simple C Phrases
46
int main()
{
float x;
float y;
scanf(“%f”, &x);
scanf(“%f”, &y);
return 0;
}
Simple C Phrases
47
int main()
{
float x;
float y;
scanf(“%f”, &x);
scanf(“%f”, &y);
return 0;
}
Simple C Phrases : “if”
48
Example 3: Mark
#include <stdio.h>
int main()
{
char name[50]; /* The next output depends
float mark; on the mark. */
Example 3: Mark
#include <stdio.h>
Outputs a “prompt” so the
user knows what the
int main() program wants.
{
char name[50]; /* The next output depends
float mark; on the mark. */
Example 3: Mark
#include <stdio.h> Inputs the string content
of the variable name.
int main()
{ (Note: no ampersand!)
char name[50]; /* The next output depends
float mark; on the mark. */
Example 3: Mark
#include <stdio.h> /* The next output depends
on the mark. */
int main()
{ if (mark < 50)
char name[50]; {
float mark; printf("Sorry. You failed.\n");
}
printf("What is your name? "); else
scanf("%s", name); {
printf("Yey! You passed!\n");
printf("What is your mark? "); }
scanf("%f", &mark);
return 0;
printf("\n"); }
printf("Hi, ");
printf("%s", name); (The usual. Note the
printf("!\n");
ampersand for floats!)
Simple C Phrases : “if”
53
Example 3: Mark
#include <stdio.h> /* The next output depends
on the mark. */
int main()
{ if (mark < 50)
char name[50]; {
float mark; printf("Sorry. You failed.\n");
}
printf("What is your name? "); else
scanf("%s", name); {
printf("Yey! You passed!\n");
printf("What is your mark? "); }
scanf("%f", &mark);
return 0;
printf("\n"); }
printf("Hi, ");
printf("%s", name);
printf("!\n"); Outputs the string name.
(Note the “%s”)
Simple C Phrases : “if”
54
Example 3: Mark
This block of instructions is
#include <stdio.h>
performed if the comparison is
int main() true.
{
char name[50]; /* The next output depends
float mark; on the mark. */
Example 3: Mark
#include <stdio.h> /* The next output depends
on the mark. */
int main()
{ if (mark < 50)
char name[50]; {
float mark; printf("Sorry. You failed.\n");
}
printf("What is your name? "); else
scanf("%s", name); {
printf("Yey! You passed!\n");
printf("What is your mark? "); }
scanf("%f", &mark);
return 0;
printf("\n"); }
printf("Hi, ");
printf("%s", name);
printf("!\n"); This block of instructions
is performed if the
comparison is false.
57
Contoh Program
58
59
Summary