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

COMPUTER PROGRAMMING

Chapter One
1.1 Introduction to programming
Computer is an electronic device that:
 accepts data
 performs computations
 And makes logical decisions according to instructions that have been given to
it.
 Then produces meaningful information in a form that is useful to the user.
They have been deployed to solve different real life problems, from the simplest game
playing up to the complex nuclear energy production.
In order to solve a given problem, computers must be given the correct instruction about
how they can solve it. The terms computer programs, software programs, or just
programs are the instructions that tells the computer what to do.
Computer requires programs to function, and a computer programs does nothing unless its
instructions are executed by a CPU.
What is 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 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 programming languages.
What is a programming language?
 Programming language is an artificial language that can be used to control the
behaviour of a machine; particularly a computer. It’s a set of different category of
written symbols that instruct computer hardware to perform specified operations
required by the designer (or programmer).
 Programming languages, like natural language (such as English, Afaan Oromo,
Amharic), are defined by syntactic and semantic rules which describe their structure
and meaning respectively.
The syntax of a language (Programming language) describes the possible
combinations of symbols that form a syntactically correct program. It’s all about the
grammar of the language.
 The meaning given to a combination of symbols is handled by semantics. In general,
programming languages allow humans to communicate instructions to machines.
 The main purpose of programming languages is to provide instructions to a computer.
As such, programming languages differ from most other forms of human expression in
that they require a greater degree of precision and completeness.
When using a natural language to communicate with other people, human authors and
speakers can be ambiguous and make small errors, and still expect their intent to be
COMPUTER PROGRAMMING

understood. However, computers do exactly what they are told to do, and cannot understand
the code the programmer "intended" to write.
 So computers need to be instructed to perform all the tasks. The combination of the
language definition, the program, and the program's inputs must fully specify the
external behaviour that occurs when the program is executed. Computer languages
have relatively few, exactly defined, rules for composition of programs, and strictly
controlled vocabularies in which unknown words must be defined before they can be
used

Why do we need to learn computer programming?


 Computer programming is critical if one wants to know how to make the computer
perform a task. Most users of a computer only use the available applications on the
computer. These applications are produced by computer programmers. Thus if
someone is interested to make such kind of applications, he/she needs to learn how to
talk to the computer, which is learning computer programming.
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.
Available programming languages come in a variety of forms and types. Thousands Of
different programming languages have been developed, used, and discarded.
Programming languages can be divided in to two major categories: low-level and high-level
languages.
a. Low-level languages
 Computers only understand one language and that is binary language or the language
of 1s and 0s.
 Binary language is also known as machine language, one of low-level Languages. In
the initial years of computer programming, all the instructions were given in binary
form. Although the computer easily understood these programs, it proved too difficult
for a normal human being to remember all the instructions in the form of 0s and 1s.
 Therefore, computers remained mystery to a common person until other languages such
as assembly language was developed, which were easier to learn and understand.
COMPUTER PROGRAMMING

Assembly language correspondences symbolic instructions and executable machine codes


and was created to use letters (called mnemonics) to each machine language instructions to
make it easier to remember or write. For example: ADD A, B – adds two numbers in memory
location A and B
Assembly language is nothing more than a symbolic representation of machine code, which
allows symbolic designation of memory locations. However, no matter how close assembly
language is to machine code, computers still cannot understand it.
 The assembly language must be translated to machine code by a separate program
called assembler. The machine instruction created by the assembler from the original
program (source code) is called object code. Thus assembly languages are unique to a
specific computer (machine). Assemblers are written for each unique machine
language.

b. High-level languages
Although programming in assembly language is not as difficult and error prone as stringing
together ones and zeros, it is slow and cumbersome. In addition it is hardware specific. The
lack of portability between different computers led to the development of high-level
languages so called because they permitted a programmer to ignore many low-level details
of the computer's hardware. Further, it was recognized that the closer the syntax, rules, and
mnemonics of the programming language could be to "natural language" the less likely it
became that the programmer would inadvertently introduce errors (called "bugs") into the
program.
 High-level languages are more English-like and, therefore, make it easier for
