Chapter 1 - Introduction

Download as pdf or txt
Download as pdf or txt
You are on page 1of 12

Chapter One

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.

Programs are commonly referred to as software. Software is essential to a computer because it


controls everything the computer does. Computers require programs to function, and a computer
program does nothing unless its instructions are executed by a CPU (the central processing unit of
the computer). All of the software that we use to make our computers useful is created by
individuals working as programmers or software developers. A programmer, or software
developer, is a person with the training and skills necessary to design, create, and test computer
programs. Computer programming (often shortened to programming or coding) is the process
of writing, testing, debugging/troubleshooting, and maintaining the source code of computer
programs. Writing computer programs means writing instructions that will make the computer
follow and run a program based on those instructions.

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.

1.1. Problem Solving


Despite common misconceptions, programming is more of a problem-solving activity than it is
about writing code (although coding is an integral part of it). Programming is problem-solving
using computers. When solving solutions, we may identify different solutions. But all correct
solutions must involve the following formal problem-solving steps.

1. Defining the problem


2. Planning the solution
3. Coding the program
4. Testing the program
5. Documentation

1. Defining the problem:

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.

➢ What part of the problem is going to be solved?

➢ What input data is required to solve the problem?

➢ What output data (result) is expected?

➢ What procedures are needed to achieve the result?

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.

2. Planning the Solution: -

This step involves the following tasks:

➢ 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

If salary > 500 birr

set tax to salary * 0.05

Else

set tax to salary * 0.03

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.

3. Coding the program

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.

4. Testing the program

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.

The process of coding and testing is called implementation.

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

• Problem (requirement) definition

• Explanations of all the steps of the algorithms (the planning)

• The problems encountered during the writing & testing of the program

• Explanations of some important or ambiguous parts of the program code (comments)


4
• A printed copy 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).

1.2. Algorithm Development

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

Terminal indicates start/end of a program

Input/output represents data input and data output of a program

Process represents actions (operations) or group of


operations (process)

Decision represents decision to be made. It contains the


conditions that determines which output path (or
arrow) will be followed out of them. Usually, two
output paths based on the result of the condition
(True /False)

Flow line indicates the direction of logical flow (a path from


one operation to another)

General rules for flowcharts:

• 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

Sometimes there is a situation in which it is necessary to execute a group of statements repeatedly


until some condition is satisfied. This situation is called a loop. Loop is a sequence of instructions,
which is repeated until some specific condition occurs.

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.

Note: The factorial of a given number n is: n x [n-1] x [n-2] x … x 1

Question: What is the difference between an algorithm and a program?

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.

1.3. Programming Languages


There are about 700 programming languages available for use today. Some sources that only list
notable languages still count up to an impressive 245 languages. Another list called HOPL, which
claims to include every programming language to ever exist, puts the total number of programming
languages at 8,945. BASIC, FORTRAN, COBOL, PASCAL, C, C++, Java, Python, JavaScript,
PHP, and R are just some of the programming languages we can mention.

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

Machine language is a programming language that is written in binary language (consisting of a


sequence of 1’s and 0’s). Programs written in this language “speak” the language of the computer.
The first programs were written in machine language. Today’s CPUs still only understand machine
language. Programs written in other types of languages still need to be converted to this format for
the computer to be able to understand and execute them. Although programs written in this
language have the fastest execution time, the language itself is very hard to learn, write and debug.
It is very prone to errors. It is also machine dependent. Meaning you have to know the hardware
architecture of the machine that you are planning to run the program on for the program to work.
9
In addition, because of the machine dependency, a program you have written for one machine
might not work on another.

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 instructions vs Machine language instructions

MOV A, 47 1010 1111

ADD A, 26 01101011 00011010

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.

Assembly language instructions vs High level language instructions

MOV A, 47 A=47

ADD A, B A= A + B

A human-readable program written in a high-level language or assembly language that is not


directly readable by the machine is commonly referred to as source code. Any high-level language

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

1. What is the difference between a machine-language program and a high-level language


program?
2. What is the role of a compiler?
3. What is a source program?
4. What is an object program?

12

You might also like