Recursion


Recursion adalah sebuah fungsi yang dimana fungsi dipanggil di dalam fungsi yang sama.

Berikut beberapa contoh kasus yang biasa digunakan untuk mengilustrasikan rekursi, antara lain

Berikut contoh penggunaan rekursi pada C++:

  • Faktorial
  • Exponensial
  • Tower of Hanoi
  • Tree Traversal
  • Depth First Search

Semoga bermanfaat!

Chapter 5- Problem-Solving with Decisions


Decision Logic Structure

It is used by a programmer to decide whether the set of instructions is to be executed based on the condition whether it is TRUE or FALSE.

Algorithm of Decision Logic

The decision logic structure uses the If/Then/Else instruction. It tells the computer that If a condition is true, Then execute a set of instructions, or Else execute another set of instructions. The Else part is optional, as there is not always a set of instructions if the conditions are false. When there are no instructions for true, a Continue statement must be used.

Flowchart Diagram of Decision Logic Structure


There are to TWO (2) types of condition, as follow:

  1. Single condition: It has only two possible actions or set of instructions
  2. Multiple conditions

Multiple Conditions

It has THREE (3) kinds of multiple conditions such as straight-through logic, positive logic, and negative logic.

Straight-through logic means that all of the decisions are processed sequentially, one after the other.straight-through logic is required when all the decisions have to be processed, and when they are independent of each other.

Problem :   to find the amount to charge people of varying ages for a concert ticket. When the person is under 16, the charge is $7; when the person is 65 or over, the charge is $5; all others are charged $10.  

Positive logic allows the flow of the processing to continue through the module instead of processing succeeding decisions. In general, when you use positive logic, you are telling the computer to follow a set of instructions and continue processing if the condition is True; If the condition is not True, then the computer processes another decision. When you use this logic, no more decisions are processed once the resultant of a condition is True.

Problem: To find the amount to charge people of varying ages for a concert ticket. When the person is under 16, the charge is $7; when the person is 65 or over, the charge is $5; all others are charged $10.  The conditions of that problem are the following:

Negative logic is similar to positive logic except that the flow of the processing continues through the module when the resultant of decision is FALSE. In general, when you use negative logic you are telling the computer to process another decision when the resultant of the condition is True; If the resultant is False, then the computer processes a consequent set of instructions and continues processing the module.  he hardest to use and understand

Problem: To find the amount to charge people of varying ages for a concert ticket. When the person is under 16, the charge is $7; when the person is 65 or over, the charge is $5; all others are charged $10.

Exercise

Develop the flowchart in Positive Logic and Negative Logic to determine whether a temperature is below or above the freezing point. The algorithm as follow:

  1. Start
  2. Read temperature
  3. If the temperature is less than 32. then print “below freezing point”, otherwise print “above freezing point”
  4. End

References:

  • Sprankle, Maureen (2001), Problem Solving and Programming Concepts, 5th edition, Prentice-Hall.
  • Jeri R. Hanly, Elliot B. Koffman (2003), Problem Solving and Program Design in C, 4th edition Addison Wesley
  • H.M. Deital, P.J. Deital (2001). C: How to Program. 3rd edition, Prentice-Hall.
  • Alice Ficher, Stephen M. Rass(2001), Applied C (An Introduction and Move) Mc Graw Hill, New Haven

Additional Material Problem Solving Techniques & Algorithms


Hi dear students,,

Actually, we will always find problems in our daily life, so we’ve to solve the problems until we get the solutions. In this lecture, you’ll know how the steps used to solve the problems.  Especially, according to computer literature, we use computer as a tool to solve the problems. As Programmer you should know the way to develop a program that used the right order of steps in developement of program.

 

Here’s I share you the ebook, that we’ll use along the lecture. Please Click here.

Lect 1 – Chap 1 – General Problem-Solving Concepts


Problem Solving in Everyday Life

People make decisions every day to solve problems that affect their lives. The problems may be as unimportant as what to watch on television or as important as choosing a new profession. If a bad decision is made, time and resources are wasted, so it’s important that people know how to make decisions well. There are six steps to follow to ensure the best decision. These six steps in problem solving include the following:

  1. Identify the problem
  2. Understand the problem
  3. Identify the alrternative ways to solve the problem
  4. Select the best way to solve the problem from the list of alternative solution
  5. List instructions that enable you to solve the problem using the selected solution
  6. Evaluate the Solution

The Example of Problem

Take the problem of what to do this evening

  1. Identify the problem. How do the individuals wish to spend the evening
  2. Understand the problem. With this simple problem, also, the knowledge base of the participants must be considered. The only solutions that should be selected are ones that everyone involved would know how to do. You probably would not select as a possible solution playing a game of chess if the participants did not know how to play.
  3. Identify alternatives. Note : The list is complete only when you can think of no more alternatives.
    1. Watch television.

    2. Invite friends over.

    3. Play video games.

    4. Go to the movies.

    5. Play miniature golf.

    6. Go to the amusement park.
    7. Go to a friend’s party.
  4. Select the best way to solve the problem.
    1. Weed out alternatives that are not acceptable, such as those that cost too much money or do not interest one of the individuals involved.
    2. Specify the pros and cons of each remaining alternative. 
    3. Weigh the pros and cons to make the final decision. This solution will be the best alternative if all the other steps were completed well.
  5. Prepare a list of steps (instructions) that will result in a fun evening.
  6. Evaluate the solution. Are we having fun yet? If nobody is having fun, then the planner needs to review the steps to have a fun evening to see whether anything can be changed, if not then the process must start again.

Continue reading