MCS-211 · Block 1 Unit 1

Basics of an Algorithm and its Properties

Section 3 of 9

Block 1 · Unit 1Basics of an Algorithm and its Properties10 min · Section 3 of 9

1.3Building Blocks of Algorithms

Covers IGNOU MCS-211 §1.3, 1.3.1, 1.3.2

Every algorithm, however large, is assembled from a handful of control structures. This page explains the three basic ones and two structuring tools that make big algorithms manageable.

The three basic control structures

These three are enough to express any algorithm. This result is known as the Böhm–Jacopini theorem (1966), the basis of structured programming.

In Euclid’s algorithm:

LinesBuilding blockWhy
r ← a mod b; a ← b; b ← rSequencingThe order matters: swapping the last two lines gives a wrong answer.
while b ≠ 0 doIterationThe three lines repeat while the condition is true.
The test b ≠ 0Condition of the loopEach time it is checked, it decides between “repeat the body” and “leave the loop”.

The study material describes the while line as both selection and iteration. It is better to call it iteration: a loop contains a test, but its purpose is repetition. A pure selection is an if statement.

Procedures

When the same group of steps is needed in many places, we write it once as a procedure (also called a subprogram, function or method) and call it with different arguments. A procedure that returns a value uses a return statement.

C · sum_procedure.c
#include <stdio.h>

/* A procedure (function) written once and called three times. */
int sum_of_array(const int a[], int n)
{
    int total = 0;
    for (int i = 0; i < n; i++)   /* iteration */
        total += a[i];            /* sequencing inside the loop */
    return total;
}

int main(void)
{
    int marks[]  = {72, 65, 88};
    int prices[] = {120, 80, 45, 55};
    int empty[1] = {0};

    printf("Total marks : %d\n", sum_of_array(marks, 3));
    printf("Total price : %d\n", sum_of_array(prices, 4));
    printf("Empty list  : %d\n", sum_of_array(empty, 0));
    return 0;
}
Output
Total marks : 225
Total price : 300
Empty list  : 0

Recursion

The classic example is the factorial: n!=n×(n−1)!n! = n \times (n-1)! for n≥1n \ge 1, and 0!=10! = 1.

A recursive procedure is correct only if:

  1. it has at least one base case that is solved without a further call;
  2. every recursive call uses a smaller argument that moves towards a base case;
  3. the system can keep track of unfinished calls. Every modern language does this with the call stack: each call gets its own stack frame holding its parameters, local variables and return address.
C · factorial_trace.c
#include <stdio.h>

/* Recursive factorial that prints each call and each return.
   depth is used only to indent the trace. */
unsigned long long factorial(unsigned int n, int depth)
{
    printf("%*scall factorial(%u)\n", depth * 2, "", n);
    if (n <= 1) {                                   /* base case */
        printf("%*sreturn 1\n", depth * 2, "");
        return 1;
    }
    unsigned long long result = n * factorial(n - 1, depth + 1);  /* recursive case */
    printf("%*sreturn %llu\n", depth * 2, "", result);
    return result;
}

int main(void)
{
    unsigned long long f = factorial(4, 0);
    printf("4! = %llu\n", f);
    return 0;
}
Output
call factorial(4)
  call factorial(3)
    call factorial(2)
      call factorial(1)
      return 1
    return 2
  return 6
return 24
4! = 24

The base case is n <= 1, which also handles 0!=10! = 1. The study material’s version uses only n equals 1 as the base case, so a call with n=0n = 0 would never stop.

Sequencing
steps in written order
Selection
if–then–else decision
Iteration
loop: repeat while true
Procedure
named block, called many times
Recursion
procedure calls itself on a smaller case

The first three are sufficient for any algorithm; the last two make large algorithms easier to write.

The five building blocks

Key takeaways

  • Sequencing, selection and iteration are sufficient for any algorithm (Böhm–Jacopini).
  • A procedure is written once and called many times with different arguments.
  • A recursive procedure needs a base case and must move towards it on every call.
  • The call stack stores one frame per active call; deep recursion uses more memory.

Practice questions

Answer on paper first, then check. The explanation matters more than the letter you chose.

Question 1 · multiple choice

Which building block is shown by if (marks >= 40) pass(); else fail();?

Show answer

Answer: B. Selection

The next action is chosen by testing a condition, which is selection. There is no repetition, so it is not iteration.

Question 2 · multiple choice

Which set of control structures is enough to express any algorithm?

Show answer

Answer: C. Sequencing, selection and iteration

This is the Böhm–Jacopini theorem. Procedures and recursion are useful but not required.

Question 3 · multiple choice

What does this program print?

C · quiz_sum_rec.c
#include <stdio.h>

int f(int n)
{
    if (n == 0)
        return 0;
    return n + f(n - 1);
}

int main(void)
{
    printf("%d\n", f(5));
    return 0;
}
Show answer

Answer: D. 15

f(5) = 5 + 4 + 3 + 2 + 1 + 0 = 15. The answer 120 would be 5!, which multiplies instead of adds.

Question 4 · multiple choice

A function is defined as int g(int n) { return n * g(n - 1); }. What is wrong with it?

Show answer

Answer: A. It has no base case, so it never stops calling itself.

There is no condition that returns without a further call, so the calls continue until the stack overflows. The argument does get smaller; that alone is not enough.

Question 5 · multiple choice

In the factorial(4) trace, what is the maximum number of factorial calls on the stack at the same time?

Show answer

Answer: C. 4

factorial(4), (3), (2) and (1) are all active when the base case is reached. 24 is the result, not a stack depth.

Question 6 · short answer

State the conditions a recursive procedure must satisfy and show them in a recursive algorithm for the sum 1+2+⋯+n1 + 2 + \dots + n.

Show worked answer

(1) A base case solved without recursion; (2) each call uses a smaller argument that moves towards the base case; (3) the system supports calls through a call stack. Algorithm: Sum(n): if n = 0 then return 0 (base case) else return n + Sum(n − 1) (smaller argument). Example: Sum(3) = 3 + Sum(2) = 3 + 2 + Sum(1) = 3 + 2 + 1 + Sum(0) = 6.

↑ ↓ move↵ openTab switch type