programmers to "think" in the programming language.
High-level languages also require translation to machine language before execution. This
translation is accomplished by either a compiler or an interpreter. Compilers translate the
entire source code program before execution. Interpreters translate source code programs one
line at a time. Interpreters are more interactive than compilers.
 FORTRAN (FORmula TRANslator), BASIC (Bingers All Purpose Symbolic
Instruction Code), PASCAL, C, C++, Java are some examples of high-level
languages.
Exercise: Compare and contrast low level languages with high level languages?
 The question of which language is best is one that consumes a lot of time and energy
among computer professionals. Every language has its strengths and weaknesses. For
example, FORTRAN is a particularly good language for processing numerical data,
but it does not lend itself very well to organizing large programs. Pascal is very good
for writing well-structured and readable programs, but it is not as flexible as the C
programming language. C++ embodies powerful object-oriented features. As might
be expected in a dynamic and evolving field, there is no single standard for
COMPUTER PROGRAMMING

classifying programming languages. Another most fundamental ways programming


languages are characterized (categorized) is by programming paradigm. A
programming paradigm provides the programmer's view of code execution. The most
influential paradigms are examined in the next three sections, in approximate
chronological order.
Major Programming Paradigms (reading assignment )
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.
1.2. Problem solving process and software engineering
What is Software Engineering?
 Software engineering is the profession that creates and maintains software
applications by applying technologies and practices from computer science, project
management, engineering, application domain and other fields.
 The method used in solving problems in computer science and/or information systems
is called the software development life cycle (SDLC).
The software development life cycle has the following components.
 Preliminary investigation
 Analysis
 Design
 Implementation
 Testing and maintenance
Problem Solving
 Problem solving is the process of transforming the description of a problem into the
solution by using our knowledge of the problem domain and by relying on our ability
to select and use appropriate problem-solving strategies, techniques, and tools.
 A problem is an undesirable situation that prevents the organization from fully
achieving its purpose, goals and objectives. Or problem can also be defined as the
gap between the existing and the desired situation where problem solving will try to
fill this gap.
Example1: If you are watching a news channel on your TV and you w ant to change it to a sports
channel, you need to do something i.e. move to that channel by pressing that channel number on your
remote. This is a kind of problem solving.
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.
COMPUTER PROGRAMMING

 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.
Basic Program development tips
 Reliable: the program should always do what it is expected to do and handle all types
of exception.
 Maintainable: the program should be in a way that it could be modified and
upgraded when the need arises.
 Portable: It needs to be possible to adapt the software written for one type of
computer to another with minimum modification (run on different hardware’s).
 Efficient: the program should be designed to make optimal use of time, space and
other resources of the computer.
Before a program is written, the programmer must clearly understand what data are to be
used to get the desired result, and the procedure to be used to produce the result. The
procedure, or solution selected is referred to as an algorithm.
What is an algorithm?
 Algorithm can be defined as: “A sequence of activities to be processed for getting
desired output from a given input.”
Webopedia defines an algorithm as:
 “A formula or set of steps for solving a particular problem. To be an algorithm, a set
of rules must be unambiguous and have a clear stopping point”.
 There may be more than one way to solve a problem, so there may be more than one
algorithm for a problem.
Now, if we take definition of algorithm as: “A sequence of activities to be processed for
getting desired output from a given input.” Then we can say that:
1. Getting specified output is essential after algorithm is executed.
2. One will get output only if algorithm stops after finite time.
3. Activities in an algorithm to be clearly defined in other words for it to be
unambiguous.
Before writing an algorithm for a problem, one should find out
 what is/are the inputs to the algorithm and
 what is/are expected output after running the algorithm.
There are two commonly used techniques (tools) to implement an algorithm. These are :
 flowcharts and
 Pseudo-code.
