An algorithm on paper and a working program are separated by a careful, mechanical step: translation. When it is done one statement at a time, with testing along the way, most errors never arise.
Translating Step by Step
Each construct of pseudocode has a direct equivalent in C, as shown in Table 2.12. Translation consists of replacing each line of the algorithm by its C form and declaring every variable that the algorithm uses.
Table 2.12: Pseudocode constructs and their C equivalents
| Pseudocode | C |
|---|---|
| READ n | scanf("%d", &n); |
| DISPLAY sum | printf("%d", sum); |
| sum = sum + i | sum = sum + i; |
| IF c THEN … ELSE … ENDIF | if (c) { … } else { … } |
| WHILE c DO … ENDWHILE | while (c) { … } |
| FOR i = 1 TO n … ENDFOR | for (i = 1; i <= n; i++) { … } |
Selection and Repetition in C
The selection statements if and if-else and the repetition statements for, while and do-while were introduced earlier in this unit. In translation, the important decision is the choice of loop: a FOR in the algorithm becomes a for loop, a WHILE becomes a while loop, and a loop whose body must run before the test becomes a do-while loop.
Incremental Compilation and Testing
Definition: Incremental development. Building a program in small parts, compiling and testing after each part is added, so that any error lies in the part most recently written.
A beginner who types fifty lines before compiling often faces a long list of errors with no idea where they began. The incremental method avoids this. The cycle is
Test data should include an ordinary case, the boundary cases and, where input is validated, an invalid value. The expected output is worked out by hand before the program is run, so that the comparison is honest.
Good Coding Practices
A program is read far more often than it is written. The following practices make it easier to read and to correct.
- Meaningful names:
totalandcountexplain themselves;tandcdo not. - Consistent indentation: statements inside a block are indented by the same amount.
- Braces for every block: even when the block contains one statement.
- Useful comments: a comment explains why something is done, not what the statement obviously does.
- One statement per line: and one blank line between logical parts.
- Prompts for input: tell the user what to enter.
Example 2.12: From algorithm to program
Problem: Translate the following algorithm into C and test it: read n, then find the sum of the natural numbers from 1 to n.
BEGIN
READ n
sum = 0
FOR i = 1 TO n
sum = sum + i
ENDFOR
DISPLAY sum
END
Solution:
- The variables n, sum and i are whole numbers, so they are declared as
int. - READ becomes scanf(), the FOR loop becomes a
forloop and DISPLAY becomes printf().
#include <stdio.h>
int main(void)
{
int n, i, sum = 0;
printf("Enter n: ");
scanf("%d", &n);
for (i = 1; i <= n; i++)
{
sum = sum + i;
}
printf("\nSum of 1 to %d = %d\n", n, sum);
return 0;
}
Input:
10
Output:
Enter n:
Sum of 1 to 10 = 55
Result: By hand, 1 + 2 + … + 10 = 55, which agrees with the output. A boundary test with n = 1 should give 1.
Common mistakes
- Typing an entire program before compiling it for the first time.
- Correcting many reported errors at once instead of fixing the first and compiling again.
- Deciding the expected output only after seeing what the program displays.
Key takeaways
- Each pseudocode construct has a direct C equivalent.
- Programs are built in small parts, compiling and testing after each.
- Test data includes ordinary, boundary and invalid cases, with expected output worked out beforehand.
- Meaningful names, indentation, braces and comments make programs readable.
Practice questions
- Which C statement corresponds to "FOR i = 1 TO n" in pseudocode?
- What is the benefit of compiling after every small addition?
- Give two good coding practices.
Answers to the practice questions
- for (i = 1; i <= n; i++)
- Any new error must lie in the part just added, so it is easy to find.
- Any two of: meaningful names, consistent indentation, braces for every block, useful comments, one statement per line, prompts for input.
Sources
- Official BCA syllabus (NEP 2025 scheme), Problem Solving Techniques, course code 25BCA401SEC01, Unit II
