Padhai Search notes and questions

C ProgrammingUnit 13 min read

Problem Solving with Computer: Algorithm and Flowchart

Unit 1 of BSc CSIT C Programming: problem analysis, algorithms, flowcharts, pseudocode, coding, compiling, debugging and program documentation, with worked examples.

Key points

  • Problem solving with a computer runs from analysis to algorithm, flowchart, coding, compiling, debugging and documentation.
  • An algorithm is a finite sequence of clear steps with input, output, definiteness, finiteness and effectiveness.
  • A flowchart draws an algorithm with standard symbols: an oval for start/stop, a parallelogram for input/output, a rectangle for a process and a diamond for a decision.
  • Pseudocode writes an algorithm in structured English, between an algorithm and real code.
  • Errors are syntax errors (found by the compiler), run-time errors (such as division by zero) and logical errors (wrong output).

A computer does exactly what it is told, so before writing any code you need a clear, step-by-step plan. This unit covers how to get from a problem to a working program.

Steps in problem solving

  1. Problem analysis. Work out what the inputs are, what output is needed, and any conditions or limits.
  2. Algorithm development. Write the solution as a finite list of clear steps.
  3. Flowcharting. Draw the algorithm so that the flow of control is easy to follow.
  4. Coding. Translate the algorithm into a programming language such as C.
  5. Compilation and execution. Turn the source code into machine code and run it.
  6. Debugging and testing. Find and fix errors, then check the program against known inputs.
  7. Documentation. Record how the program works so that others (and you, later) can maintain it.

Algorithm

An algorithm is a finite sequence of well-defined steps that solves a problem. A good algorithm has these properties:

  • Input: zero or more quantities are supplied.
  • Output: at least one result is produced.
  • Definiteness: every step is clear and unambiguous.
  • Finiteness: it stops after a finite number of steps.
  • Effectiveness: every step is simple enough to be carried out.

Example: largest of three numbers

Step 1: Start
Step 2: Read a, b, c
Step 3: If a > b and a > c, largest = a
        Else if b > c, largest = b
        Else largest = c
Step 4: Print largest
Step 5: Stop

Flowchart

A flowchart is a diagram of an algorithm drawn with standard symbols.

Symbol Name Used for
Oval Terminal Start and Stop
Parallelogram Input/Output Reading and printing
Rectangle Process Calculations and assignments
Diamond Decision Yes/No conditions
Arrow Flow line Direction of flow
Small circle Connector Joining parts of a chart

Advantages: easy to understand, helps find logic errors, and serves as documentation. Limitations: hard to draw and change for large programs, and it gets cluttered with many branches.

Pseudocode

Pseudocode describes an algorithm in structured English. It sits between an algorithm and real code:

READ a, b, c
IF a > b AND a > c THEN
    largest ← a
ELSE IF b > c THEN
    largest ← b
ELSE
    largest ← c
ENDIF
PRINT largest

Types of errors

  • Syntax errors break the rules of the language, such as a missing semicolon. The compiler reports them.
  • Run-time errors happen while the program runs, such as division by zero.
  • Logical errors give wrong output without crashing, such as using + where you meant *. These are the hardest to find.

Exam tip

"Write an algorithm and draw a flowchart to …" appears almost every year. Practise the classic problems: the largest of three numbers, whether a number is even or odd, factorial, Fibonacci series, prime check, and reversing a number.

Based on the TU BSc CSIT syllabus for C Programming (CSC110), unit 1.