What a Program Is, and What an Algorithm Is
Chapter One
Syllabus topic 1, "Introduction: Algorithms, History of C, Structure of C Program. Program Characteristics, Compiler, Linker and preprocessor, pseudo code statements and flowchart symbols, Desirable program characteristics."
Pages 1 to 5 of 222
In one line
An algorithm is a finite list of unambiguous steps that turns its inputs, if it has any, into the wanted output, and a program is that algorithm written in a language a computer can be made to follow.
Why you start here and not at the keyboard
A beginner's instinct is to open the editor and start typing. It is the wrong instinct, and the reason is worth understanding rather than being told.
A computer does exactly what it is told, in the order it is told, and it has no idea what you meant. Every part of a problem you have not thought through is a part the machine will get wrong at full speed. So the thinking is done first, in a form you can check with a pencil, and only then translated into C.
That form is the algorithm. It is not a lesser version of the program. It is the part where the problem is actually solved. The C is the part where the solution is written down for a machine.
Your University asks for it in exactly this way. All three parts of the very first practical in Major Practical 1 ask for more than a program. Each one ends "Write algorithm & draw flowchart for the same." The algorithm carries marks of its own.
What makes a list of steps an algorithm
Not every list of instructions qualifies. Five properties are required, and each one rules out a specific way of getting it wrong.
1. Finiteness. The algorithm must stop, after a finite number of steps, for every input it accepts.
A procedure that says "keep adding 1 to n and print it" is not an algorithm. It never ends. This is the property a beginner breaks most often, and it has a name in the wild: the infinite loop.
2. Definiteness. Every step must mean exactly one thing.
"Take a suitable value of interest" is not a step. Suitable to whom? "Set the rate to 7.5" is a step. If two competent people can read your step and do different things, it is not definite.
3. Input. The algorithm takes zero or more inputs, and where they come from is stated.
Zero is allowed. An algorithm that prints the first ten natural numbers needs no input at all. What is not allowed is a step that silently assumes a value nobody supplied.
4. Output. It produces at least one output, and that output is the answer to the problem.
An algorithm that computes the interest correctly and never displays it has failed. This sounds pedantic until the first time a student loses marks for a program that calculates and does not print.
What a Program Is, and What an Algorithm Is
5. Effectiveness. Every step must be basic enough that a person could carry it out exactly, with a pencil, in a finite time.
"Set X to the exact decimal value of one third" is not such a step. It is perfectly definite, and you know exactly what is meant, but the digits never end, so nobody can finish performing it. "Set X to 0.3333, correct to four decimal places" is a step. The test is not whether the instruction can be described; it is whether it can be performed.
A useful way to remember the five: an algorithm must stop, must be unambiguous, must know what it is given, must say what it found, and must be made of steps a human could actually perform.
Writing one out: simple interest
This is the first program on your practical list, so it is the first algorithm here.
To calculate simple interest taking principal, rate of interest and number of years as input from user.
The formula is the easy part. Simple interest is SI = (P R T) / 100, where P is the principal, R is the annual rate as a percentage, and T is the time in years.
The algorithm is written as numbered steps, with a Start and a Stop, and nothing else:
Step 1: Start
Step 2: Read P, R, T from the user
Step 3: Set SI = (P * R * T) / 100
Step 4: Display SI
Step 5: StopRead it against the five properties. It stops at step 5, so it is finite. Every step means one thing. Step 2 names its inputs. Step 4 produces the output. Every step could be done by hand. It is an algorithm.
Notice what is not in it: no mention of C, no printf, no data types, no semicolons. An algorithm is language independent, and that is the point of writing one. The same five steps become a C program, a Python program or a set of instructions to a clerk with a calculator.
Tracing it
An algorithm is checked by tracing: choosing values, walking the steps in order, and writing down what every quantity holds after each one. A trace table is how you prove to yourself, and to an examiner, that the thing works before a compiler is involved.
Take P as Rs 12,000, R as 8.5 per cent and T as 3 years.
| Step | P | R | T | SI | What is displayed |
|---|---|---|---|---|---|
| 1. Start | - | - | - | - | |
| 2. Read P, R, T | 12000 | 8.5 | 3 | - | |
| 3. SI = (P R T) / 100 | 12000 | 8.5 | 3 | 3060 | |
| 4. Display SI | 12000 | 8.5 | 3 | 3060 | 3060 |
| 5. Stop |
What a Program Is, and What an Algorithm Is
The arithmetic: 12000 times 8.5 is 1,02,000, times 3 is 3,06,000, divided by 100 is Rs 3,060. A trace table with one row per step, and one column per quantity, is a habit worth forming now. In the loop chapters it stops being a formality and becomes the only reliable way to find a bug.
A second one, where a decision appears: the greatest of three numbers
Write a program to find greatest of three numbers using conditional operator.
Her practical names the C construct she wants, and the chapter on the conditional operator writes that program. The algorithm below names no construct at all, because an algorithm never does, and it is the same algorithm whichever one you finally reach for.
Simple interest had no decision in it. Most problems do. The moment a step depends on a comparison, the algorithm branches.
Step 1: Start
Step 2: Read A, B, C
Step 3: If A > B and A > C, then set MAX = A
Step 4: Otherwise, if B > C, then set MAX = B
Step 5: Otherwise, set MAX = C
Step 6: Display MAX
Step 7: StopSteps 3, 4 and 5 are one decision with three outcomes, not three separate decisions. Exactly one of them runs, and step 5 can afford to carry no condition at all precisely because steps 3 and 4 have already failed by the time it is reached.
Write them instead as three independent tests and that stops being true. Every one of them would then have to carry a condition of its own, including the last, or MAX would be overwritten with C on every run whatever steps 3 and 4 decided. All three would also be evaluated even after the answer was known.
Trace it with A as 14, B as 27 and C as 9. Step 3 asks whether 14 is greater than both 27 and 9; it is not, so MAX is not set to A. Step 4 asks whether 27 is greater than 9; it is, so MAX becomes 27. Step 5 is skipped because step 4 ran. Step 6 displays 27, which is correct.
Now trace it with all three equal, say 5, 5 and 5. Step 3 fails, because 5 is not greater than 5. Step 4 fails for the same reason. Step 5 runs and MAX becomes C, which is 5. Correct, but only just: the algorithm reaches the right answer through its last resort. Testing the equal case, the all-negative case and the two-equal case is what separates an algorithm that works from one that has only been tried once.
What a Program Is, and What an Algorithm Is
A third one, where the rule itself is the difficulty: the leap year
Write a program to check if the year entered is leap year or not.
Here the coding is trivial and the rule is the whole problem. Most students remember "divisible by 4" and stop. The full rule, as fixed by the Gregorian calendar, has three parts:
- a year divisible by 4 is a leap year,
- except that a year divisible by 100 is not,
- except that a year divisible by 400 is.
So 2024 is a leap year, 1900 was not, and 2000 was. Any algorithm that gets 1900 and 2000 both right has the rule; one that gets either wrong does not.
Step 1: Start
Step 2: Read Y
Step 3: If Y is divisible by 400, then set R = "Leap year"
Step 4: Otherwise, if Y is divisible by 100, then set R = "Not a leap year"
Step 5: Otherwise, if Y is divisible by 4, then set R = "Leap year"
Step 6: Otherwise, set R = "Not a leap year"
Step 7: Display R
Step 8: StopThis has the same shape as the greatest of three: one decision with four outcomes, exactly one of which runs, and then a single Display that every path reaches. An algorithm with one way out is easier to check and easier to draw than one that finishes in four different places.
The order of the tests is the algorithm. Test 400 first, then 100, then 4, and each test can be simple because the ones before it have already removed the exceptions. Reverse the order and every test needs conditions bolted onto it.
Check it on the years that settle whether a rule is right:
| Y | Divisible by 400? | Divisible by 100? | Divisible by 4? | Displayed |
|---|---|---|---|---|
| 2000 | yes | not reached | not reached | Leap year |
| 1900 | no | yes | not reached | Not a leap year |
| 2024 | no | no | yes | Leap year |
| 2023 | no | no | no | Not a leap year |
From algorithm to program
Once the algorithm is right, writing the C is mechanical, and the rest of this book is how. The order never changes:
- Understand the problem, and write down what is given and what is wanted.
- Write the algorithm, in steps, in ordinary words.
- Trace it by hand on values you have chosen to be awkward.
- Only then, translate it into C.
- Compile it, run it, and check it against the trace you already did.
Step 3 is the one that gets skipped, and skipping it is why a program that compiles cleanly still prints the wrong number. The compiler checks your grammar. Nothing but a trace checks your thinking.
What a Program Is, and What an Algorithm Is
What an examiner can ask on this, and how to answer it
"Define an algorithm and state its characteristics." Give the one-line definition, then the five properties with a word of explanation each. A bare list of five nouns is a weak answer; each property with the failure it prevents is a full one.
"Write an algorithm to ..." Number the steps, open with Start and close with Stop, name your inputs in a Read step, and finish with a Display step. Keep the steps in ordinary words: an algorithm written in C statements is no longer language independent, which is the one thing it exists to demonstrate.
"What is the difference between an algorithm and a program?" An algorithm is a finite sequence of unambiguous steps solving a problem, written in ordinary language, independent of any programming language and not executable by a machine. A program is that algorithm expressed in a particular programming language, following that language's grammar, and executable once translated. One is the solution; the other is the solution written down for a computer.
A trace question. You may be given a short algorithm and asked what it displays for stated inputs. Build the trace table. Do not read the algorithm and guess.
The rest of this subject
These notes are cut from the University's printed syllabus. Open the syllabus itself for the same subject.