CSC171 - Problem Solving Techniques

This document describes four different techniques (natural-language, pseudo-code, flowchart, Nassi-Schneiderman) for developing an algorithm. Two of these techniques -- pseudo-code and flowcharts -- are widely used today. However, the Nassi-Schneiderman Notation is perhaps the easiest to convert to a procedural programming language (e.g., C++). Included within each technique description is a list of advantages and disadvantages, and two examples to illustrate the technique.

Natural-language Algorithm

This technique results in a series of numbered steps that are executed in sequence. Each step is written using your natural language (in my case, English), using the sentence structure inherent with this language.

First Example

Problem Statement: Obtain a number from the user, then display twice this number back to the user.

1. Ask the user for a number.
2. Get the number from the user.
3. Multiply the number by itself.
4. Display the result from step 3 to the user.

• Easy to learn, since you're using your natural language.

• Wordy - algorithm tends to be much longer than necessary.
• May be difficult to translate into a programming language, since the programming language contains very precise statement syntax and semantics.

Second Example

Refer to the comparison of the four techniques for a second example.

Navigation: Go to top, natural-language, pseudo-code, flowchart, Nassi-Schneiderman.

Pseudo-code Algorithm

This technique results in a series of numbered steps that are executed in sequence. Each step is written using a subset of your natural language (in my case, English). This subset is based on a common set of verbs found in procedural programming languages and the common mathematical operators (+, -, *, /, =).

First Example

Problem Statement: Obtain a number from the user, then display twice this number back to the user.

1. display prompt to user
3. result = number * number
4. display result

• Almost as easy to learn as the English Algorithm technique.
• Easier to translate into a programming language than the English Algorithm technique. The psuedo-code statements closely resemble most prodecural programming languages.

• This technique relies on your understanding of the procedural programming languages. For someone learning their first programming language, this can be frustrating.

Second Example

Refer to the comparison of the four techniques for a second example.

Navigation: Go to top, natural-language, pseudo-code, flowchart, Nassi-Schneiderman.

Flowchart

This technique results in a series of symbols, connected by arrows that indicate the sequence in which the symbols are performed. Each symbol contains either a natural language or pseudo-code statement, and has a unique use within the flowcharting technique. These symbols and their use (purpose) are: The rectangle symbol is used to denote a process (statement) that is performed in sequence. After the statement is performed, flow continues to the next symbol in the flowchart. The rhombus symbol is used to denote either input from the user or output to the user. After the input or ouptut is performed, flow continues to the next symbol in the flowchart. The diamond symbol is used to denote a decision. After the decision is performed, flow continues in one of two paths, depending on the result of the decision. The two paths coming from the decision symbol are typically labeled as Yes/No or True/False. The arrow symbol is used to connect the other symbols and indicate the direction of execution (flow). The circular symbol is used to connect different parts of a flowchart. This symbol is used when the flowchart is larger than can fit onto one page. The connector symbol is used to branch between the separate pages of the flowchart and has no signficance on the logical correctness of the flowchart. The oval symbol is used to start (begin) and stop (end) the flowchart. The start oval indicates where the flowchart begins execution. The stop oval ends the execution of the flowchart.

First Example

Problem Statement: Obtain a number from the user, then display twice this number back to the user. • Graphical presentation of algorithm lends itself to finding logic errors, since the sequence of flow is graphical (i.e., "a picture is worth a thousand words").
• Use of the Connector symbol allows you to expand/change a flowchart rather easily.
• Can be easier to translate into a programming language than the English Algorithm or Pseudo-code Algorithm techniques.

• Need to remember the meaning of each flowchart symbol.
• Can be harder to translate into a programming language than the English Algorithm or Pseudo-code Algorithm techniques. A flowchart that looks like a plate of spaghetti (arrows going every which way) can be very dificult to translate into program code.

Second Example

Refer to the comparison of the four techniques for a second example.

Navigation: Go to top, natural-language, pseudo-code, flowchart, Nassi-Schneiderman.

Nassi-Schneiderman chart

This technique results in a series of symbols, contained within a box. The symbols are executed starting with the first symbol at the top of the box, and continuing until the bottom of the box is reached. Each symbol contains either a natural language or pseudo-code statement, and has a unique use within the Nassi-Schneiderman Notation. These symbols and their use (purpose) are: The rectangle symbol is used to denote a statement that is performed in sequence. After the statement is performed, flow continues to the next symbol in the Nassi-Schneiderman chart. The decision structure contains a condition which is tested. When the condition is true, the instructions underneath the True branch are executed. When the condition is false, the instructions underneath the False branch are executed. After the appropriate instructions are performed, flow continues with the next symbol contained within the Nassi-Schneiderman chart. The loop structure contains a loop statement along with instructions to execure as long as the loop statement is true. After the instructions are performed (zero or more times), flow continues with the next symbol contained within the Nassi-Schneiderman chart.

First Example

Problem Statement: Obtain a number from the user, then display twice this number back to the user. • Graphical presentation of algorithm lends itself to finding logic errors, since the sequence of flow is graphical (i.e., "a picture is worth a thousand words").
• Easier to translate into a programming language than the flowchart technique. The only three symbols supported - statement, decision and loop - are found in all procedural programming languages. In addition, the complete lack of arrows in Nassi-Schneiderman Notation means that you can not create a plate of spaghetti while drawing the chart.

• Need to remember how to draw the decision and loop structures.
• May be more dificult to expand/change as compared to the flowchart technique since the use of Connectors and arrows give you maximum flexibility when drawing a flowchart. On the other hand, you can certainly extend a Nassi-Schneiderman chart to another page by simply drawing a box at the top of the next page and labeling this page 2.

Second Example

Refer to the comparison of the four techniques for a second example.

Navigation: Go to top, natural-language, pseudo-code, flowchart, Nassi-Schneiderman.

Comparison

This section allows you to compare the four techniques by showing a similar solution for the same problem statement, written in each of the techniques.

Problem Statement: Obtain numbers from the user, then display the average of all numbers entered once the user has completed the entry of numbers.

 Natural-language Algorithm Pseudo-code Algorithm Initialize sum of all numbers entered to zero. Initialize count of numbers entered to zero. Ask the user for a number. Get a number from the user. Repeat steps 5a...5d until there are no more numbers entered. Add number entered to sum of all numbers entered. Add one to the count of numbers entered. Ask the user for a number. Get a number from the user. If the count of numbers entered is zero, do step 6a: Display a message indicating unable to calculate average since no numbers were entered. Otherwise do steps 6b...6c: Divide the sum of all numbers entered by the count of numbers entered. Display the result from step 6b to the user. sum = 0 count = 0 display prompt to user read number while more numbers do sum = sum + number count = count + 1 display prompt to user read number if count = 0 then display message "unable to calculate average since no numbers were entered" else average = sum / count display average

Flowchart Nassi-Schneiderman chart 