Review Sebelum QUIZ & UAS

Review Sebelum Quiz & UAS

Include <stdio.h>

Include <stdlib.h>

Include <time.h>

Include <string.h>

Struct Baju{

Char kode baju [7];

Char nama baju [100];

Int qty;

Int harga satuan;

};

Int

Char dipakai untuk name

Int dipakai untuk hitungan

1 array memiliki 1 baris

Array dibuat karena sudah awalnya sudah dibentuk struct

R = Read

W = Write

Fopen = nulis nambahan di depan

“ % [^;];% [^;];% [^;];% [^;];% [^;];

Cara bacanya cari sebuah data yang diakhiri dengan titik koma;

Atoi = ASCII int

Kalau mau lebih cepat menggunakan Sprintf

Printf = printf format

Printf (“%-4S%-12S%-20%”) maksudnya ada beberapa yang terdapat 4 karakter

Jika tidak ada fflush dapat menyebabkan looping forever

Switch Case selalu diakhiri dengan break

Strcmp = String Conpare

Pow (math.h)

Itoa <stdlib.h>

Switch tidak selalu diakhiri break bisa juga diakhiri continue;

 

 

 

 

 

Sorting & Searching

Sorting & Searching

Sorting adalah mengurutkan array yang berisi angka

-) Mengurangi kompleks

Bubble Sort

-) For di dalam For

-) While

-) Tergolong di solting yang Kompleks

Selection Sort

Selection sort adalah memastikan data kecil dan data besar

-) Menyimpan indeks

-) Yang di cek nilai indeks, bukan nilai indeks yang kecil, tapi tetap ada pada tempatnya sendiri

-) Kompleksitas n2

Insertion Sort

-) Disisipkan

Misal dibandingkan dengan data 1

-) Tidak memastikan buatannya terkecil

-) Bandingkan indeks yang dipilih dengan indeks yang lain.

-) Kompleksitas n2

Quick Sort

-) Menggunakan teknik recursive

-) Memanggil dirinya sendiri

Merge Sort

-) Data dipecah menjadi dua – dua

-) Terdapat kelipatan 2,4,8

-) Lebih tepatnya 2n

Terdiri dari 2 array misal : Array X

Array Y

Yang dipecah menjadi 4

Kemudian array x yang lebih kecil masuk ke array

Teknik Merge Sort:

-) Divide

-) Recursive

-) Conquer

Searching

Untuk mempermudah serching

Key = data yang kita cari

Teknik Searching:

-) Linear search {data kecil}

Algorithm:

  1. n : total record of array x.
  2. For each x[i], 0 £ i £ n-1, check whether x[i] = key.
  3. If x[i] = key, then the searched data is found in index=i. Finished.
  4. If x[i] ¹ key, then continue searching until the last data which is i = n-1.
  5. If i= n-1 and x[i] ¹ key, it means the data is not exist in the list, and set index = -1. Finished.

-) Binary search {data besar}

Algorithm:

  1. n : total record of array x.
  2. left=0, right= n-1.
  3. mid =(int) (left + right)/2.
  4. If x[mid]=key then index = mid. Finished.
  5. If x[mid]<key then left = mid+1.
  6. If x[mid]>key then right = mid-1.
  7. If left £ right and x[mid] ¹ key, then repeat point 3.
  8. If x[mid] ¹ key then index = -1. Finished.

-) Interpolation search {data besar}

 

  1. In the interpolation search, we’ll split the data according to the following formula:Mid =     kunci ― data[min]           *(max   ―   min )  + min

     

    Data[max]  ― data[min]

  2. .If data[mid] = sought data, data has been found, searching is stopped and return mid.
  3. .If data[mid]!= sought data, repeat point **

**Searching is continued while sought data > data[min] and sought data < data[max].

File Processing

File Processing

File Definition

File is a collection of record (Kumpulan baris)

Record is a collection of field

Field is a block of byte

Byte is collection of bit

Byte terdiri dari 8 bit

