Anda di halaman 1dari 58

1

TUI1E3

PENGANTAR ALGORITMA

Fakultas Teknik Elektro


2

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

Nama adalah sesuatu yang


memberikan identitas.

Komentar dituliskan diantara tanda //

Teks yang dituliskan diluar // kurawal


adalah teks algoritma

3
4

Nama merupakan sesuatu yang dipakai sebagai


identifikasi :
• Modul program, algoritma, skema program, dll
• Fungsi
• Prosedur
• Type
• Tempat penyimpanan ( misalkan nama variabel  isi bisa berubah-
ubah)
• Konstanta  nilainya tetap dan tidak berubah-ubah
5

Bagian teks algoritma tempat mendefinisikan


apakah teks tsb adalah program, prosedur,
fungsi, atau modul
J
U Setelah judul tuliskan spesifikasi singkat dari teks
D algoritma.

U Bagian judul identik dengan judul buku dan


L intisari pada suatu teks ilmiah dalam suatu
makalah berbahasa Indonesia

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

a. Nama : integer { untuk bilangan bulat }


b. Domain : …,-3,-2,-1,0,1,2,3,…
c. Konstanta:
4 -120 10 0 -3

a.Nama : real { untuk bilangan pecahan }


b.Domain : semua bilangan pecahan
c.Konstanta:
4.0 -1/20 10.990.1 -3/4
10
11

Operator Type Numerik

No Nama Arti Tipe Hasil Operasi

1 * Kali Tergantung Operand

2 / Bagi Selalu Real

3 + Tambah Tergantung Operand

4 - Kurang Tergantung Operand

5 ^ Pangkat Tergantung Operand

6 Abs Harga Mutlak Tergantung Operand


12

Operator Type Numerik

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

O beraturan. Harus dihitung dan dituliskan hasilnya, jarak yang


ditempuh benda yang bergerak lurus beraturan dengan
N kecepatan v tersebut dalam waktu t.

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

Sr tuktur Dasar Bahasa Pemrograman C


C “baby-talk” phrases

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...

Source code Executable code

• Compilers and linkers menterjemahkan bahasa tingkat


tinggi kedalam bahasa mesin (Executable code).
19

Mengapa C?

Bahasa C tersedia hampir di semua jenis computer.


Kode bahasa C sifatnya adalah portable dan fleksibel untuk
semua jenis computer.
Bahasa C hanya menyediakan sedikit kata-kata kunci, hanya
terdapat 32 kata kunci.
Proses executable program bahasa C lebih cepat
Dukungan pustaka yang banyak.
C adalah bahasa yang terstruktur
Bahasa C termasuk bahasa tingkat menengah
20

History of C

Bahasa C merupakan perkembangan dari bahasa BCPL yang


dikembangkan oleh Martin Richards pada tahun 1967. Selanjutnya bahasa
ini memberikan ide kepada Ken Thompson yang kemudian
mengembangkan bahasa yang disebut bahasa B pada tahun 1970.
Perkembangan selanjutnya dari bahasa B adalah bahasa C oleh Dennis
Ricthie sekitar tahun 1970-an di Bell Telephone Laboratories Inc. (sekarang
adalah AT&T Bell Laboratories). Bahasa C pertama kali digunakan di
computer Digital Equipment Corporation PDP-11 yang menggunakan
system operasi UNIX. Hingga saat ini penggunaan bahasa C telah merata
di seluruh dunia. Hampir semua perguruan tinggi di dunia menjadikan
bahasa C sebagai salah satu mata kuliah wajib. Selain itu, banyak bahasa
pemrograman populer seperti PHP dan Java menggunakan sintaks dasar
yang mirip bahasa C. Oleh karena itu, kita juga sangat perlu
21

History of C

CPL Combined Programming Language (Barron et al., 1963)


BCPL Basic CPL (Richards, 1969)
B (Thompson, 1970)
C K&R C (Ritchie, 1972)
ANSI C American National Standards Institute C (X3J11, 1989)
C9X (JTC1/SC22/WG14, ISO/IEC 9899)
22

Aturan Penulisan Program C

1.Program C dapat ditulis tanpa dilihat dari kolom ke berapa


2.Bersifat case sensitif
3.Setiap statement diakhiri ; (titik koma)
4.Program diawali dengan { (kurawal buka) dan diakhiri
dengan } (kurawal tutup)
5.Komentar diletakkan diantara /* dan */. Komentar tidak akan
dieksekusi oleh compiler. Bisa juga dengan //. Sehingga
semua kalimat di belakangnya menjadi komentar.
23

Struktur Program C
24

Tipe Data
25

Jenis – Jenis Operator

1. Operator aritmetika
26

Jenis – Jenis Operator

2. Operator assignment (penugasan)


27

Jenis – Jenis Operator

3. Operator relasi
28

Jenis – Jenis Operator

3. Operator logika
29

Struktur Dasar Program C

Example: Da-da-da
Instructions: C Program:
#include <stdio.h>

int main()
{
output “Da-da-da” printf(“Da-da-da”);

return 0;
}
30

Struktur Dasar Program C

Example: Da-da-da

C#include
Program:
<stdio.h>
“Skeleton” int main()
{
printf(“Da-da-da”);

return 0;
}
31

