Chapter 1 - Introduction
Chapter 1 - Introduction
Chapter 1 - Introduction
1. Introduction
Think about some of the different ways that people use computers. In school, students use
computers for tasks such as writing papers, searching for articles, sending emails, and participating
in online classes. At work, people use computers to analyze data, make presentations, conduct
business transactions, communicate with customers and coworkers, control machines in
manufacturing facilities, and do many other things. At home, people use computers for tasks such
as paying bills, shopping online, communicating with friends and family and playing computer
games. And don’t forget that cell phones, iPods, car navigation systems, and many other devices
are computers too. The uses of computers are almost limitless in our everyday lives. Computers
can do such a wide variety of things because they can be programmed. This means that computers
are not designed to do just one job, but to do any job that their programs tell them to do. A
computer program (also referred to as a program) is a set of instructions that a computer follows
to perform a task.
A computer program (also known as source code) is often written by professionals known as
Computer Programmers (simply programmers). Source code is written in one of the
programming languages. A programming language is an artificial language that can be used to
control the behavior of a machine, particularly a computer. Programming languages, like natural
1
language (such as Amharic), are defined by syntactic and semantic rules which describe their
structure and meaning respectively. The syntax of a language describes the possible combinations
of symbols that form a syntactically correct program. The meaning given to a combination of
symbols is handled by semantics.
Computer programming is an exciting and rewarding career. Today, you will find programmers’
work used in business, medicine, government, law enforcement, agriculture, academics,
entertainment, and many other fields.
Before starting any part of the solution, we have to be introduced to the problem. First, we must
understand thoroughly what the problem is. This should lead us to complete the specifications of
the problem to be addressed.
2
There are various methods to find these specifications, like interviews, observations, or for simpler
problems in written form, read the requirements carefully
This and the next steps of the problem-solving process are computer independent works.
➢ Divide the original problem into several sub-problems. These sub-problems, being
necessarily smaller than the original problem, are easier to solve and their solution will be
the components of our final solution. This method, by which a complex problem is divided
into smaller and easier sub-problems is called “divide and conquer”.
➢ Associating each task with a precise method to accomplish it, which consists of a sequence
of well-defined steps that, when carried out, perform the corresponding task. This sequence
of steps is called an Algorithm.
Algorithms are usually developed in pseudo-code or using a flowchart. Pseudo code is a language
used to describe the manipulations to be performed on data. This language is a simple mixture of
natural language and mathematical notations and it is independent of programming language.
Example: the pseudo code to calculate tax deduction of employees based on the following
requirement: For salary more than 500-birr, tax is 5% of the salary, and for salary less than 500-
birr, tax is 3% of the salary. The pseudo-code for this requirement may look like the following:
Input salary
Else
Output tax
3
Planning is sketching the step-by-step activities that lead to the solution. This is formulating a
problem in terms of the steps to its solution.
Pseudo-code or flow chart algorithms cannot be executed (performed) directly by the computer.
They must first be translated into a programming language, a process referred to as coding.
It is expressing the solutions (in the previous step) in one of the various programming languages.
There are many high-level programming languages like BASIC, COBOL, Pascal, FORTRAN, C,
C++, VB, etc. To get our program to work, we write it based on the syntax rules of the
programming language.
Syntax rule: a correct way of writing or expressing commands that direct the control of the
computer system.
It is very rare that a program can be written correctly the first time. Most programmers get used to
the idea that there are errors in their newly written programs, these errors are detected during
testing the program, and appropriate revision must be made and the tests return.
5. Documentation
This activity starts with the first step of defining the problem and continues. It is compiling related
documents throughout the lifetime of the program development. The documentation must include
• The problems encountered during the writing & testing of the program
• Users’ manual that explains how to use the program, how to prepare the input data, and
how to interpret the program’s results.
Though there can be many solutions (various programs) for one problem (requirement), we should
select the best one based on its reliability, maintainability, portability, and efficiency (time &
space).
An algorithm is a finite set of well-defined rules (statements) for the solution of a problem in a
finite number of steps. To design an algorithm for a problem first we break down the problem into
simpler and more manageable tasks. For one problem there may be a lot of algorithms that help to
solve, but the algorithms that we select must be powerful, easy to maintain, and efficient (doesn’t
take too much space and time).
The most common ways to represent algorithms include pseudo code and flowcharts.
Pseudocode consists of natural language-like statements that precisely describe the steps of an
algorithm or program. It uses statements that describe actions and focuses on the logic of the
algorithm or program. Pseudocodes avoid language-specific elements and are written at a level
where the desired programming code can be generated almost automatically from each statement.
A flowchart is a graphical tool that diagrammatically depicts the steps and structure of an
algorithm. A flow chart consists of an ordered set of standard symbols (mostly geometrical
shapes), which represent operations, data flow, or decisions.
5
Major flowchart symbols
• All symbols of the flowchart are connected by flow lines (note arrows, not lines)
• Flowlines enter the top of the symbol and exit out the bottom, except for the decision
symbol, which can have flow lines exiting from the bottom or the sides
• Flowcharts are drawn so flow generally goes from top to bottom
• The beginning and the end of the flowchart are indicated using the Terminal symbol
Example: the flowchart to calculate tax deduction of employees based on the following requirement:
6
For salary more than 500-birr, tax is 5% of the salary, and for salary less than 500-birr, tax is 3% of
the salary. The flowchart for this requirement may look like the following:
Start
Read salary
[No] [Yes]
Salary
>=500
tax = sal *0.03 tax = sal * 0.05
Display tax
end
All algorithms are constructed using three control structures or construct. These are:
• Sequence structure is the construct where one statement is executed after another
• Selection structure is the construct where statements can be executed or skipped depending
on whether a condition evaluates to TRUE or FALSE
• Repetition structure is the construct where statements can be executed repeatedly until a
condition evaluates to TRUE or FALSE
7
Example: Flowchart to find the factorial of a given number.
Answer: An algorithm is a sequence of logical steps expressed informally that solves a given
problem. A program is a list of formal instructions that the computer must follow in order to solve
a problem. It is the solution to a problem. A program is written in a certain programming language
whereas an algorithm will be written in English (or any other natural language) like pseudo-
language or flowchart.
Exercise
1. Write an algorithm that calculates and displays the area and perimeter of a rectangle. The
width and the length are given by the user. Present the algorithm using both pseudocode
and a flowchart.
8
2. Write an algorithm that calculates the class average of n students for math's score where n
is supplied by the user. Present the algorithm using both pseudocode and a flowchart.
3. It has been decided that a bonus of 12% of gross salary is to be given to each employee in
an organization. It was also agreed that if an employee has worked for more than 13 years,
she/he is to receive an additional amount of Birr 350.00. Write an algorithm that calculates
and displays the bonus and the net salary. Present the algorithm using both pseudocode and
a flowchart.
Since being first conceptualized, programming has gone through an evolution, which is derived
from the need to make program writing and maintenance simpler. There are three major levels
or types of programming languages:
1. Machine Language
2. Assembly Language
3. High-Level Languages
Machine Language
Assembly Language
One step above machine language, assembly language, is a symbolic representation of machine
language. Each machine language instruction is represented by a symbol or abbreviation in
assembly language. A program written in assembly language should be translated to machine
language by a translator known as Assembler before it is executed by the machine. Machine
language and assembly language together are referred to as low-level languages.
Assembly language is relatively easier than machine language in the sense that it uses easy-to-
remember symbols but it is still difficult to remember all the symbols and abbreviations and it is
still prone to errors.
High-Level Languages
Most programming languages you’ve heard of are high-level languages. High-level languages
resemble human languages in many ways. They are designed to be easy for human beings to write
programs in and to be easy for human beings to read. A high-level language, such as C++, contains
instructions that are much more complicated than the simple instructions a computer’s processor
(CPU) is capable of following.
MOV A, 47 A=47
ADD A, B A= A + B
10
program must be translated into machine language before the computer can understand and follow
the program.
Translators
Programs that translate source code to machine code are called translators. There are two types of
translators: interpreters and compilers.
An interpreter is a program that converts each high-level language statement into machine
language when needed to be executed immediately, statement by statement. A compiler converts
the entire program (source code) of a high-level language into machine code program called the
object code or object program before the computer executes these programs.
High-level languages which use compilers such as C are called compiled languages. Those that
use interpreters, such as Python, are called interpreted languages. Compiled languages are
generally faster and take less memory than interpreted languages. C++ is a compiled language.
11
Exercise
12