3 macam stream:

-) Standard input stream (stdin) : Controlling input stream from keyboard

-) Standard ouput stream (stdout) : Controlling output stream to the monitor

-) Standard error stream (stderr) : Controlling the error messaging

Text file saved in a text format or ASCII

Storage size data: 10.000 needs 5 byte

Orc:> TYPE = file name

Binary File

hanya bisa dibuka data sendiri

Buffer Area

Syntax:

FILE*FP;

Open File

Opening a File using fopen():

FILE *fopen (const char *filename, const char *mode );

fopen() defined at <stdio.h>

“r” (opening a file to be read)

“w” (creating file to be written)

“a” (opening a file for data append)

“r” (opening a file for read or write)

w+ (creating file for read/write)

a+” (opening a File for read/append)

“rb” (opening a File (binary) to be read)

“wb” (creating a file (binary) for write operation)

 

Close File

Closing a file using fclose( ):

Int fclose (File*stream) :

f close. ( ) definied at <stdio.h> (menutup file)

 

f close (m ) will return o if succesful and EOF if error

EOF (End Of File) equals to -1

f close ( ) will release the buffer area and send the remaining data file

Closing a File using fcloseall():

int fcloseall (void);

Close all active stream except: stdin, stdout, stdprn, stderr, and stdaux.

Will return the number of stream closed if successful, and return EOF instead.

Header file <stdio.h>

 

input and ouput file

f gets (Input)

f puts(Output)

f scanf (Input)

f printf (output)

f write

f read

feof

  • Example using fwrite():

fwrite( &mhs, sizeof( mhs ), 1, fp );

  • &mhs = data origin location
  • sizeof(mhs) = return the size of mhs
  • 1 => one time write sizeof(mhs)
  • fp = file pointer

 

Function And Recursion

Function and Recursion

-) Modular Progamming

Program teerdapat di dalam modul

Modul dijadikan sebagai fungsi

Function disebut juga modal

Tujuan Parameter untuk berkomunikasi dengan fungsi lain

Library US user dibagi 2:

-) Libary function

Used definied function

Library function

Example: clrsrc ( ) in conio.h

Sqrt ( ) in math.h

Function definition

Function Construction

Return value type function name ( parameter list )

{

Statements:

}

Contoh parameter

Int maximum ( int x, int y) {

Int max = x;                  ( int harus di atas fungsi main )

If ( y > max ) max; y;

Return max

}

Fungsi prototype

Syntax : return value type function name

( Parameter List );

Int          maximum ( int a; int b; )

Identifier Scoping

Variabel global : bisa pakai semua fungsi

Variabel lokal   : Cuma fungsi di main

 

Recursive Function (2)

-) Base case : return value with out calling next recursive call

-) Reduction step : sequence of input value converging

Review Sebelum Quiz

REVIEW SEBELUM QUIZ

3 langkah sistem pemograman :

-) Input

-) Process

-) Output

Input adalah tahap proses suatu data yang dimasukkan

Process adalah tahap pemrosesan suatu data

Output adalah hasil dari suatu data yang di input & process

Yang termasuk dalam input:

-) If

-) Else

-) Else if

-) Nested if

-) Switch Case

-) While

-) Do While

-) For

Yang termasuk dalam process:

-) Selection

-) Looping

-) Storage

Coding Segitiga Bolong

#include <stdio.h>

 

int main(){

int i, j, k, baris=20;

baris /= 2;

for(i=0; i<baris; i++){

for(j=-(baris-1); j<baris; j++){

if(j<0){

k = j*(-1);

}

else{

k = j;

}

if(i==k||i==(baris-1)){

printf(“*”);

}

else{

printf(” “);

}

}

printf(“\n”);

}

for(i=(baris-2); i>=0; i–){

for(j=-(baris-1); j<baris; j++){

if(j<0){

k = j*(-1);

}

else{

k = j;

}

if(i==k){

printf(“*”);

}

else{

printf(” “);

}

}

printf(“\n”);

}

return 0;

}

 