Struktur Dasar Program C

Example: Da-da-da
Also: C Program:
#include <stdio.h>

void main() int main()


{ {
printf(“Da-da-da”);

return 0;
} }

Not recommended
32

Struktur Dasar Program C

Example: Da-da-da
Also: C Program:
#include <stdio.h>

main() int main()


{ {
printf(“Da-da-da”);

return 0; return 0;
} }

Assumes int
33

Struktur Dasar Program C

Example: Da-da-da
Also: C Program:
#include <stdio.h>

main() int main()


{ {
printf(“Da-da-da”);

return 0; return 0;
} }

Warning messages: “Return


value expected”
34

Struktur Dasar Program C

Example: Da-da-da
C Program:
#include <stdio.h>

Includes standard input / int main()


output library of {
printf(“Da-da-da”);
functions.
Read: “Hash-include” return 0;
}
35

Struktur Dasar Program C

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

Dasar Struktur Dasar Program C

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

Dasar Struktur Dasar Program C

Example: Da-da-da
C Program:
#include <stdio.h>

“Statements” (lines of int main()


instructions) end with a {
semi-colon (;) printf(“Da-da-da”);

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

Example 2: Sum C Program:


#include <stdio.h>

int main()
{
float x;
x and y are both float y;
numeric variables
scanf(“%f”, &x);
scanf(“%f”, &y);

printf(“The sum is “);


printf(“%f”, x + y);
printf(“\n”);

return 0;
}
Simple C Phrases
44

Example 2: Sum C Program:


#include <stdio.h>

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

Example 2: Sum C Program:


#include <stdio.h>

int main()
{
float x;
float y;

scanf(“%f”, &x);
scanf(“%f”, &y);

Outputs the string: “The printf(“The sum is “);


sum is “ printf(“%f”, x + y);
printf(“\n”);

return 0;
}
Simple C Phrases
46

Example 2: Sum C Program:


#include <stdio.h>

int main()
{
float x;
float y;

scanf(“%f”, &x);
scanf(“%f”, &y);

Outputs the sum printf(“The sum is “);


x+y printf(“%f”, x + y);
printf(“\n”);

return 0;
}
Simple C Phrases
47

Example 2: Sum C Program:


#include <stdio.h>

int main()
{
float x;
float y;

scanf(“%f”, &x);
scanf(“%f”, &y);

printf(“The sum is “);


Outputs a newline printf(“%f”, x + y);
printf(“\n”);

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. */

printf("What is your name? "); if (mark < 50)


scanf("%s", name); {
printf("Sorry. You failed.\n");
printf("What is your mark? "); }
scanf("%f", &mark); else
{
printf("\n"); printf("Yey! You passed!\n");
printf("Hi, "); }
printf("%s", name);
printf("!\n"); return 0;
}
Simple C Phrases : “if”
49

Example 3: Mark Declares that name is a


variable which can
#include <stdio.h> contain a “string”
int main()
(i.e. words, names, etc.)
{
char name[50]; /* The next output depends
float mark; on the mark. */

printf("What is your name? "); if (mark < 50)


scanf("%s", name); {
printf("Sorry. You failed.\n");
printf("What is your mark? "); }
scanf("%f", &mark); else
{
printf("\n"); printf("Yey! You passed!\n");
printf("Hi, "); }
printf("%s", name);
printf("!\n"); return 0;
}
Simple C Phrases : “if”
50

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. */

printf("What is your name? "); if (mark < 50)


scanf("%s", name); {
printf("Sorry. You failed.\n");
printf("What is your mark? "); }
scanf("%f", &mark); else
{
printf("\n"); printf("Yey! You passed!\n");
printf("Hi, "); }
printf("%s", name);
printf("!\n"); return 0;
}
Simple C Phrases : “if”
51

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. */

printf("What is your name? "); if (mark < 50)


scanf("%s", name); {
printf("Sorry. You failed.\n");
printf("What is your mark? "); }
scanf("%f", &mark); else
{
printf("\n"); printf("Yey! You passed!\n");
printf("Hi, "); }
printf("%s", name);
printf("!\n"); return 0;
}
Simple C Phrases : “if”
52

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 Comments: everything


#include <stdio.h> between /* and */ are
ignored by the compiler.
int main()
{
char name[50]; /* The next output depends
float mark; on the mark. */

printf("What is your name? "); if (mark < 50)


scanf("%s", name); {
printf("Sorry. You failed.\n");
printf("What is your mark? "); }
scanf("%f", &mark); else
{
printf("\n"); printf("Yey! You passed!\n");
printf("Hi, "); }
printf("%s", name);
printf("!\n"); return 0;
}
Simple C Phrases : “if”
55

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. */

printf("What is your name? "); if (mark < 50)


scanf("%s", name); {
printf("Sorry. You failed.\n");
printf("What is your mark? "); }
scanf("%f", &mark); else
{
printf("\n"); printf("Yey! You passed!\n");
printf("Hi, "); }
printf("%s", name);
printf("!\n"); return 0;
}
Simple C Phrases : “if”
56

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

Basic structure of a C program


Simple C “phrases”
Declaring numerical variables (floats)
Declaring string variables
Input (scanf) and Output (printf)
comments
The “if...else...” condition

Anda mungkin juga menyukai