Pemodelan untuk Komputasi : Repetition atau Pengulangan dan Contoh Soal ( Bahasa Inggris )

Pemodelan untuk Komputasi : Repetition atau Pengulangan dan Contoh Soal ( Bahasa Inggris )



Introduction


“Wash, rinse, repeat”

A phrase that originated on the back of many brands of shampoo. It is often used as a metaphor for people who robotically follow instructions without critically thinking about what they are doing. Anyone following these instructions will literally spend the rest of their lives endlessly repeating the same steps over and over; at least until they run out of shampoo

In everyday life we must often repeat a sequence of actions in order to achieve some greater outcome:

  • Painting a wall which we repeatedly dipping the brush into the paint can & applying the paint to the wall. 
  • Constructing a fence which we repeatedly digging a hole & inserting a fence post



Repetition


Just as many real-life activities require repetition, computers often must also execute a sequence of actions repeatedly in order to achieve some greater outcome. In fact, one of the great strengths of a computer is its ability to robotically and quickly repeat actions without complaint!


Loops


Loop is a control structure that repeatedly executes a sequence of actions. A while loop is a type of loop where a sequence of actions is repeated as long as some logical condition holds. 

Following Loops also has a flowchart structure which shown below :



When the while loop is first encountered, we must determine whether to perform some action or sequence of actions at least one (more) time. This decision is made by asking a yes/no question. If the question is answered affirmatively, then the actions are performed after which we again ask whether we must repeat the sequence at least one more time. 

The repeating loop is highlighted by the red arrow.



Using everyday English we might describe a repeated sequence of actions with a phrase similar to “while the steak has not yet reached a temperature of 135°F, cook for three minutes.” 

The logical condition is “the steak has not yet reached 135 °F” and the repeated action is “cook for three minutes.” A flowchart that models this process:





How to Express Loops


A while loop will be expressed using the notation shown below:




  • The CONDITION and the ACTIONS contain elements that make the loop meaningful. 
  • The CONDITION must be an expression that produces a logical value. 
  • The ACTIONS is a meaningful sequence of actions. 
  • The loop body is the sequence of actions of a loop 
  • The condition controls how many times the loop body actions will be executed.


Example

A $306 Japanese Pure Wagyu New York Sirloin Steak. How did they cooks a perfect steak to customers ? Credit to Buzzfeed



In this example, we assume that the steak is placed on the grill at a room temperature of 75° and that for every three minutes the steak remains on the grill the temperature increases by 13°. The first line binds the value 75 to the name steakTemp; a variable that models the current temperature of the steak. The loop is controlled by the logical condition “steakTemp < 135”. The repeated action is expressed as “steakTemp ← steakTemp + 13”. 

In other words, the loop repeatedly increases the steak temperature in increments of 13 degrees until the minimum desired temperature is attained.


The Computational State


The computational state of the steak grilling program.




Example 2


Computing The Time to Grill A Steak


Perhaps the chef is cooking a large meal that includes steak, baked potatoes, and steamed broccoli. The chef needs to know the time required for grilling the steak in order to ensure that all three dishes are completed at about the same time. To compute the length of time required, we can introduce a new variable that represents the number of minutes of grilling that have elapsed at the conclusion of every three-minute span of time.

This variable should obviously be initialized to zero prior to entering the loop since, at that point in the process, the steak has not yet been put on the grill. Each execution of the loop should increase the variable by three minutes since our model states that the temperature is checked in three minute intervals. 

Once the loop terminates, the value of the variable will then contain the length of time required to grill the steak. 


Algorithm to Compute The Time To Grill A Steak




  • The variable minOnGrill is initialized to zero in line 2 and incremented by three in line 5. 
  • Counting loop
    • One that contains a variable to keep track of the number of times the loop is actually executed. 
  • The variable minOnGrill effectively counts the number of times that the loop executes, although the count is maintained as a multiple of three. 
  • Prior to executing the loop body, the variable is initialized to zero, thus denoting that the loop has been executed zero times. 
  • After the loop body is executed for the first time, the minOnGrill variable holds the value 3, denoting that the loop has been executed 1 time (1 multiple of 3). 
  • After the loop is executed for the second time, the minOnGrill variable holds the value 6, denoting that the loop has been executed 2 times (2 multiples of 3).

Loops Formula

The previous algorithm exhibits a design pattern that all computer programmers are familiar with. This design pattern states that all well-written loops must have three well-defined elements:
  1. initialization
  2. condition
  3. progress


Initialization



Every variable that occurs in the loop must hold the correct value prior to entering the loop. The initialization of the loop in the example is performed on lines 1 and 2. It is stated that the steakTemp immediately prior to the grilling process is 75 degrees and that the steak has been on the grill for 0 minutes. 

Condition



The condition that determines when to repeat the loop must be precise. Since the chef requires the steak to reach 135 degrees, the condition, given on line 3, indicates that the process will continue as long as steakTemp < 135.

Progress



The actions that are repeatedly executed must in some way make progress that allows the loop to terminate. The loop will terminate only when the steakTemp is not less than 135. The loop makes progress toward termination by increasing the steakTemp by 13 with every repetition. This repeated action ensures that the loop will eventually terminate since the steakTemp must eventually exceed 135.

Infinite Loops


  • What will be happened if we change the conditions from "steakTemp<135“ to steakTemp ≠ 135? 
  • Will the loop terminate? 
  • The steak temperature will therefore increase without limit. 
  • The time required for grilling the steak will also grow without bound.


Definition of Infinite Loops

An Overcooked Steak. This happens when programmed timer steak has an infinite looping issue.


An infinite loop ia a loop that will never terminate because the loop never makes progress toward termination. In other words, when we consult the condition of the loop to determine whether the loop should repeat, the answer is always “yes … let’s do it again!”

An infinite loop is almost always a logical error since it violates the requirement that a program halt in a finite number of steps. Infinite loops are the source of many mistakes in real-world software.


Exercise 1


For each of the following flowcharts, list each action that the computer takes and indicate the computational state that follows from each action





Exercise 2

  1. Write a flowchart that describes how to print the values 0, 1, 2, 3, . . ., n. You must use a loop. 
  2. Write a flowchart that describes how to print the values n, n-1, . . ., 1, 0. You must use a loop. 
  3. Write the algorithm that describes how to print the values 0, 3, 6, 9, and 12. You must use a loop. 
  4. Write the algorithm to compute the sum of the number 1, 2, 3, 4, 5, …, n! 
  5. Write the algorithm to compute how many vowels in a word! 
  6. Write the algorithm to check if an integer is a prime number.


Post a Comment

Lebih baru Lebih lama