Most computer programs follow sets of rules so that they can operate perfectly every day without human interference. These rules are called Algorithms. Before any code is written, these rules must be identified and formalised in a process referred to as ‘Algorithm Design’.
In this lesson, we will learn about the process of designing algorithms, which includes the following:
An algorithm is a description, independent of language, of a process that achieves some task. The idea is that you could give an algorithm, written in a text file, to any programmer and then he can build your program in whatever language – Python, C#, VB.NET, Pascal, C++, Java, etc.
It can also be thought of as a sequence of steps, independent of any programming language that can be performed over and over again, to achieve a task, almost like a set of rules.
We follow algorithms every day without thinking. Consider an algorithm for crossing the road:
Is this algorithm correct? Could you follow these rules every time you cross the road?
The above algorithm is a good start for the rules we follow for crossing the road. However, what if the crossing button has already been pressed? What if there are no cars coming?
How can this algorithm be modified to cope with all possibilities of crossing the road?
In order to design an algorithm, you need to follow some basic steps.
Let us consider our algorithm from Lesson 2 for a teacher logging into an Information Management System and searching for a student.
The teacher will need to log in using a username and password.
The teacher will be given a list of matching students from which they need to select the correct one. Once they select a student, the student’s ID will be used to display the full details.
Make sure you fully understand the problem you have been set. A teacher needs to log in, search by surname, pick from a list and view the correct data. If you do not fully understand the problem, you need to go back to your Client.
You need to identify the type and values of the data. These can correspond to NOUNS in a problem statement e.g. “search by SURNAME” indicates a noun input.
First is a username and password, they will be a value as words or words and numbers.
Then there is the student surname, this will be words, this is shown in figure 3.1.
Finally, there will be the student ID selected from the list, this is shown in figure 3.2.
Are there any calculations, or is there any other computational operations going on? Are there any? These usually correspond to verbs in a problem statement e.g. “SEARCH by surname” indicates a verb process.
The username and password will be used to search the database to see if there is a match.
The student name will be used to search the databases to see if there are any matches.
The selected student ID will be used to search the database to retrieve the student details.
There has to be a database, containing a list of usernames and passwords, and a list of students with all of their contact details as well as their student IDs.
We don’t know if this is going to be one or two databases at this time.
The first output will be either a confirmation or rejection of the teacher login.
The second output will be a list of students with a matching surname to the input. This is shown in figure 3.1
The final output will be the contact details of the student selected from the list, shown in figure 3.2
Organise your data into a Quad Diagram, this is simply four boxes showing the Inputs, Processes, Outputs and Data storage required by your algorithm.
Produce a Quad Diagram for the Student Planner Program identified in Lesson 1, showing Input, Output, Process and Data Storage.
So to summarise what we have learned in this lesson: