Use pseudocode (INPUT, WRITE, FOR, WHILE)

Resources | Subject Notes | Information Technology IT

IT 9626 - Algorithms and Flowcharts

IT 9626 - Algorithms and Flowcharts

Objective: Use Pseudo-code (INPUT, WRITE, FOR, WHILE)

This section provides detailed notes on algorithms and flowcharts, focusing on the use of pseudo-code with the keywords INPUT, WRITE, FOR, and WHILE. We will cover fundamental concepts, examples, and best practices for designing and documenting algorithms.

What is an Algorithm?

An algorithm is a finite sequence of well-defined, computer-implementable instructions that leads to the solution of a problem. It's a step-by-step procedure.

Flowcharts

Flowcharts are diagrams that visually represent the steps in an algorithm. They use standardized symbols to show the flow of control. Flowcharts are a valuable tool for planning and documenting algorithms before coding.

Common Flowchart Symbols

  • Start/Stop: Oval
  • Process: Rectangle
  • Input/Output: Parallelogram
  • Decision: Diamond
  • Flow Lines: Arrows

Pseudo-code

Pseudo-code is an informal way of describing an algorithm. It uses a combination of natural language and programming-like constructs to outline the steps. It's easier to read and understand than actual code, but it's more structured than plain English.

Key Pseudo-code Keywords

  • INPUT: Used to get data from the user or a file.
  • WRITE: Used to display output to the user or a file.
  • FOR: Used to repeat a block of code a specified number of times.
  • WHILE: Used to repeat a block of code as long as a condition is true.

Example 1: Calculating the Area of a Rectangle

Let's design an algorithm to calculate the area of a rectangle. The algorithm will take the length and width as input and output the area.

Flowchart

Suggested diagram: A flowchart showing input of length and width, calculation of area, and output of the area.

Pseudo-code

Step Description
1 START
2 INPUT length, width
3 Calculate area = length * width
4 WRITE area
5 STOP

Example 2: Finding the Largest of Three Numbers

Here's an algorithm to find the largest of three numbers using a `FOR` loop and a `WHILE` loop.

Flowchart

Suggested diagram: A flowchart showing input of three numbers, comparison using a loop, and output of the largest number.

Pseudo-code

Step Description
1 START
2 INPUT num1, num2, num3
3 SET largest = num1
4 FOR i = 2 TO 3
5 IF num(i) > largest THEN
6 SET largest = num(i)
7 ENDIF
8 ENDFOR
9 WRITE largest
10 STOP

Example 3: Checking if a Number is Even or Odd

This algorithm uses a `WHILE` loop to determine if a number is even or odd.

Flowchart

Suggested diagram: A flowchart showing input of a number, a while loop checking for divisibility by 2, and output indicating even or odd.

Pseudo-code

Step Description
1 START
2 INPUT number
3 WHILE number mod 2 <> 0
4 WRITE "Odd"
5 number = number / 2
6 ENDWHILE
7 WRITE "Even"
8 STOP

Best Practices

  • Use clear and concise language.
  • Indicate the flow of control clearly.
  • Consider edge cases and error handling.
  • Test your algorithms with sample inputs.