Contactnomanakhter.com

Noman Akhter

Technology · Business · Tutorials
Programming

Algorithms and Flowcharts: A Beginner's Introduction to Problem Solving

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:

  1. Start
  2. Read A, B, C
  3. Sum = A + B + C
  4. Avg = Sum / 3
  5. Print Avg
  6. 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:

SymbolShapeMeaning
TerminalRounded rectangle (oval)Start / Stop
Input / OutputParallelogramRead or Print
ProcessRectangleCalculation or assignment
DecisionDiamondYes/No question; two exits
ConnectorSmall circleJoins parts of a large chart
Flow lineArrowDirection of control
PreparationHexagonLoop initialisation (for loops)
Predefined processRectangle with double sidesA 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

  1. Write the algorithm and draw the flowchart.
  2. Translate it into a language (C, Python, Java).
  3. Type and compile.
  4. Test with normal, boundary and wrong inputs; fix bugs.
  5. 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.

Noman Akhter

Written by Noman Akhter (Startups Coach)

Online business expert and IT consultant since 2005. Founder of NAFsols and Startups Pakistan. More at nomanakhter.com.

Comments

Post a Comment