t1103 - struktur data - pakhartono.files.wordpress.com · • header file: file yang berekstensi.h...

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 pohon pencarian

Upload: vankhanh

Post on 03-Mar-2019

242 views

Category:

Documents


0 download

TRANSCRIPT

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)editpengurutanpohonpencarian

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 buku

“The C Programming Language”• Bahasa C (dan turunannya: C++, Visual C, C#) adalah

salah 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

Identifier & Tipe Data C• Identifier adalah nama (atau pengingat) dari tempat penyimpanan

data di dalam memori komputer. Secara umum dibedakan, Variabel : isi data bisa diubahKonstanta : isi data bersifat tetap

Beberapa istilah dalam bahasa C

– Source code: kode program– Compile (build): pengubahan source code ke dalam

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

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(){…..

}

Contoh program Hello World…

#include <stdio.h> /* My first C program which prints Hello World */ int main (int argc, char *argv[]) { printf ("Hello World!\n"); return 0; }

Preprocessor

Library command

main() means “start here”

Comments are good

Return 0 from main means our programfinished without errorsBrackets

define code blocks

Keywords of C• Flow control (6) – if, else, return, switch, case, default

• Loops (5) – for, do, while, break, continue

• Common types (5) – int, float, double, char, void

• Structures (2) – struct, typedef

• Sizing things (1) – sizeof• Rare but still useful types (7) – extern, signed, unsigned, long, short, static, const

• Evil keywords which we avoid (1) – goto

Variabel

• Kita harus mendeklarasikan tipe data setiap variabel pada C.

• Setiap varibel punya tipe data dannamanya.

• Variabel adalah unik, tidak boleh berupakeyword, dimulai dengan huruf atauunderline, maks 32 karakter

int a,b;double d;/* This isa bit cryptic */

int start_time;int no_students;double course_mark;/* This is a bit better */

Pendeklarasian Variabel & Konstanta

The char type• char disimpan dalam kode ascii (integer)• Print char dengan %c• char menggunakan single quote

int main(){

char a, b;a= 'x'; /* Set a to the character x */printf ("a is %c\n",a);b= '\n'; /* This really is one character*/printf ("b is %c\n",b);return 0;

}

A short note about ++

• ++i means increment i then use it• i++ means use i then increment itint i= 6;printf ("%d\n",i++); /* Prints 6 sets i to 7 */

int i= 6;printf ("%d\n",++i); /* prints 7 and sets i to 7 */

Note this important difference

All of the above also applies to --.

Casting

• Memaksa suatu tipe data• Tipe data yang serupa• float -> int• Int -> float• Lihat contoh!

Formatting Command Summary

Format Command Data type Description

%d Int Decimal number

%x Int Hexadecimal number

%b Int Low byte as binary number

%c Int Low byte as ASCII character

%f float Floating point number

%s char array Char array (string)

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();

elseDoSomethingElse();

Control Structure 2• FOR

int i, j;for (i=0; i<5; i++)

for (j=5; j>0; j--) {// i counts up// j counts downprintf(“%i %j\n”, i,

j);};

• The “++” / ”--” is shortcut used 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

What is a function?• The function is one of the most basic things to

understand in C programming.• A function is a sub-unit of a program which

performs a specific task.• We have already (without knowing it) seen

one function from the C library – printf.• We need to learn to write our own functions.• Functions take arguments (variables) and

may return an argument.– Formal parameter– Actual parameter

Type of function

• Void : tidak mengembalikan nilai• Non-void : mengembalikan nilai

Contoh function#include <stdio.h> int maximum (int, int); /* Prototype – see later in lecture */ int main(int argc, char*argv[]) { int i= 4; int j= 5; int k; k= maximum (i,j); /* Call maximum function */ printf ("%d is the largest from %d and %d\n",k,i,j); printf ("%d is the largest from %d and %d\n",maximum(3,5), 3, 5); return 0; } int maximum (int a, int b) /* Return the largest integer */ { if (a > b) return a; /* Return means "I am the result of the function"*/ return b; /* exit the function with this result */ }

Prototype the function

Call the function

The function itself

function header

The main() Function• function main() dibutuhkan agar program C

dapat dieksekusi! • Tanpa function main, program C dapat

dicompile tapi tidak dapat dieksekusi (harusdengan flag parameter –c, jika di UNIX)

• Pada saat program C dijalankan, maka compiler C pertama kali akan mencari function main() danmelaksanakan instruksi-instruksi yang ada disana.

int main()• Berarti di dalam function main tersebut harus terdapat

keyword return di bagian akhir fungsi danmengembalikan nilai bertipe data int,

• Mengapa hasil return harus bertipe int juga? karena tipedata yang mendahului fungsi main() diatasdideklarasikan 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

Area pemakaian Variabel• Area pemakaian variabel (the scope of a

variable) is where it can be used in a program• Normally variables are local in scope - this

means they can only be used in the function where they are declared (main is a function)

• If we declare a variable outside a function it can be used in any function beneath where it is declared declare global variables.variabel global dapat digunakan oleh barisprogram yang ada dibawahya.

• Global variables are A BAD THING

Contoh program mencetakkarakter bintang (*)

#include <stdio.h>void print_stars(int);

int main(){ int i; for (i= 0; i < 5; i++) print_stars(5); return 0;}

void print_stars (int n){ int i; for (i= 0; i < n; i++) printf ("*"); printf ("\n");}

This program prints five rows offive stars

This prints 'n' stars and thena new line character

Loop around 5 times to print the stars

*************************

Variables here are LOCAL variables

Cara umum (sederhana) untuk melakukanpengecekan kesalahan (debugging)

• Check missing brackets and commas.• Check that you have a semicolon at the end of

every line which needs one.• Put in some printf

– 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 come back to it fresh. – Debugging is FRUSTRATING

Sumber Referensi

– James Roberge, Stefan Brandle, dan David Whittington, 2003, C++ Data Structures 2nd Edition, Jones and Bartlett Publishers, Inc., Sudbury, Massachusetts.

– Antonius Rachmat Chrismanto – UKDW Yogyakarta.

– P. Insap Santosa, 1992, Struktur Data Menggunakan Turbo Pascal 6.0, Penerbit Andi, Yogyakarta.

– Berbagai sumber dari Internet.