Pointer and Arrays

Pointer & Arrays

Pointer dimana dia menyimpan alamat dari variabel lain.

* = content of

&= addres of

Syntax

<type> * ptr_name:

Ex:

Int i, *ptr; (content)

Ptr = & i; * (menyimpan nilai variable yang lain)

 

*Ptr

Val            Addr

41 108

 

i

5 104

 

Tujuan fflush     : untuk membersihkan inputan yang tidak diperlukan

Single pointer   : hanya dapat menyimpan alamat dari variable

Double pointer : menyimpan alamat dari single

Syntax

< type > ** ptr_ptr:

Ex:

Inti, *ptr, **ptr_ptr:

Ptr= &i;

Ptr_ptr = &ptr;

To assign new value = i

*ptr = 5,          // i = 5
**ptr_ptr = 9;  // i  = 9

 

Prinsip pointer : berapa pun pointernya maka akan tetap berkurang satu per satu

**ptr_ptr

*ptr

*x

*Ptr = 15

Ptr_ptr = &,ptr;

X = 10;

**ptr_ptr = 20

 

Array Characteristics

  • Homogenous

Semua elemen memiliki data yang sama

  • Random Acces

Setiap elemen dapat dicapai individual secara berurutan

 

0 – 99

Int X [100]

0 – (n – 1) X [99] = 100

[] = indeks elemen

 

Array initialization

Ex :

Int B [ ] = {1,2,-4,8};

1 2 -4 8 0 0 0 0 0

B(0)          B(1)            B(2)          B(2)       B(3)           B(4)           B(5)           B(6)                    B(7)

Accesering Arrays dengan menggunakan  pointer

  • (A +2) or A [2] or A [2]

 

 

 

 

 

Pointer Variable

Pointer = assigned with value new run time

Pointer Constant = can’t assigned with value new run time

Array selalu diawali dengan nilai 0

Kerugian array = meemakan banyak memory

Int a[4][3]; [  ]        [  ]

Row    column

[ 0 ]     [ 1 ]  = 10

[ 0 ]     [ 1 ]  = 20

Int B (8) = {0,0,0,5};

B(5) = 15;

Int X;

Int * arr . ptr [ 5 ];

Arr_ptr [ 0 ]=&x;

 

Arr_ptr [ 4 ]

Character (double code)

String (single mode)