Modeling a programs logic using pseudo-code
 Pseudo-code is writing an algorithm as close as possible to computing languages.
COMPUTER PROGRAMMING

 It consists of natural language-like statements that precisely describe the steps of


an algorithm or program
 It is very important and recommended algorithm to represent large problem
 It is similar as writing real code (in your programming language) without needing
to worry about program syntax.
 There is no standard for Pseudocode syntax, as a program in pseudocode is not an
executable program.
 As the name suggests, pseudocode generally does not actually obey the synatx
rules of any particular language; there is no systematic standard form, although
any particular writer will generally borrow the appearance of a particular
language.

 Some rules in writing pseudo-code:


1. Write only one statement per line ( meaning statements with the same action)
2. Capitalize initial keyword ; like READ, WRITE, IF, ELSE, ENDIF, WHILE,
ENDWHILE, REPEAT, UNTIL
3. Indent to show hierarchy :
We will use a particular indentation pattern in each of the design structures:
i. SEQUENCE: keep statements that are “stacked” in sequence all starting in the same
column.
ii. SELECTION: indent the statements that fall inside the selection structure, but not
the keywords that form the selection
iii. LOOPING: indent the statements that fall inside the loop, but not the keywords
that form the loop
Keep statements language independent: Write in whatever language you are most
comfortable with.
The pseudo code and flowchart, classification to the three types of control structures.
They are:
1. Sequence
2. Branching (Selection)
3. Loop (Repetition)
COMPUTER PROGRAMMING

These three control structures are sufficient for all purposes.


The sequence is exemplified by:-
 Sequence of statements place one after the other – the one above or before another
gets executed first. In flowcharts, sequence of statements is usually contained in the
rectangular process box.
1.3.1 Example of SEQUENCE
Problem 1: Find the area of a Circle of radius r.
Inputs to the algorithm:
Radius r of the Circle.
Expected output:
Area of the Circle
Algorithm:
Step1 Start
Step2: READ\input the Radius r of the Circle
Step3: Area ←PI*r*r // calculation of area
Step4: Print Area
Step5: Stop
Problem2: Write an algorithm to read two numbers and find their sum.
Inputs to the algorithm:
First num1.
Second num2.
Expected output:
Sum of the two numbers.
Algorithm:
Step1: Start
Step2: Read\input the first num1.
Step3: Read\input the second num2.
Step4: Sum ←num1+num2 // calculation of sum
Step5: Print Sum
Step6: End
COMPUTER PROGRAMMING

Problem 3: Convert temperature Fahrenheit to Celsius


Inputs to the algorithm:
Temperature in Fahrenheit
Expected output:
Temperature in Celsius
Algorithm:
Step1: Start
Step 2: Read Temperature in Fahrenheit F
Step 3: C ←5/9*(F32)
Step 4: Print Temperature in Celsius: C
Step5: End

If the condition is true, one of the two branches is explored; if the condition is false, the
other alternative is taken. This is usually represented by the ‘if-then’ construct in pseudo-
codes and programs. In flowcharts, this is represented by the diamond-shaped decision
box. This structure is also known as the selection structure.
Problem1: write algorithm to find the greater number between two numbers
Step1: Start
Step2: Read/input A and B
Step3: If A greater than B then C=A
Step4: if B greater than A then C=B
Step5: Print C
Step6: End
Problem3: A algorithm to find the largest value of any three numbers.
Step1: Start
Step2: Read/input A, B and C
Step3: If (A>=B) and (A>=C) then Max=A
Step4: If (B>=A) and (B>=C) then Max=B
Step5: If (C>=A) and (C>=B) then Max=C
Step6: Print Max
Step7: End
COMPUTER PROGRAMMING

The loop allows a statement or a sequence of statements to be repeatedly executed


