01. Struktur Data Pendahuluan
-
Upload
virginia-dnz-motionvz -
Category
Documents
-
view
229 -
download
0
Transcript of 01. Struktur Data Pendahuluan
-
8/9/2019 01. Struktur Data Pendahuluan
1/24
Struktur Data...
Sistem pengorganisasian data pada memori komputer(RAM) atau media penyimpanan menggunakan teknik:
tumpukan, antrian, pointer, dan senarai berantai.
Teknik-teknik manipulasi data:
tambah (add)hapus (delete)
edit
pengurutan
pohonpencarian
-
8/9/2019 01. Struktur Data Pendahuluan
2/24
Sekilas bahasa C/C++
Bahasa C dibuat oleh Ken Thompson dan Dennis M.Ritchie, tahun 1978,untuk Sistem Operasi Unix oleh Bell
Labs. Didokumentasikan dalam bukuThe C Programming Language
Bahasa C (dan turunannya: C++, Visual C, C#) adalahsalah satu bahasa pemrograman yang paling seringdipakai oleh pemrogram
Memperbolehkan mengakses memori secara manual,menggunakan pointer
Sering dipakai untuk membuat bahasa pemrogramanyang lain, bahkan untuk membuat sistem operasi!
Bahasa C yang digunakan sekarang berdasarkanstandarisasi ANSI tahun 1989
-
8/9/2019 01. Struktur Data Pendahuluan
3/24
Identifier & Tipe Data C Identifier adalah nama (atau pengingat) dari tempat penyimpanan
data di dalam memori komputer. Secara umum dibedakan,
Variabel : isi data bisa diubah
Konstanta : isi data bersifat tetap
-
8/9/2019 01. Struktur Data Pendahuluan
4/24
Beberapa istilah dalam bahasa C
Source code: kode program
Compile (build): pengubahan source code ke dalamobject code (bisa bahasa mesin / assembly)
Executable: program dalam bahasa mesin yang siap
dieksekusi.
Library: fungsi-fungsi yang digunakan pada program
Preprocessor Directive
Dimulai dengan tanda # Header file: file yang berekstensi .h yang disertakan pada
program.
-
8/9/2019 01. Struktur Data Pendahuluan
5/24
Struktur program C
struktur program C:
Preprocessor Directive Function Definitions
Data Structures
Code programs Function Body
#include
#define .int coba();
void main()
{int a;
printf(Hello, world!\n);
a = coba();
}
int coba(){
..
}
-
8/9/2019 01. Struktur Data Pendahuluan
6/24
Contoh program Hello World
#i ncl ude
/ * My f i r st C pr ogr am whi ch pr i nt s Hel l o Wor l d */
i nt mai n ( i nt ar gc, char *ar gv[ ] ){
pr i nt f ( "Hel l o Wor l d! \ n") ;r et ur n 0;
}
Preprocessor
Library command
main() means start here
Comments are good
Return 0 from main means our program
finished without errorsBracketsdefine code blocks
-
8/9/2019 01. Struktur Data Pendahuluan
7/24
Keywords of C Flow control (6) i f , el se, r et ur n,
swi t ch, case, def aul t Loops (5) f or , do, whi l e, br eak,
cont i nue Common types (5) i nt , f l oat ,
doubl e, char , voi d Structures (2) st r uct , t ypedef
Sizing things (1) si zeof Rare but still useful types (7) ext er n,si gned, unsi gned, l ong, shor t ,st at i c, const
Evil keywords which we avoid (1) got o
-
8/9/2019 01. Struktur Data Pendahuluan
8/24
Variabel
Kita harus mendeklarasikan tipe data
setiap variabel pada C. Setiap varibel punya tipe data dan
namanya.
Variabel adalah unik, tidak boleh berupakeyword, dimulai dengan huruf atau
underline, maks 32 karakteri nt a, b;doubl e d;
/ * Thi s i sa bi t cr ypt i c */
i nt st ar t _t i me;i nt no_st udent s;
doubl e cour se_mar k;/ * Thi s i s a bi t bet t er */
-
8/9/2019 01. Struktur Data Pendahuluan
9/24
Pendeklarasian Variabel &
Konstanta
-
8/9/2019 01. Struktur Data Pendahuluan
10/24
The char type char disimpan dalam kode ascii (integer) Print char dengan %c
char menggunakansingle quote
i nt mai n( )
{char a, b;a= ' x' ; / * Set a t o t he char act er x */
pr i nt f ( "a i s %c\ n" , a) ;b= ' \ n' ; / * Thi s r eal l y i s one char act er */pr i nt f ( "b i s %c\ n" , b) ;r et ur n 0;
}
-
8/9/2019 01. Struktur Data Pendahuluan
11/24
A short note about ++
++i means increment i then use it
i++ means use i then increment iti nt i = 6;pr i nt f ( "%d\ n", i ++) ; / * Pr i nt s 6 set s i t o 7 * /
i nt i = 6;pr i nt f ( "%d\ n", ++i ) ; / * pr i nt s 7 and set s i t o 7 */
Note this important difference
All of the above also applies to - - .
-
8/9/2019 01. Struktur Data Pendahuluan
12/24
Casting
Memaksa suatu tipe data
Tipe data yang serupa float -> int
Int -> float Lihat contoh!
-
8/9/2019 01. Struktur Data Pendahuluan
13/24
Formatting Command Summary
Format Command Data type Description
%d Int Decimal number
%x Int Hexadecimal number
%b IntLow byte as binary
number
%c IntLow byte as ASCII
character
%f float Floating point number
%s char array Char array (string)
-
8/9/2019 01. Struktur Data Pendahuluan
14/24
Control Structure 1
SWITCH
switch ( key ) {
case a:
case A:
DoFirstThing();
DoSecondThing();break;
case b:
DoSomething();
break;default:
break;
};
IF / IF ELSE
if ( true ) {
DoFirstThing();
DoSecondThing();
};
if ( true )
DoSomething();else
DoSomethingElse();
-
8/9/2019 01. Struktur Data Pendahuluan
15/24
Control Structure 2
FOR
int i, j;for (i=0; i0; j--) {
// i counts up
// j counts down
printf(%i %j\n, i,j);
};
The ++ / - - is shortcutused to increment /decrement value of intvariables
WHILE
int i = 0;
int StayInLoop = 1;
while ( StayInLoop ) {
i+=2;
// Make sure you have// exit condition!
if ( i > 200 )
StayInLoop = 0;
};
+= increments by n
-
8/9/2019 01. Struktur Data Pendahuluan
16/24
What is a function?
The function is one of the most basic things tounderstand in C programming.
A function is a sub-unit of a program whichperforms a specific task.
We have already (without knowing it) seen
one function from the C library pr i nt f . We need to learn to write our own functions.
Functions take arguments (variables) andmay return an argument. Formal parameter
Actual parameter
-
8/9/2019 01. Struktur Data Pendahuluan
17/24
Type of function
Void : tidak mengembalikan nilai
Non-void : mengembalikan nilai
-
8/9/2019 01. Struktur Data Pendahuluan
18/24
Contoh function#i ncl ude i nt maxi mum ( i nt , i nt ) ; / * Pr ot ot ype see l at er i n l ect ur e */
i nt mai n( i nt ar gc, char *ar gv[ ] ){
i nt i = 4;i nt j = 5;i nt k;k= maxi mum ( i , j ) ; / * Cal l maxi mum f unct i on */pr i nt f ( "%d i s t he l ar gest f r om %d and %d\ n" , k, i , j ) ;
pr i nt f ( "%d i s t he l ar gest f r om %d and %d\ n", maxi mum( 3, 5) , 3, 5) ;r et ur n 0;}
i nt maxi mum ( i nt a, i nt b)
/ * Ret ur n t he l ar gest i nt eger */{i f ( a > b)
r et ur n a; / * Ret ur n means " I am t he r esul t of t he f unct i on"*/r et ur n b; / * exi t t he f unct i on wi t h t hi s r esul t */
}
Prototype the function
Call the function
The function itself
function header
-
8/9/2019 01. Struktur Data Pendahuluan
19/24
The main() Function
function main() dibutuhkan agar program Cdapat dieksekusi!
Tanpa function main, program C dapatdicompile tapi tidak dapat dieksekusi (harusdengan flag parameter c, jika di UNIX)
Pada saat program C dijalankan, maka compilerC pertama kali akan mencari function main() danmelaksanakan instruksi-instruksi yang ada di
sana.
-
8/9/2019 01. Struktur Data Pendahuluan
20/24
int main()
Berarti di dalam function main tersebut harus terdapatkeyword return di bagian akhir fungsi dan
mengembalikan nilai bertipe data int, Mengapa hasil return harus bertipe int juga? karena tipe
data yang mendahului fungsi main() diatas
dideklarasikan int Tujuan nilai kembalian berupa integer adalah untukmengetahui status eksekusi program. jika terminated successfully (EXIT_SUCCESS) maka, akan
dikembalikan status 0, sedangkan jika terminated unsuccessfully (EXIT_FAILURE)
akan dikembalikan nilai status tidak 0, biasanya bernilai 1
Biasanya dipakai di lingkungan UNIX
-
8/9/2019 01. Struktur Data Pendahuluan
21/24
Area pemakaian Variabel
Area pemakaian variabel (the scope of avariable) is where it can be used in a program
Normally variables are local in scope - thismeans they can only be used in the functionwhere they are declared (main is a function)
If we declare a variable outside a function itcan be used in any function beneath where itis declared declare global variables.
variabel global dapat digunakan oleh barisprogram yang ada dibawahya.
Global variables are A BAD THING
Contoh program mencetak
-
8/9/2019 01. Struktur Data Pendahuluan
22/24
Contoh program mencetak
karakter bintang (*)#i nc l ude voi d pr i nt _ s t ar s ( i nt ) ;
i nt mai n( ){ i nt i ; f or ( i = 0; i < 5; i ++)
pr i nt _ s t a r s ( 5) ; r et ur n 0;}
voi d pr i nt _ s t ar s ( i nt n)
{ i nt i ; f or ( i = 0; i < n; i ++) pr i nt f ( " * " ) ; pr i nt f ( " \ n" ) ;}
This program prints five rows of
five stars
This prints 'n' stars and then
a new line character
Loop around 5 times to
print the stars
**********
***************
Variables here are LOCAL variables
-
8/9/2019 01. Struktur Data Pendahuluan
23/24
Cara umum (sederhana) untuk melakukan
pengecekan kesalahan (debugging) Check missing brackets and commas.
Check that you have a semicolon at the end ofevery line which needs one.
Put in some pr i nt f if you know what your program is DOING you will
know what it is Doing wrong or Doing right. Try to explain to someone else what the
program is meant to do.
Take a break, get a cup of coffee and comeback to it fresh. Debugging is FRUSTRATING
-
8/9/2019 01. Struktur Data Pendahuluan
24/24
Sumber Referensi
James Roberge, Stefan Brandle, dan DavidWhittington, 2003, C++ Data Structures 2ndEdition, Jones and Bartlett Publishers, Inc.,Sudbury, Massachusetts.
Antonius Rachmat Chrismanto UKDWYogyakarta.
P. Insap Santosa, 1992, Struktur Data
Menggunakan Turbo Pascal 6.0, PenerbitAndi, Yogyakarta.
Berbagai sumber dari Internet.