Setiap karakter array diakhiri dengan dengan (`\’)

Repetition C

Repetition C : Pengulangan, looping

Looping operation (repetition)

-) for : kondisinya bisa dikosongin

-) While : kondisinya harus ada isi dan sebelum buat statement kondisi harus di cek dulu

-) Do.while : membuat kondisi dahulu baru di cek [ (isi dulu) baru jalanin ]

Repetition : for

For (exp1; exp2; exp3;) statement;

Or

For ( exp1; exp2; exp3;){

Statement 1;

Statement 2;

………………

}

 

Exp1; initialization

Exp2; conditional

Exp3; increament or decrement

Mau 3”nya dihilangkan / tidak dihilangin tetap looping forever

Break: untuk memberhentikan looping

Continue : skip

 

While

Akan berjalan jika kondisinya TRUE

Tidak akan berjalan jika kondisinya FALSE

Kelebihan : bisa jalan dengan syarat ≠ 0 jadi selalu benar jika kondisinya benar

Do While

Terus berjalan selama exp True, statement berjalan dahulu baru mengecek kondisi

Selection

Selection

Syntax :

-) if

-) if-else

-) Switch Case

Syntax if (boolean Expression) statement

Or

It (boolean expression) {

Statement 1

Statement 2

}

Kata Cuma 1 statement boleh ga pakai {}

If else

Syntax:

If (boolean expression) statement

Else statement 2;

Or

If (boolean expression) {

Statement 1

Statement 2

}

Else {

Statement 3

Statement 4

}

 

 

 

 

 

Nested – if

Bersifat harus melalui seleksi

If ( boolean expression ) statement;

Else

If ( boolean expression statement 2;

Else

If ( boolean expression statement 3;

Switch – case

Tidak harus bersifat True or false bisa juga berupa data, nilai {sifat expression}

Syntax

Switch (expression) {

Case contant 1 : statement 1 : break :

.

.

Case contant 2 : statement 2 ; break ;                                                                                                                                                     default : statement:

.

}

Wajib di break supaya tidak turun ke bawah

If terdapat 2 nilai : True

False

While              T

% d int         10        0-9

% 0 octal       8        0-7

%x%x hexa 16        0-9

 

 

 

 

 

 

 

 

Operator Introduction

Operator Introduction

Operator

Operand

C = A+B → Binary ada + =

  • % (modulus)

*

/

 

Operation number 3 :

Unary Operator → 1 operand

Binary Operator → 2 operand

Temary Operand → 3 operand

Operation type 6 group :

  • Assignment → nilai variabel X = 3 (operand 1 = operand 2)
  • Logical
  • Arithmetic → + − * / % ++ −− ()
  • Relational
  • Bitwise
  • Pointer

 

N ++ : // post increament

+ NN : // pre increament

N −−  : // post decreament

−− N : // pre decrement

 

Expression                            combined operator

A = a + b;                               a + = b;

A = a – b;                               a – = b;

 

Relational

= =   → Equality

! =    → Not Equal

<      → Less than

>      → Greater than

< =   → Less or equal then

> =   → Greath or equal then

? =   → Conditional statement

1 = T   X = ( 20 > 10 ) ; (1)

0 = f    X = ( 20 > 10 ) ; (1)

 

Conditional expressions

If ( a > b ) Z = a;

Else Z = b ;

Z = ( a > b ) ? a : b ;

A = 5                 Z = 3                           benar               Z = a

B = 3                                                     salah         Z≠ b

A = 3                Z =7

B = 5

 

Logical Operator

Symbol Functionally
&& AND
|| OR
| NOT

 

P Q Xor → a|b & ! (a & b)
T T F
T F T
F T T
F F F

 

Logical operators

Int X = 5 , int Y = 0 ;

X && Y;                        // FALSE               0

(X > Y ) && ( Y >= 0 )   // TRUE ≠ 0

 

Bitwise

Symbol Meaning Example
& AND A & B
| OR A|B;
˄ XOR A˄B;
˜ Complement ˜A;
>> Shiftt right A>>3;
<< Shift left B<<2,

 

EX:

10 : itu berapa bit

Cari 2n yang lebih kecil dari 10
n = 3

23      22     21    20

_      _     _     _

 

15 itu berapa bit

23  :  ˄ = 3

1       1     1     1

__   __   __   __

15 – 8 = 7 = 23 = 3 – 21  = 1

 

37 = 25

1      0      0      1      0    1

__    __   __   __   __   __                                                  37 – 25  = 5 – 22 = 1- 20 = 0

25   24   222 220                 

I I = ( 100 – 1 )

I I I = ( 1000 – 1 )

I I I I I I = 63

( 100000 ) – 1

64 -1 = 63

 

Bit wise = yang dioperasikan Bitnya

4 = 100                4 = 100

3 = 011               5 = 011

0 = 000               1 = 111

int A,B = 78

A = B >> 3 ; // Value A = 9

A = B << 2 ; // Value A = 312

78, binary : 0100 1110

First shift      : 0010 0111

Second shift : 0001 0011

Third shift     : 0000 1001               9 decimal

Operator Introduction

Operator

Operand

C = A+B → Binary ada + =

  • % (modulus)

*

/

 

Operation number 3 :

Unary Operator → 1 operand

Binary Operator → 2 operand

Temary Operand → 3 operand

Operation type 6 group :

  • Assignment → nilai variabel X = 3 (operand 1 = operand 2)
  • Logical
  • Arithmetic → + − * / % ++ −− ()
  • Relational
  • Bitwise
  • Pointer

 

N ++ : // post increament

+ NN : // pre increament

N −−  : // post decreament

−− N : // pre decrement

 

Expression                            combined operator

A = a + b;                               a + = b;

A = a – b;                               a – = b;

 

Relational

= =   → Equality

! =    → Not Equal

<      → Less than

>      → Greater than

< =   → Less or equal then

> =   → Greath or equal then

? =   → Conditional statement

1 = T   X = ( 20 > 10 ) ; (1)

0 = f    X = ( 20 > 10 ) ; (1)

 

Conditional expressions

If ( a > b ) Z = a;

Else Z = b ;

Z = ( a > b ) ? a : b ;

A = 5                 Z = 3                           benar               Z = a

B = 3                                                     salah         Z≠ b

A = 3                Z =7

B = 5

 

Logical Operator

Symbol Functionally
&& AND
|| OR
| NOT

 

P Q Xor → a|b & ! (a & b)
T T F
T F T
F T T
F F F

 

Logical operators

Int X = 5 , int Y = 0 ;

X && Y;                        // FALSE               0

(X > Y ) && ( Y >= 0 )   // TRUE ≠ 0

 

Bitwise

Symbol Meaning Example
& AND A & B
| OR A|B;
˄ XOR A˄B;
˜ Complement ˜A;
>> Shiftt right A>>3;
<< Shift left B<<2,

 

EX:

10 : itu berapa bit

Cari 2n yang lebih kecil dari 10
n = 3

23      22     21    20

_      _     _     _

 

15 itu berapa bit

23  :  ˄ = 3

1       1     1     1

__   __   __   __

15 – 8 = 7 = 23 = 3 – 21  = 1

 

37 = 25

1      0      0      1      0    1

__    __   __   __   __   __                                                  37 – 25  = 5 – 22 = 1- 20 = 0

25   24   222 220                 

I I = ( 100 – 1 )

I I I = ( 1000 – 1 )

I I I I I I = 63

( 100000 ) – 1

64 -1 = 63

 

Bit wise = yang dioperasikan Bitnya

4 = 100                4 = 100

3 = 011               5 = 011

0 = 000               1 = 111

int A,B = 78

A = B >> 3 ; // Value A = 9

A = B << 2 ; // Value A = 312

78, binary : 0100 1110

First shift      : 0010 0111

Second shift : 0001 0011

Third shift     : 0000 1001               9 decimal

Ringkasan Algoritma dan Pemrograman

JAVA

OOP merupakan kepanjangan dari Object Oriented Progamming

End capsulation: teknik privatisasi bersifat private bisa juga untuk public

Abstraction: bersifat wajib (contoh: wajib meneruskan keturunan dari kedua orang tuanya)

Interface: berssifat bisa lebih dari 1 (boleh pinjam)

Polymerism: menggunakan method dengan bijak efesien

Inherter: bersifat tidak wajib (contoh: Bapak dengan anak, tidak wajib, sifat bapak pada anak tidak harus ditunjukkan dan anak bisa melakukan sifatnya sesuai keturunan bisa juga melakukan perubahan.

NO SQL adalah membaca dan mengenali storage

ALGORITMA

  • Pseudo Code = tulisan
  • Flow Chart = aliran berupa data/gambar (Start End)

Source Code = bahasa pemograman

Executable Code = bahasa machine

Compile

Syntax Error

Testing

Outputer = problem defination

Documention

Pseudo code terbagi 3 :

  • Input
  • Process (compile, store, compare, loop)
  • Output

Structure Theorem

Procuderal = Structure

Striucture terbagi 3:

  • Sequence = urutan
  • Selection = if else
  • Repetition/loop(downwhile)

C terbagi atas 4 :

  • Flexibility
  • Portability
  • Well – known
  • Supported with large libraries

C structure

  • Main = utama
  • Case sensitive
  • Every statement should be with ; semi colon

4 jenis 

  • Comment : (|* */) & (//)
  • Keyword
  • Data
  • Variable