Skip to main content

Tugas_AlgoProg_RicoKumala_2201829735

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.

  • Advantages of using Modules :
  1. Top-down design with sub goal, huge program divided into smaller modules
  2. Can be done by more than one developer/programmer
  3. Easier to debug, as logical flow is easy to follow and easier to pin point errors.
  4. Modification can be done without affecting overall codes
  5. 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 : 
-Library function

-User-defined function

  • Library function, is standard function provided by C compiler. Those function described in the header files (.h)
EXAMPLE : strcpy() in string.h

  • User-Defined Function is self defined functions
Identifier Scoping: 
  1. Local
  2. Global

  • Passing Parameter
  1. By-Value, sent to other module is the value
  2. By-Location/by reference, sent to other module is the address
  • Recursive Drawback
Although recursive code more concise it needs:

-More memory consumption as stack memory is needed

-Takes longer time, should traverse through all recursive call using stack

  • Recursive Best Practice
Generally, use recursive solution if:

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






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.


  • Nested Structure
Nested structure is a structure with one of its element is another structure. Structure as a

member should be declared in advance.


  • Array of Structure
Structure data type can only contain one record. Real world problem needs group of

record.


  • Typedef
typedef  is an alias(other name). Used for short naming especially for long identifier.


typedef commonly used in a structure.


  • Bit Field
A struct with each elemeny assign with certain number of bit.


  • Union Definition
Union is used for memory join. By using inion, a memory location can be assigned for

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.

  • Static Keyword
Keyword static can be used as variable type, or return value type of a function.

  • Registered Variable
Objective : increase run time speed.

Defining a variable store in a register (if possible -- register is limited). If rejected :

automatic variable.

  • External Variable
In a modular programming, a program is divided into modules. In C modules are

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.






File Processing





  • File And Streams
Streams is a sequence of character. All input and output data is stream. C sees file as a 

stream.


There are (3) standard streams activated : 
  1. Standard Input Stream
  2. Standard Output Stream
  3. 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
Buffer area is part of the memory used as a temporary space before data moved to file.





Sorting & Searching


Sorting needs to speeed up searching operation in a list. 

Sorting type :

- 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
-Compared two neighboring values.


-Compare and swap (if necessary)

-Also knows as exchange sort.


  • Selection Sort
  • Insertion Sort
  • Quick Sort

  • Merge Sort
Merge sort is sorting algorithm based on the divide -and-conquer algorithm.

Divide-and-conquer is a general algorithm design paradigm.
  1. Divide : Divide the input data in two disjoint subsets.
  2. Recur : Solve the sub problems associated with subsets.
  3. Conquer : combine the solutions for each subset into a solution.


  • Searching
It may be necessary to determine whether an array contains a value that matches a certain

 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.







2201829735
Rico.kumala@binus.ac.id
Skyconnectiva.com
Rico Kumala

Comments