0% found this document useful (0 votes)
4 views

Chapter Four

fd4

Uploaded by

habentsegay30
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Chapter Four

fd4

Uploaded by

habentsegay30
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

Aksum University

College of Engineering and Technology

Chapter Four
Introduction to Programming

Programming is a skill that can be acquired by a computer professional that gives him/her the knowledge of
making the computer perform the required operation or task.
A program is a detailed set of instructions written in a programming language that directs the computer to
solve a problem.
Programming Language: is a set different category of written symbols that instruct computer hardware to
perform specified operations required by the designer.
Syntax. The set of rules determine how the programmer can make the computer hardware to perform a
specific operation.
What skills do we need to be a programmer?
 For someone to be a programmer, in addition to basic skills in computer, needs to have the following
major skills.
 Programming Language Skill: knowing one or more programming language to talk to the computer
and instruct the machine to perform a task.
 Problem Solving Skill: skills on how to solve real world problem and represent the solution in
understandable format.
 Algorithm Development: skill of coming up with sequence of simple and human understandable set
of instructions showing the step of solving the problem. Those set of steps should not be dependent on
any programming language or machine.

Types of Languages
Programming languages can generally be classified as low-level languages and high-level languages. Under
low-level language, we have machine languages and assembly languages.
1) Machine language: - There is the only language that the computer understands directly. A machine
language is a set of machine instructions which consists of zeros and one. A machine instruction contains
two parts and operation code and an address. The operation code tells the microprocessor system what
operation it should perform, add, transfer, compare, or move data to output device, etc. The address
identifies the location (memory, register) holding the required operands that is, the data to be operated

DEPARTMENT OF COMPUTING TECHNOLOGY 1


Aksum University
College of Engineering and Technology

upon. The address part may contain one, two or more addresses that is, there may be one (or single
address, two (double) address, and three (or triple) address instructions.

Advantage
 Machine Languages make efficient use of memory
 Instructions of a machine language program are immediately executable.
 Machine language instructions can be used to manipulate the individual bits in a byte of
computer storage.
Disadvantage
 Difficult to learn
 Prone to errors
 Difficult to correct or modify errors
 Not portable
 Machine Languages are machine dependent
o Machine languages require a high level of programming skill

2) Assembly Language: - In machine language we have seen that the operation code and the address are
represented as a binary sequence but it is difficult for the programmer to write a big program using binary
sequence and it is difficult to debug an error from such program so instead of representing the Operation
code and the adders as a binary sequence we can represent them in mnemonics (symbols). An Assembly
language is a programming language which uses mnemonics to write a program. It is machine dependent.

Advantage
 Writing in Assembly Language saves time and reduces the need for detail.
 Fewer errors are made and those made are easier to find. These programs are easier to
modify.
 Easier to learn
 Easier to locate correct errors
Disadvantage
 Coding is time consuming
 It is also machine-oriented program. The programmers must know the machine
characteristics and requirements
 The program size is big like machine codes

DEPARTMENT OF COMPUTING TECHNOLOGY 2


Aksum University
College of Engineering and Technology

 It is usually unique to a particular computer series


 It is not directly executable by the computer. It needs assembler to translate it into
machine codes

3) High-level language: -We have seen that writing a program in low-level languages is easier and simple
compare to machine languages. But still Assembly language has its own drawback, which is machine
dependent. So we need another type of languages which are not machine-dependent and more flexible.
These languages are called high-level languages.

Advantage
 Easier to learn and write the codes
 Codes are written without any particular machine in mind
 Portable
 Small program size
 These languages may be used with different makes of computers with little
modifications.
 They are easier to learn.
 They require less time to write.

Disadvantage
 Require more time to run and more computer memory space
 Cannot access all components of the computer hardware such computer accessories. In
that case we have to use lower level languages
 They require additional time during translation
 They are less efficient compared to the machine code languages.

Language Translator
For a High-Level language to work on the computer, it must be translated into machine
language. There are two kinds of translators. Compilers and interpreters high level languages are called
either compiled languages or interpreted languages.

