Repitition
Arti repitition dalam bahasa Indonesia adalaha pengulangan. Tetapi dalam bahasa pemrogaman artinya adalah pengulangan instruksi dengan waktu yang diinginkan. Repitition dalam pemrograman ada 3 jenis yaitu:
- For
- While
- Do-while
For
Syntax untuk for adalah :
for(a; b; c) statement;
Atau:
for(a; b; c){
statement1;
statement2;
…….
}
b : inisialisasi
b : kondisi
c : inkremen atau dekremen
exp1, exp2 and exp3 are optional
Jenis-jenis looping:
1. Infinite Looping
Loop tanpa kondisi untuk berhenti bisa menggunakan "for-loop" dengan menghilangkan semua parlemen (a,b,c). Untuk mengakhiri loop gunakan break.
2. Nested loop
Maksud dari nested loop adalah loop di dalam loop. Operasi repitisinya akan memulai dari sebelah dalam.
While
Syntax :
while (exp) statements;
or:
while(exp){
statement1;
statement2;
…..
}
While (exp) statement;
Exp adalah ekspresi boolean. Yang akan menghadilkan true(tidak nol) atai false (nol).
Ekspresi akan di jalani selama exp tidak nol.
Do-while
Syntax :
do{
< statements >;
} while(exp);
Di operasi while, statment tidak akan pernah bisa di execute jika nilai exp salah.
Di do-while minimal akan terexecute sekali.
Break Vs Continue
Break berfungsi untuk mengakhiri loop (fot, while, do-while) dan mengakhiri operasi switch.
Continue berfungsi untuk melewati statement yang ada di bawahnya yang berada di dalam repitition dan lanjut ke loop selanjutnya dengan normal.
Pointers And Array
- Pointer
Pointer adalah suatu variabel yang menyimpan alamat suatu variabel lain. Ada dua operator yang biasa digunakan dalam pointer yaitu : * (isi dari) dan & (alamat dari).
Contoh :
Initialize an integer pointer into a data variable:
int i, *ptr;
ptr = &i;
To assign a new value to the variable pointed by the pointer:
*ptr = 5; /* means i=5 */
- Pointer to Pointer
Pointer to pointer adalah variabel yang menyimpan alamat sebuah pointer. syntaxnya (<type> **ptr_ptr;). Contoh :
int i, *ptr, **ptr_ptr;
ptr = &i;
ptr_ptr = &ptr;
To assign new value to i:
*ptr = 5; // means i=5 ;
**ptr_ptr = 9; // means i=9; or *ptr=9;
- Array
Array adalah suatu variabel yang bisa menyimpan banyak data yang dapat dibedakan dari urutan indexnya. Array memiliki dua karakteristik yaitu : Homogenous (Semua elemen memiliki tipe data yang mirip) dan Random Access (Setiap elemen dapat mencapai individualisme). Contoh : int A[10];
- Pointer Konstant & Pointer Variabel
-Pointer Variabel adalah pointer yang dapat ditugaskan dengan nilai yang baru saat dalam proses.
-Pointer Konstant adalah pointer yang tidak dapat ditugaskan dengan nilai yang baru saat dalam proses.
-Array adalah sebuah pointer kontant kepada elemen pertama dari array tersebut. Array bisa diisi dengan pointer variabel.
- Macam-maca Array
1. Dua dimensi Array (Syntax : type name_array[row][col];)
2. Tiga dimensi Array (Syntax : type name_array[row][col][depth];)
- String
String adalah array of character yang di akhiri dengan karakter null ('\0' atau dalam ASCII = 0).
- Macam - Macam Manipulasi String
•In Standard Library Function (header file string.h) provides functions to manipulate string:
–strlen()
Return a value of string length; excluded null char
–strcpy(s1,s2)
Copy s2 to s1
–strncpy(s1,s2,n)
Copy first n characters of s2 to s1
–strcat(s1,s2)
Adding string s2 to the end of string s1
–strncat(s1,s2,n)
Adding n characters of string s2 to the end of string s1
–strcmp(s1,s2)
Comparing the value of string s1 and s2, if similar returning 0
–etc.
Function And Recursion
Module in C programming language is implemented using function. Function is formed
through grouping some statements to do particular job. Module is needed when a certain
block of statement frequently used by other distinct code in program. Also called Sub-
Program.
through grouping some statements to do particular job. Module is needed when a certain
block of statement frequently used by other distinct code in program. Also called Sub-
Program.
- Advantages of using Modules :
- Top-down design with sub goal, huge program divided into smaller modules
- Can be done by more than one developer/programmer
- Easier to debug, as logical flow is easy to follow and easier to pin point errors.
- Modification can be done without affecting overall codes
- Easier to document
- Best practice in module Programming :
- High Fan-In, frequently used
- Low Fan-Out, more specific functionally / small number of job
- Self-Contained, self resource sufficient.
- Function in C divided into two types :
-User-defined function
- Library function, is standard function provided by C compiler. Those function described in the header files (.h)
- User-Defined Function is self defined functions
- Local
- Global
- Passing Parameter
- By-Value, sent to other module is the value
- By-Location/by reference, sent to other module is the address
- Recursive Drawback
-More memory consumption as stack memory is needed
-Takes longer time, should traverse through all recursive call using stack
- Recursive Best Practice
-Difficult to solve iteratively
-Efficiency using recursive has been reached
-Efficiency is less important in comparison with readability
-Memory efficiency and execution time are not the main concern
Condiser carefully speed and efficiency using iterative approach, rather than nice logical
design using recursive.
design using recursive.
Structures and Unions & Memory Allocation
Structure is a data type to store group of data with various of data type. Structure
componen called member/field/element. Heterogeneous (various elemnt data type).
Structure in other programming language also called record.
componen called member/field/element. Heterogeneous (various elemnt data type).
Structure in other programming language also called record.
- Nested Structure
member should be declared in advance.
- Array of Structure
record.
- Typedef
typedef commonly used in a structure.
- Bit Field
- Union Definition
two or more variable with differenet data types. Memory capacity used by union is the
largest capacity used by any element of the union.
- Enumeration
Enumeration is a data type with predefined number of data. This limited number of data
is named for program readability.
is named for program readability.
- Static Keyword
- Registered Variable
Defining a variable store in a register (if possible -- register is limited). If rejected :
automatic variable.
automatic variable.
- External Variable
implemented using function.
A module may consist of several functions. And save in a file.
If function in a particular file wants to access a variable in other file, then use
keyword extern.
keyword extern.
File Processing
- File And Streams
stream.
There are (3) standard streams activated :
- Standard Input Stream
- Standard Output Stream
- Standard Error Stream
File Definition :
- File is collection of record
-Record is a collection of field
- Field is a block of byte
- Byte is collection of bit
- Buffer Area
Sorting & Searching
Sorting needs to speeed up searching operation in a list.
- Ascending
-Descending
Sorting algorithm:
1. Internal sorting
All data to be sorted are loaded to RAM
2. External sorting
Sorting process using secondary storage.
- Bubble Sort
-Compare and swap (if necessary)
-Also knows as exchange sort.
- Selection Sort
- Insertion Sort
- Quick Sort
- Merge Sort
Divide-and-conquer is a general algorithm design paradigm.
- Divide : Divide the input data in two disjoint subsets.
- Recur : Solve the sub problems associated with subsets.
- Conquer : combine the solutions for each subset into a solution.
- Searching
key value. The process of finding a particular element of an array is called searching.
Searching is an action to retrieve information based on particular key from some saved
information.
Key is used to do record seaching which is desired from a set of data list. Key must be
unque, means there must not be any same key in the data.
1. Linear Search
Linear searcch compares each element of the array with the search key.
2. Binary Search
The Linear searching method works well for small or unsorted arrays. However, for large
arrays linear searching is inefficient. If the array sorted, the high-speed binary search
technique can be used.
3. Interpolation Search
Interpolation Search technique is performed on the sorted data. This searching process is
almost similar with binary search technique. Searching technique is done with the
approximate location of the data.
Rico.kumala@binus.ac.id
Skyconnectiva.com
Rico Kumala
Comments
Post a Comment