based on some loop condition. It is represented by the ‘while’ and ‘for’
A trip around the loop is known as iteration. You must ensure that the condition for the
termination of the looping must be satisfied after some finite number of iterations, otherwise
it ends up as an infinite loop, a common mistake made by inexperienced programmers.
The loop is also known as the repetition structure.
Examples:
Problem1: An algorithm to calculate even numbers between 0 and 99
1. Start
2. I ← 0
3. print I
4. I ← I+2
5. If (I <=98) then go to line 3
6. End
Problem2: Design an algorithm which gets a natural value, n, as its input and calculates
odd numbers equal or less than n. Then write them in the standard Output:
1. Start
2. Read n
3. I ← 1
4. Write I
5. I ← I + 2
6. If (I <= n) then go to line 4
7. End
COMPUTER PROGRAMMING

Problem3: Design an algorithm which generates even numbers between 1000 and 2000
and then prints them in the standard output. It should also print total sum:
1. Start
2. I ← 1000 and S ← 0
3. Write I
4. S ← S + I
5. I ← I + 2
6. If (I <= 2000) then go to line 3
else go to line 7
7. Write S
8. End
Problem4: Design an algorithm with a natural number, n, as its input which calculates
the following formula and writes the result in the standard output:
S = ½ + ¼ + … +1/n
1. Start
2. Read n
3. I ← 2 and S ← 0
4. S= S + 1/I
5. I ← I + 2
6. If (I <= n) then go to line 4 else write S in standard output
7. End
1.3.2 Properties of algorithm
Donald Ervin Knuth has given a list of five properties for a, algorithm, these properties are:
1) Finiteness: An algorithm must always terminate after a finite number of steps. It means
after every step one reach closer to solution of the problem and after a finite number of steps
algorithm reaches to an end point.
2) Definiteness: Each step of an algorithm must be precisely defined. It is done by well
thought actions to be performed at each step of the algorithm. Also the actions are defined
unambiguously for each activity in the algorithm.
3) Input: Any operation you perform need some beginning value/quantities associated with
different activities in the operation. So the value/quantities are given to the algorithm before
it begins.
COMPUTER PROGRAMMING

4) Output: One always expects output/result (expected value/quantities) in terms of output


from an algorithm. The result may be obtained at different stages of the algorithm. If some
result is from the intermediate stage of the operation then it is known as intermediate result
and result obtained at the end of algorithm is known as end result. The output is expected
value/quantities always have a specified relation to the inputs
5) Effectiveness: Algorithms to be developed/written using basic operations. Actually
operations should be basic, so that even they can in principle be done exactly and in a finite
amount of time by a person, by using paper and pencil only.

Flow chart
 The flowchart is a diagram which visually presents the flow of data through
processing systems.
 This means by seeing a flow chart one can know the operations performed and the
sequence of these operations in a system.
 Algorithms are nothing but sequence of steps for solving problems.
 So a flow chart can be used for representing an algorithm. A flowchart, will describe
the operations (and in what sequence) are required to solve a given problem.
For example suppose you are going for a picnic with your friends then you plan for the
activities you will do there. If you have a plan of activities then you know clearly when you
will do what activity.
Similarly when you have a problem to solve using computer or in other word you need to
write a computer program for a problem then it will be good to draw a flowchart prior to
writing a computer program. Flowchart is drawn according to defined rules.
COMPUTER PROGRAMMING

Flowchart Symbols

Some examples of Flowcharts


Now, we will discuss some examples on flowcharting. These examples will help in proper
understanding of flowcharting technique. This will help you in program development process
in next unit of this block.
COMPUTER PROGRAMMING

Problem1: Find the area of a circle of radius r.


COMPUTER PROGRAMMING

Problem 2: Convert temperature Fahrenheit to Celsius


COMPUTER PROGRAMMING

Problem 3: - Draw flow chart of an algorithm that add two numbers and display their result.

Problem4: Algorithm for find the greater number between two numbers.
COMPUTER PROGRAMMING

Problem 5: Flowchart to calculate the average from 25 exam scores.

You might also like