DEPARTMENT OF COMPUTING TECHNOLOGY 3


Aksum University
College of Engineering and Technology

In compiled languages, a translation program is run to convert the programmer’s entire high level
program, which is called the source code, in to a machine language code. This translation process is
called compilation. The machine language code is called the object code.
Examples of the most widely used compiled languages are COBOL, C, C++ and FORTRAN.

An interpreted language, a translation program converts each program statement into machine code
just before the program statement is to be executed. Translation and execution occur immediately one
after the other. That means, it translate one statement at a time.
Example of the most frequently used interpreted language is BASIC.

 Assembler: - is a program that translates assembly languages into machine code.


 Compiler: - is a program that translates a high level language into machine code. (example:
PASCAL, FORTRAN COBOL)
 Interpreter: - is a program that translates each instruction of high level language & executes the
instruction before translating the next instruction.

Major Programming Paradigms


The major land marks in the programming world are the different kinds of features or properties observed in
the development of programming languages. Among these the following are worth mentioning: Procedural,
Structured and Object Oriented Programming Paradigms.

Procedural Programming.
 Procedural programming is a programming paradigm based upon the concept of procedure call.
Procedural programming is often a better choice than simple sequential programming in many
situations which involve moderate complexity or which require significant ease of maintainability.
 Possible benefits: the ability to re-use the same code (function or procedure) at different places, an
easier way to keep track of program flow than a collection of “GO TO” or “JUMP” statements.

Structured Programming.
 Process of writing a program in small, independent parts. This makes it easier to control a program's
development and to design and test its individual component parts.

DEPARTMENT OF COMPUTING TECHNOLOGY 4


Aksum University
College of Engineering and Technology

 Structured programs are built up from units called modules, which normally correspond to single
procedures or functions.
 Can be seen as a subset or sub discipline of procedural programming. It is most famous for removing
or reducing reliance on the GO TO statement.

Object-Oriented Programming.
 The idea behind OOP is that, a computer program is composed of a collection of individual units, or
objects as opposed to traditional view in which a program is a list of instructions to the computer.
 Object-oriented programming is claimed to give more flexibility, easing changes to programs. The
OOP approach is often simpler to develop and maintain.

Problem Solving
Problem solving using computers consists of the following steps:
1. Requirement specification – understand exactly what the problem is, what is needed to solve it, what
the solution should provide, and if there are constraints and conditions.
2. Analysis – identify the inputs to the problem and outputs expected from the solution, their form, and
the media to be used. Any special constraints or conditions as well as the formulas or equations to be
used should also be identified.
3. Design – develop a series of steps to be performed in a specific logical order, i.e. develop an
algorithm for solving the problem.
4. Implementation – write the program using a programming language.
5. Testing and verification – check the correctness of the program by supplying different inputs and
seeing the output.
6. Documentation – prepare documents that explain how the program works and how to use it.

There are two approaches of problem solving:


Top down design: is a systematic approach based on the concept that the structure of the problem
should determine the structure of the solution and what should be done in lower level. This approach
will try to disintegrate a larger problem into more smaller and manageable problems to narrow the
problem domain.
Bottom up design: is the reverse process where the lowest level component are built first and the
system builds up from the bottom until the whole process is finally completed

DEPARTMENT OF COMPUTING TECHNOLOGY 5


Aksum University
College of Engineering and Technology

Program Algorithms
An algorithm is a procedure consisting of a finite number of precisely defined steps for solving a problem.
Each step in an algorithm must be an unambiguous instruction which, when written in a computer language,
can be executed by a computer. An algorithm must terminate whether or not the task is completed
successfully. Pseudo codes or flowcharts may be used to develop algorithms.
A pseudo code is a semiformal, English-like language with a limited vocabulary that can be used to design
and describe an algorithm. A pseudo code for an algorithm is ultimately implemented as a computer program.
The algorithm should be:
 Precise and unambiguous
 Simple
 Correct
 Efficient
