Dasar-Dasar Pemrograman : Percabangan II ( Branching Lanjut ) dan Contoh Soal

Dasar-Dasar Pemrograman : Percabangan II ( Branching Lanjut ) dan Contoh Soal



Introduction



The Richter scale is a measurement of the strength of an earthquake. Every step in the scale, for example from 6.0 to 7.0, signifies a tenfold increase in the strength of the quake. We can categorize the scale into 5 categories: Category A (most structures fall), B (many buildings destroyed), C (many buildings considerably damaged, some collapse), D (damage to poorly constructed buildings), and E (no destruction of buildings). Write the program to categorize the Ritcher scale !



Multiway if-else


Is it enough to use only if-else structure to solve the previous problem? We need more than one conditions to categorize the Ritcher scale. 

Solution: use multiway if-else or nested if

Syntax for multiway if-else


Flowchart for multiway if-else



Ritcher Scale Solution


Exercise



Wombat wants to know the form of water based on its temperature. We know that the form of water is solid when its temperature is less than or equals 0 C. Its form is liquid when the temperature is more than 0 C but it is less than 100 C. The form of water is gas when its temperature is more than 100 C. Make a program to help Wombat determine the form of water! Your program will accept an integer denotes the temperature of water. The output is one of these strings: "solid", "liquid“, "gas“.

Solution


switch Statement

Switch is another control structure to implements multiway branches. Use switch expression (integer expression) to determine which branch to be executed. Use break statement to jump to the next statement after the switch statement.

Syntax



Example Using switch Statement



Nested if

It is often necessary to include an if statement inside another. Such an arrangement is called a nested set of statements. Put an if statements within another if statement : nested control structure

In general, any problem that involves a multiway branch can be solved (coded) using Nested-if (including multiway if-else and switch)

Example : Tax regulation in US




Syntax



Flowchart



Solution for Tax Regulation Problem



Dangling Else

When using Nested-if structure, it is very important to use braces. In the absence of braces, an else is always paired with the closest preceding if that does not already have an else paired with it.


Example


Dangling Else Correction


Exercises


Salary


You are given the salaries of these 3 employees working in the accounts department. You have to find out the minimum salary of those employees. Write the algorithms and draw the flowchart!


The Colors




In RYB colors, there are three primary colors: red, blue, and yellow. We can make a secondary color by mixing up two primary colors. We can create green by mixing up the blue and the yellow. The orange is created by mixing up red and yellow. The purple is created by mixing up red and blue. 

Given two primary colors, make an algorithm to find out what is the secondary color created by those primary colors!


Leap Year


Write an algorithm and draw the flowchart to determine if a year is a leap year. A leap year is a calendar day containing one additional day. These extra days occur in years which are multiples of four (with the exception of years divisible by 100 but not by 400).


Zapping


My TV remote is almost broken. Only △ and ▽ are working. My TV has 100 channels conveniently numbered 0 through 99. They are cyclic, in the sense that if I’m on channel 99 and press △ I’ll go to channel 0. Similarly, if I’m on channel 0 and press ▽ I’ll change to channel 99. I would like a program that, given the channel I’m currently watching and the channel I would like to change to, tells me the minimum number of button presses I need to reach that channel.
Write the algorithm and draw the flowchart!


How Old Are You



Given two dates: current date and a birth date of a person. Write your algorithm and draw the flowchart to calculate how old a person is! Assume that there are no invalid date (current date is < birth date). 

Example: 
  • Current Date: 12/11/2007 
  • Birth Date: 01/01/1984 
  • Age: 23


Chocolate Bar

We want to make a package of goal kilos of chocolate. We have small bars (1 kilo each) and big bars (5 kilos each). Count the number of small bars to use, assuming we always use big bars before small bars. Output -1 if it can't be done. 

Example:



Sumber


Slide Daspro : Branching II

1 Komentar

Posting Komentar

Lebih baru Lebih lama