Pemodelan untuk Komputasi : Modularization dan Contoh Soal ( Bahasa Inggris )

Pemodelan untuk Komputasi : Modularization dan Contoh Soal ( Bahasa Inggris )


Introduction


Modularization is a vital element of programming. It allows us to define new computable actions by assigning a name to some process. Algorithms can be modularized by breaking them into independent subprocesses.

Example: to calculate the volume of a cone, there are 2 subprocesses:
  • Calculate the base area 
  • Multiply: the base area with the height of cone and 1/3

Example: The Volume of A Cone



Module



A module is a named subprocess. Modules are vital when writing large programs that consist of many individual parts. Syntactic pattern for naming a subprocess as a module: 


Parameters are the input of the subprocess/algorithm.

Example: The Volume of A Cone





Invoking A Module

To invoke a module, mention the name along with the parameters. So, the statement baseArea(10) will calculate the area of circle with radius = 10. The result is 314. Example :


Example: Vowel

To count how many vowel in a word, there are 2 subprocesses:
  1. A: Check if a letter is vowel
  2. B: Do subprocess A for all the letters in the word

The statement ltr('a') will check if a is a vowel. The statement ltr('v') will check if v is a vowel


Example: Fraction 1

Write a module to compute the addition of two fractions.



Example: Fraction 2

Write a module to compute the addition of two fractions.


Exercises


  1. Write a module to transform a fraction to its simplest form !
  2. Write a module to transform a point. The transformation is including:
    • Translation 
    • Dilation 
    • Reflection from the origin
  3. Write a module to find the closest pair of points ! 
  4. Write a module to swap the values of two variables ! 

Post a Comment

Lebih baru Lebih lama