Example
Consider the simple problem of calculating and printing the average value of a set of samples of air pollution.
The basic steps in this calculation are:
1. Add the data values
2. Divide the total by the number of data
3. Print the result
The first step includes repeated addition. The computer needs more detail on adding the values and counting
them. Here is a pseudo code description of the algorithm.
Total  0
Tally  0
For each sample
Total  Total + Sample
Tally  Tally + 1
End for
Average  Total/Tally
Print the average
End

DEPARTMENT OF COMPUTING TECHNOLOGY 6


Aksum University
College of Engineering and Technology

The following conventions are used for writing pseudo codes:


1. Summations and counters are initialized to zero
2. Assignment of values is shown by an arrow pointing to the left
3. The beginning and end of any decision is indicated, as well as the basis for making the decision.
4. The beginning and end of any repetition is indicated, as well as the basis for continuing and/or
stopping the repetition
5. Optional steps and repetitions are indented.
6. Either words or arithmetic operators may be used where arithmetic is involved.
7. The algorithm termination must be clearly indicated.

Flowcharts
Flow charts use symbols to represent each part of an algorithm. Here is a list of the symbols used in flow
charts.
1. Process box – rectangular symbol with one control line leading into it and one leading out of it. It may
be single instruction that computes, moves data from one memory location to another, carries out some
other type of data manipulation or a sequence of instructions which jointly implement a step in the
program.

Statement(s)

2. Input/output box – a parallelogram symbol with one control line leading into it and one leading out of
it.

Input/output

DEPARTMENT OF COMPUTING TECHNOLOGY 7


Aksum University
College of Engineering and Technology

3. Decision box – a diamond shaped symbol used for the comparison of quantities for equality. It
generates`either a true or false answer, which is the basis for decision.

False True
Decision

Execution follows either the path on the true branch or the path on the false branch, but not both paths.

4. Connector – a circle with two flow lines drawn into the circle and a single line drawn out of it. A
connector is also used when a flow chart is too large to fit on a single page or too complicated for all
lines to be drawn completely. In this case, the circle has only a single line in or a single line out and is
labeled to show which parts of the flowchart are connected.

A A

5. Flow lines – lines used to connect different flow chart symbols in the order of the logic and control
flow of programs. An arrow is used to indicate the direction of the logic flow.

DEPARTMENT OF COMPUTING TECHNOLOGY 8


Aksum University
College of Engineering and Technology

The algorithm developed earlier using pseudocodes can be represented using flow charts as follows.

Start

Total =0
Tally = 0

Get data values

Is data value No
found?

Yes

Total = Total + data


value
Tally = Tally + 1

Average = Total/Tally

Display average

Stop

DEPARTMENT OF COMPUTING TECHNOLOGY 9


Aksum University
College of Engineering and Technology

Example
Write an algorithm to select the largest and the smallest integers in a given list of values.
1. Method
Assume the first number is both the largest and and the smallest. Then, as each number is read, compare it
to the smallest and largest numbers already found. If the number is smaller than the previous smallest,
save it as the smallest. If it is larger than the previous largest, save as the largest. Repeat the process until
the numbers have been processed.

Start

2. Algorithm in pseudocode Input first N

Input the first number


MAX=N
Save it as
MIN=N
largest  first number
Smallest  first number
For each remaining number Input next N

Input the number


If number > largest then Was value NO
N found?
largest  number
If number < smallest then Yes
smallest  number N>MA
N<MIN MIN?N
End for MAX?N

Output largest and smallest


Stop MIN=N MAX=N

3. Algorithm in flow chart

Output MAX,
MIN

Stop

DEPARTMENT OF COMPUTING TECHNOLOGY 10


Aksum University
College of Engineering and Technology

Problems
Develop a flowchart for each problem below.
1. Receive a number and determine whether it is odd or even.
2. Obtain two numbers from the keyboard, and determine and display which (if either) is the larger of the
two numbers.
3. Receive 3 numbers and display them in ascending order from smallest to largest
4. Add the numbers from 1 to 100 and display the sum
5. Take an integer from the user and display the factorial of that number

DEPARTMENT OF COMPUTING TECHNOLOGY 11

You might also like