ALGORITJHM, FLOWCHART AND PSEUDOCODE.
ALGORITJHM, FLOWCHART AND PSEUDOCODE.
An algorithm is a set of defined steps designed to perform a specific objective. This can be a
simple process, such as a recipe to bake a cake, or a complex series of operations used
in machine learning to analyze large datasets and make predictions. In the context of machine
learning, algorithms are vital as they facilitate the learning process for machines, helping them to
identify patterns and make decisions based on data.
Algorithms Explained
Algorithms are structured sets of instructions designed to solve specific problems or perform
particular tasks. They function through a series of well-defined steps, each contributing to the
ultimate goal. Here, we break down the typical stages involved in the functioning of an
algorithm:
1. Input. The first step involves defining the inputs that the algorithm will use. Inputs are the data
on which the algorithm will operate. It could be anything from a single value to a complex data
structure.
2. Processing. This is the core phase where the algorithm performs operations on the inputs using a
series of computational steps. This phase is guided by logical and arithmetic calculations to
process the data effectively. Within the processing phase, there are often crucial substeps:
Decision making. At various points during processing, decisions need to be made based on
certain conditions. This substep involves directing the flow of the algorithm based on conditional
statements, leading to different paths in the algorithm.
Looping. For many algorithms, certain steps need to be repeated multiple times until a specific
condition is met. Looping allows the algorithm to execute the same steps repeatedly, optimizing
the process and saving time.
3. Output. After processing the inputs through various computational and conditional steps, the
algorithm produces an output. This output is the result of the algorithm’s operations and is used
to solve the problem or perform the task at hand.
4. Termination. An algorithm must have a defined stopping point to ensure it doesn’t run
indefinitely. Once all the steps are executed successfully, and the output is produced, the
algorithm reaches its termination point.
1. Input. The algorithm receives temperature data from a sensor located within the home.
2. Processing.
Decision making. The algorithm decides the state of the heating system based on the temperature
data it receives:
If the temperature is below a certain lower threshold, it turns the heating system on.
If the temperature is above a certain upper threshold, it turns the heating system off.
If the temperature is between the two thresholds, it maintains the current state of the heating system.
Looping. The algorithm checks the temperature data every second to decide whether any action
needs to be taken.
3. Output. In this scenario, the output could be seen as the state of the heating system at any given
moment (on, off, or unchanged) and any adjustment made to the home’s temperature. However, not
every algorithm needs to produce an observable output, as some may run in the background to
maintain a certain state or condition.
4. Termination. This algorithm does not have a fixed termination point as it continues to run as long as
the heating system is active, or until someone turns off the heating system at the control panel.
Social media platforms are another area where algorithms play a crucial role.
They analyze a plethora of data, including your interactions, the content you
like, and the people you follow, to curate a feed that is tailored to your
interests, keeping you engaged for longer periods.
Types of Algorithms
Understanding the different types of algorithms can help in selecting the
most appropriate one for solving a specific problem. Broadly, we can
categorize algorithms based on their use cases and their structural or
problem-solving strategies:
Correctness. Foremost, a good algorithm must be correct, meaning it should always produce the
right output for any given input. It should be free of errors and bugs to ensure reliable
performance.
Efficiency. Efficiency is a critical aspect of a good algorithm. It refers to the optimal use of
computational resources, including time and memory. An efficient algorithm performs tasks
swiftly, saving both time and energy.
Simplicity. A good algorithm should be simple and straightforward, avoiding unnecessary
complexity. Simplicity facilitates easier understanding, implementation, and maintenance,
making the algorithm more user-friendly.
Flexibility. Flexibility is the ability of an algorithm to adapt to changes and varying conditions.
A flexible algorithm can accommodate different inputs and adjust to modifications without
compromising its performance.
Robustness. Robustness refers to the algorithm's ability to handle errors gracefully. A robust
algorithm can manage unexpected inputs or conditions without crashing, providing stable and
reliable performance.
Stability. Stability is crucial; it ensures that the algorithm performs reliably and consistently
under various conditions, maintaining its accuracy and reliability over time, even with varied
inputs.
Maintainability. Maintainability is about how easily an algorithm can be updated or modified.
A maintainable algorithm allows for smooth updates and alterations, ensuring it remains up-to-
date and functional over time.
Documentation. Good algorithms come with comprehensive documentation that outlines how
the algorithm works, its limitations, and how to use it effectively. Well-documented algorithms
are easier to use and integrate into different systems.
Security. In the current digital age, security is a paramount concern. A good algorithm should be
designed with security in mind, ensuring that it protects sensitive data and resists attacks from
malicious entities.
Examples include GPS navigation algorithms, online shopping recommendation algorithms, and
social media content curation algorithms.
Fortran went through many revisions over the years. FORTRAN II came out in 1958 and
FORTRAN IV in 1962 (I am not sure what happened to III). FORTRAN IV was renamed
FORTRAN 66 when it became an ANSI standard in 1966. The next major update came in
1977 with FORTRAN 77. This is the version that has been used for the last 20 or so years and is
still used by the hard-core, crazy engineers who resist change and enjoy difficult programming.
Fortran 90 was developed in 1991 and addresses most of the problems with FORTRAN 77,
incorporates most of the new features of languages developed in newer programming languages,
attempts to phase out some archaic programming features from previous versions by offering
more direct commands, and abandons the tradition of writing Fortran in all upper case. Fortran
90 is a big improvement. Fortran 95 was an update in 1997 that offered a few new features and
makes obsolete those awkward options that 90 was attempting to phase out. Although developed
early in the 90's, compilers have only recently become available for widespread use of Fortran
90. The resistance of many to the newer version and the simple and versatile (even though less
powerful) features of many new computer languages made Fortran/FORTRAN fall out of favor
with many during the 90's. Now that Fortan 90/95 compilers are readily available, lucky
students like you will be able to have all the advantages of Fortran compilers with much simpler
programming syntax.
Also, a newer version of Fortran is in development that will allow more modern features like
object oriented programming and will be able to run more efficiently when used for parallel
computing.
Probably many of you are wondering why you need to learn Fortran with all the
seemingly more simple alternatives, such as MATLAB (in fact, this is hardly an
alternative. Fortran is much more powerful than MATLAB. A better argument may
be C++ v/s Fortran 90). In fact many universities have dropped Fortran from their
Chemical Engineering curriculum. The first reason this occurred was the resitance in
the engineering community to moving from FORTRAN 77 to Fortran 90. While many
languages were being created and modified for simplicity and for use in specific
applications, Fortran lagged behind. However, Fortran remained throughout this time
as the superior language for numerical, scientific, and engineering applications. With
the improvements in Fortran from version 77 to 90, it remains the language of choice
for computational science. Fortran 90 is more powerful than C++ in the areas of
numerical robustness, data parallelism, data abstraction, and functional programming.
C++ leads only in object-oriented programming, which the next version of Fortran
hopes to address.
When writing code in UNIX you can use any of the text editors (emacs, vi & pico for
instance) and write the program. When complete, the program must be saved with a
filename and an extension .f90, for instance if you write a program, with
name my_code, you have to save that file as my_code.f90. This is important
as .f implies fixed format file, while Fortran 90 files (.f90 files) are free format files.
An alternative is using Sun Workshop. Those of you who like windows type
applications with a nice GUI this is the one for you. I am a novice in using Workshop
and prefer the old UNIX way; so, I can offer little advice. It allows you to creat
programs, debug them,and build executables. This program is invoked in UNIX by
typing workshop &. More information can be found
at http://www.ictp.trieste.it/~manuals/programming/sun/. Found there are .html files
introducing workshop and also a Fortran handbook
(http://www.ictp.trieste.it/~manuals/programming/sun/fortran/user_guide/index.html) .
Practice: Open a text editor and write the following simple fortran program. Then save
this in a file named code.f90.
PROGRAM joke
WRITE(*,*) "Hello world!"
END
Once you have written your code you must compile it before running. Compiling translates
the written source code into machine language, or an object program. In order to compile in
UNIX, at the prompt type f90 followed by the program name (e.g % f90 progname.f90). This
should create an executable file named a.out. You should then be able to run the program by
typing a.out at the UNIX prompt (i.e % a.out, should run the program). Every time you compile
a program without the -o option (discussed in the next paragraph) a new a.out file will be created
and if a previous a.out file exists it will be overwritten.
Many options are available when compiling code and some of these will be discussed later in
the notes. One option (-o) allows you to define the name for your executable file. You do not
have to use the default executable named a.out. In this way you can compile a program once and
have the executable available whenever it is needed. Also, in more advanced cases, two or more
executables will need to be linked. In these cases, you must have different names for all
executables. For example:
This command compiles the program progname.f90 and creates an executable file
named progname. The typical convention is to name your executable file with the same name as
your program, but without an extension.
Again, compiling and running in Sun Workshop will be different. Refer to the manuals if
interested.
Although you probably have never seen FORTRAN 77, I will give you a brief summary of some
of the changes and obsolete features. Keep in mind that Fortran 90 is upwardly compatible with
FORTRAN 77, meaning all features present in FORTRAN 77 are present in Fortran 90. Some
FORTRAN 77 features have been designated obsolete and should never be used because of
better alternatives. The features deemed obsolete in Fortran 90 are not available in Fortran 95
(they are actually obsolete).
The first, and most obvious, change from FORTRAN 77 to Fortran 90 is the conversion from
fixed source code to free source. In fixed source form (which dates back to punch cards)
statement labels had to appear in the first five columns, column six was used for a continuation
indicator, and statements appeared on lines 7-72. In Fortran 90 free source form means you are
free to type comments and commands wherever you want. An exclamation point, ! (also known
as a "bang"), is used to indicate a comment statement in Fortran 90.
Obsolete features include the arithmetic IF statement, ASSIGN and GO TO statements. Avoid
use of these features in your code. In fact, don't learn them.
There is some basic information that everyone should know when beginning to use Fortran. First
of all, Fortran 90 is case insensitive, meaning A is the same as a, WriTe is the same as WRITe,
etc.. However, when programming it is common (i.e. good) practice to use uppercase when
writing Fortran keywords (i.e. WRITE, REAL, PARAMETER, etc.). Also, you should always
be consistent when using upper of lowercase. It makes your code more readable and less
confusing to others (like the graders) when deciphering your code.
Fortran 90 is free source form, unlike FORTRAN 77 which was fixed source. Fixed form, which
dates back to the use of punch cards, meant that statement labels had to appear in the first five
columns, column six was used for a continuation indicator, and statements appeared on lines 7-
72. In Fortran 90 free source form means you are free to type comments and commands
wherever you want. An exclamation point, ! (also known as a "bang"), is used to indicate a
comment statement. The comment statement follows the bang and continues to the end of the
line. Comment statements are ignored by the compiler, but are very important for documenting
your program. It is important to use comments frequently to clarify the procedures intended in a
program. Comments can be of great assistance to debugging and revising computer programs.
A line of Fortran 90 code can have a maximum of 132 characters. An ampersand (&) is
placed at the end of a line to indicate that it continues on the next line. At most 39
continuation lines are permitted. If continuing a character string an ampersand must be placed at
the end of the first line and the beginning of the next. Only with character strings is the
ampersand needed at the start of the next line.
Example:
PROGRAM test
! This line is a comment
! Comments are ignored by the compiler
a = 3.0 + 4.0 + &
13.0
! After execution variable a will have the value 20.0
PRINT* a
END