Resources | Subject Notes | Information Technology IT
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.
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 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.
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.
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.
Step | Description |
---|---|
1 | START |
2 | INPUT length, width |
3 | Calculate area = length * width |
4 | WRITE area |
5 | STOP |
Here's an algorithm to find the largest of three numbers using a `FOR` loop and a `WHILE` loop.
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 |
This algorithm uses a `WHILE` loop to determine if a number is even or odd.
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 |