As we’ve covered with if, if-else, and switch statements, decision making is essential to managing a program’s flow. It’s usually helpful to prepare your approach before writing the actual C code to implement these decisions and other program operations. Creating algorithms is a part of this planning stage, and pseudocode and top-down, stepwise refinement are two useful methods for this.
At its core, an algorithm is a process for resolving an issue. It is characterised by the steps that must be taken and the sequence in which they must be taken. Program control is the process of specifying this order of execution.
C Pseudocode
What it is: An informal artificial language that mimics standard English is called pseudocode. It isn’t a real language for computer programming.
Purpose: Before writing a program in a programming language like C, it helps you “think out” the program and create algorithms.
Characteristics
- It’s easy to use and convenient.
- You can type it in any text editor because it is made entirely of characters.
- Converting well-written pseudocode to the matching C program is frequently simple.
- After the program is converted to C and run, it outlines the choices and activities that will take place.
- In C, definitions (such as variables) are messages to the compiler to reserve memory; in pseudocode, they are typically not executable statements. While some programmers use pseudocode to list variables and their functions, others do not.
- Statements in pseudocode are usually indented to demonstrate structure, such as the body of a loop. In contrast to C, statements in pseudocode loops are not grouped together using braces {}.
Top-Down, Stepwise Refinement
What it is: This method is used to create programs that are well-structured. It’s an approach to creating algorithms.
Process
- The “top,” a declaration that expresses the general purpose of the program, is represented by high-level pseudocode at the outset. At a very abstract level, the top is seen as a comprehensive representation of the program.
- This “top” sentence gets broken down into smaller jobs during the refinement phase. These tasks are listed in the order that they were completed.
- One or more pseudocode statements from the preceding level are broken down into smaller, more explicit steps in each succeeding refinement.
- With the exception of detail level, each refinement is a comprehensive statement of the method.
- You keep doing this until the pseudocode algorithm gives you enough information to translate it straight into C code.
Benefits
- By employing a “divide and conquer” strategy, it makes program development easier to handle.
- It lessens the complexity of the program.
- By dividing programs into smaller, clearly defined components typically implemented as functions in C it makes the process of creating modular programs easier.
- Programs are simpler to write, comprehend, test, and debug with this modularity.
- It encourages program reusability by enabling you to use pre-existing functions as building blocks.
- It facilitates project progress tracking.
The top-down method is particularly supported by the C programming language, which lets you break down a solution into modules, which are frequently functions. As long as you are familiar with the interface (arguments and return type) of the function you are calling, you can write calls to it in your main or other functions before you have even created the code for the called function.
Illustrative Example: Analyzing Exam Results
The challenge of evaluating exam results to decide whether to give a bonus to a teacher is a clear example that illustrates creating an algorithm using pseudocode and top-down, progressive refinement.
This is how the procedure goes:
Pseudocode Representation of the Top: Let’s begin with the problem’s broadest statement: Examine the results and determine if the instructor deserves a bonus.
First Refinement: Divide the top statement into the following main steps: Set up the variables. Enter the scores from the ten quizzes and note the successes and failures. Print a summary of the exam results, then determine if to give the teacher a bonus.
Second Refinement: Provide specifics for every step in the initial refining.
- Make it better Choose particular counts and initialise them to start variables: Set the initialisation passes to zero. Set the initial failures to zero. Set the student’s initial value to one.
- Make it better Enter the scores from the ten quizzes and note the successes and failures. Counter-controlled looping is suitable as the number of exams (10) is known beforehand. To determine whether the student passed, a choice (using if-else) must be made inside the loop. However, the student counter is equal to or fewer than ten. Enter the results of the next exam. Add one to passes if the student passed; if not, To failures, add one. Add one to the student counter (notice the indentation to display the nested if-else and the while loop structure).
- Make it better Decide whether to give the instructor a bonus after printing an exam results report. After printing the totals, a final decision based on the number of passes is made. Print the quantity of passes. Print the quantity of failures. If more than eight pupils were successful “Bonus to instructor” should be printed.
Final Pseudocode Algorithm: The whole pseudocode method is obtained by combining the enhancements.
1 Initialize passes to zero
2 Initialize failures to zero
3 Initialize student to one
4
5 While student counter is less than or equal to ten
6 Input the next exam result
7
8 If the student passed
9 Add one to passes
10 else
11 Add one to failures
12
13 Add one to student counter
14
15 Print the number of passes
16 Print the number of failures
17 If more than eight students passed
18 Print “Bonus to instructor!”
It is presently thought that this pseudocode is sufficiently polished to be translated to C.
Corresponding C Code
The algorithm outlined in the pseudocode is implemented in this C code.
#include <stdio.h> // Required for printf and scanf
int main(void) {
// Initialization phase- Corresponds to lines 1-3 in pseudocode
int passes = 0; // Initialize passes counter
int failures = 0; // Initialize failures counter
int student = 1; // Initialize student counter
int result; // Variable to store each exam result
// Processing phase- Corresponds to lines 5-13 in pseudocode
// Loop 10 times for ten exam results
while (student <= 10) { // Condition for counter-controlled loop
// Input the next exam result
printf("Enter result (1=pass, 2=fail): ");
scanf("%d", &result); // Read integer input
// Nested decision making (selection)- Corresponds to lines 8-11 in pseudocode
if (result == 1) { // Check if the student passed
passes = passes + 1; // Increment passes counter
} else { // Otherwise, assume they failed
failures = failures + 1; // Increment failures counter
}
// Increment student counter to process the next student
student = student + 1;
} // End of while loop
// Termination phase- Corresponds to lines 15-18 in pseudocode
// Print exam results summary
printf("Passed: %d\n", passes);
printf("Failed: %d\n", failures);
// Decide whether to bonus the instructor
if (passes > 8) { // Final decision based on total passes
printf("Bonus to instructor!\n");
}
return 0; // Indicate successful program execution
}
This example demonstrates how top-down, incremental refining in the pseudocode produces a clear, structured plan that can be readily translated into a C program using control structures like nested if-else statements and the while loop. This procedure aids in the creation of organised, manageable programs through the use of pseudocode and refinement.