Before a student writes a single line of C or Python, they should be able to solve the problem on paper. That is what algorithms and flowcharts are for. I taught this as the first lecture in every programming course at FUUAST, because students who skip it write code that "works" by accident. Here is that lecture in written form.
A computer is a tool, not a solution
Solving any engineering problem follows the same path: understand the problem, collect the facts, choose a method, state your assumptions, solve, and verify. The computer helps only with the "solve" step — and only if you can tell it exactly what to do.
What is an algorithm?
An algorithm is a finite, ordered list of unambiguous steps that solves a problem. A recipe is an algorithm. Directions to your house are an algorithm. Three properties matter: every step must be clear, the steps must be in order, and the process must end.
Example — average of three numbers:
- Start
- Read A, B, C
- Sum = A + B + C
- Avg = Sum / 3
- Print Avg
- Stop
What is a flowchart?
A flowchart is the same algorithm drawn as a diagram, so that the flow of control is visible. The symbols are standard:
| Symbol | Shape | Meaning |
|---|---|---|
| Terminal | Rounded rectangle (oval) | Start / Stop |
| Input / Output | Parallelogram | Read or Print |
| Process | Rectangle | Calculation or assignment |
| Decision | Diamond | Yes/No question; two exits |
| Connector | Small circle | Joins parts of a large chart |
| Flow line | Arrow | Direction of control |
| Preparation | Hexagon | Loop initialisation (for loops) |
| Predefined process | Rectangle with double sides | A sub-program / function |
The three structures every program is built from
1. Sequence — steps one after another, like the average example above.
2. Selection (decision) — a diamond with two paths. Example: read marks; if marks ≥ 50 print "Pass" else print "Fail". In code this becomes if / else.
3. Repetition (loop) — a decision whose "yes" path flows back to an earlier step. Example: set N = 1; print N; N = N + 1; if N ≤ 10 go back to print. In code: while or for.
Every real program is a combination of these three. If you can draw a problem using only these structures, you can code it in any language.
From flowchart to program — the five steps
- Write the algorithm and draw the flowchart.
- Translate it into a language (C, Python, Java).
- Type and compile.
- Test with normal, boundary and wrong inputs; fix bugs.
- Run with real data.
Practice problems
- Read a number and print whether it is even or odd.
- Read three numbers and print the largest.
- Print the multiplication table of a number entered by the user.
- Read marks of five subjects and print the grade (A/B/C/F).
Draw each one before coding it. For the next step see a small C program that reads and tokenises a file, or the OOP video lectures in Urdu.
Comments
Post a Comment