Computer Programming (CoSc1012 ) Chapter One: Introduction to programming
Chapter One
1.1. Introduction to Programming
❑ What is Computer?
✓ Computer 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.
❑ What are Computer programs?
✓ Computer programs are sets of instructions that control a computer’s processing of data. And they
are the instructions that tell the computer hardware what to do.
❑ What is Computer programming?
✓ Computer programming is the process of writing, testing, debugging / troubleshooting, and
maintaining the source code of computer programs.
✓ Computer programs (also known as source code) is often written by professionals known as
Computer Programmers
✓ A computer program usually consists of two elements:
1. Data – characteristics
2. Code – action
✓ Source code is written in one of programming languages
❑ What is Programming Language?
✓ Programming Language: is an artificial language that can be used to control the behavior of a
computer. It is defined by: -
1. Syntactic - describes the possible combinations of symbols that form a syntactically correct
program
2. Semantic - The meaning given to a combination of symbols computers do exactly what they
are told to do
✓ Programming languages can be divided in to two major categories: as a low-level and a high-
level language.
❑ Machine language: the term machine is short for computing machine (i.e., computer) and a machine
language is computer’s native language, sequence of zeroes and one’s different computers understand
different sequences. And it is hard for humans to understand.
Page 1 of 12 Computer Programming Lecture Note Compiled By: Department of Computer Science JKU
Computer Programming (CoSc1012 ) Chapter One: Introduction to programming
❑ Assembly language: mnemonics for machine language that is low level in which each instruction is
minimal. It is still hard for humans to understand.
❑ High-level languages: a language which is easily understandable for the user. Example of high level
programming languages are FORTRAN, Pascal, BASIC, C, C++, Java, COBOL, etc.
Eeach instruction composed of many low-level instructions and closer to English easier to read and
understand: e.g., hypotenuse= [Link](opposite*opposite+adjacent*adjacent);
1.2. Problem solving using computers
Problems: Undesirable situations that prevent an organization from fully achieving its purpose,
goals and objectives
✓ True problem situations, either real or anticipated, that require corrective action
✓ Unexploited Opportunities to improve a situation despite the absence of complaints
✓ Directives to change a situation regardless of whether anyone has complained about the
current situation
Problem solving is the process of transforming the description of a problem into the solution of that problem
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.
Problem Solving: is basic intellectual process that has been refined and systemized for the various challenges
people face.
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.
In reality, large systems are usually developed using a combination of the both methods. Top-down usually
works well for small systems but for large complex systems, the combination is the best approach and in
practice, what actually happens.
1.3. Basics of program development
Page 2 of 12 Computer Programming Lecture Note Compiled By: Department of Computer Science JKU
Computer Programming (CoSc1012 ) Chapter One: Introduction to programming
The vehicle for the computer solution to problem is a set of explicit and unambiguous
instructions called programs expressed in programming language.
Quality programming is necessary for the economic and correct solution of problems. This only
comes from good program design. The programs we design need to be:
Reliable: the program should always do what it is expected to do and handle all types of
expectations
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.
Efficient: the program should be designed to make optimal use of time, space and other resources.
1.4. Software Engineering
"Software Engineering" has been used to describe "building of software systems which are so large or so
complex that they are built by a team or teams of engineers".
Programming Life cycle
The method (approach) that software Engineers use in solving problems in computer science is called the
software development method. Another name that is commonly used for the software development method
is software life cycle.
The software life cycle has the following components
1. Preliminary Investigation
2. Analysis
3. Design
4. Implementation
5. Testing
6. Maintenance
Preliminary Investigation
➢ Defining the problem (examines documents, work papers, and procedures; observe system
operations; interview key users of the system). Suggesting a solution (often improving an existing
one or building a new information system)
➢ Feasibility study (determine whether the solution is feasible)
➢ Technical feasibility: whether implementation is possible with the available or affordable
hardware, software and other technical resources.
Page 3 of 12 Computer Programming Lecture Note Compiled By: Department of Computer Science JKU
Computer Programming (CoSc1012 ) Chapter One: Introduction to programming
➢ Economic feasibility: whether the benefits of the proposed solution outweigh the costs
➢ Operational feasibility: whether the proposed solution is desirable within the existing
managerial and organizational framework.
Analysis (Requirement gathering)
➢ Try to understand the business in general (activities done, how it is done, etc)
➢ Define the specific information requirements: who needs what information, where, when, and
how. Present a detail description of the functions the new system must perform
➢ In this phase we identify:
Inputs to the problem and their form (Filing status: Single, Married, divorced)
Outputs expected from the solution and their form (Intermediate results or Final results –
Completed tax information)
Constraints (What are the limits on the data? Income cannot be a negative number)
Assumptions (Problem deals in $US, Only for 2005)
Formulas (For determining the area, it is length x width)
Design
Based on the requirements specified in the analysis phase algorithms are developed at this stage.
Develop a series of steps with a logical order which, when applied to the input would produce the
specified output.
✓ Consider alternative technology configurations to develop the system (hardware, software,
security capability of the system, network alternatives etc.)
✓ Management and control of the technical realization of the system: detailed program
specification,
✓ Detail system specifications for the functions identified in the analysis: managerial,
organizational and technological components of the system solution (input, output, user
interface, database design, processing, manual procedures, controls: input, processes, output
and procedural controls; security, documentation, training, organizational change)
Implementation
During implementation, we translate (code) each step of the algorithm in the design specifications
into a statement in the particular language selected, and end up with a computer program.
Testing (Will the system produce the desired results?)
Page 4 of 12 Computer Programming Lecture Note Compiled By: Department of Computer Science JKU
Computer Programming (CoSc1012 ) Chapter One: Introduction to programming
Unit testing (Program testing): test each unit of program in the system separately
System testing: test the functions of the system as a whole (performance, capacity for file
storage, recovery and restart capabilities)
Acceptance testing: system is evaluated by users and management
Maintenance
Changes in hardware, software, documentation, or procedures to correct errors, meet new requirements, or
improve processing efficiency.
– Fix bugs found by the customer
– Make modifications
– Add new features
Documentation: Written descriptions of, specifications, development, and actual code of program.
Documentation is valuable because:
1. It is probable that you may come back to this program sometime in the future in order to use it again,
or you may want to use part of it in developing the solution for another similar problem.
2. If you have developed this program for a client's use, he or she will need some information so that in
your absence the program can be used.
3. If you are working for a company and have developed this program as an addition to the company's
software library, some other programmer may be assigned to maintain it or to make additions to it.
4. It is possible that sometime after you have developed the program, you may discover some errors in it
and you may be required to correct them.
The significance of proper program documentation in the software life cycle cannot be overemphasized.
Program documentation consists of
1. A concise problem specification;
2. Descriptions of inputs, expected outputs, constraints, and applicable formulas;
3. A pseudo code or flowchart for its algorithm;
4. A source program listing;
5. A self-documenting code: a program containing meaningful identifiers as well as clarifying
comments;
6. A hard copy of a sample of data and output of test run of the program; and
7. A user's guide explaining to nonprogrammer users how the program should be used.
Algorithm development
Page 5 of 12 Computer Programming Lecture Note Compiled By: Department of Computer Science JKU
Computer Programming (CoSc1012 ) Chapter One: Introduction to programming
An algorithm is procedure for solving a problem in terms of
➢ the action to execute (what to do) and
➢ the order in which these actions are executed(done)
An algorithm needs to be
➢ Precise and unambiguous (no ambiguity in any instruction and in the order of execution)
➢ Simple
➢ Correct
➢ Finite (has to have an end)
➢ Produce expected output
➢ Efficient: in time, memory and other resources
An algorithm can be expressed in many ways. Some of these methods are narrative, flowchart and pseudo-
code:
Narrative: often used to narrate the algorithm, can be understood by any user who may not have any
knowledge of computer programming. Too wordy, too unambiguous and can be interpreted in different ways.
Example: Accept salary of the employee. Calculate bonus as 10% of salary and add it to salary. Accept
service year of employee. If the service year is greater than 10, give additional 100 birr as bonus. Display the
bonus of the employee.
Flowcharts: a diagram consisting of labeled symbols, together with arrows connecting one symbols to
another.
Basic flowcharting symbols are:
Terminal point: marks the beginning or end of a program
Decision: Indicates a yes/no decision to be made by the program
Process: indicates any arithmetic computations or data movement
Input/output: is used to show input or output data
On-page Connector: used to connect two points without drawing a flow line
Flow line: is used to show the direction of logical flow
Page 6 of 12 Computer Programming Lecture Note Compiled By: Department of Computer Science JKU
Computer Programming (CoSc1012 ) Chapter One: Introduction to programming
Annotation flag: used to add clarifying comments or descriptions
Inter-page Connector: used as exit or entry from a flowchart on
one page to a flowchart in another page
Predefined Process: indicates a process defined elsewhere
Example: A certain company plan to give a 10% bonus to each of its employees at the end of every year.
If an employee has been working 10/more years at the company, she/he is to get an additional birr 100. Draw
a flowchart of an algorithm to calculate and print the bonus for a given employee.
Solution:
Analysis: the problem is to compute bonus for employee. To do so, the salary and number of service years
of the employee must be known. Let the salary be denoted by SAL, service be denoted by YEAR and bonus
by BONUS. To compute the bonus, we multiply the salary by 10% and assign the result to BONUS. Thus,
BONUS = SAL * 0.10. If the employee has served at least 10 years, birr 100 is added to the computed bonus
of the employee and the result is displayed. Otherwise the originally computed bonus is displayed.
Algorithm: uses English to write operations in a group.
1. Read employee’s salary and year of service
2. Calculate the employee’s bonus
3. If years of service is greater than or equal to 10 then increase the bonus.
Otherwise only print the bonus
Eg 1) A flowchart for calculating bonus
Page 7 of 12 Computer Programming Lecture Note Compiled By: Department of Computer Science JKU
Computer Programming (CoSc1012 ) Chapter One: Introduction to programming
Yes
No
2) A flow chart for calculating interest amount
Start
Read NAME,
BALANCE, RATE
Interest=BALANCE *RATE
Display NAME,
INTEREST
Stop
Page 8 of 12 Computer Programming Lecture Note Compiled By: Department of Computer Science JKU
Computer Programming (CoSc1012 ) Chapter One: Introduction to programming
3) A program that identifies a larger and smaller number from two numbers
Start
Read A, B
Yes Big=B
A<B?
No
Big=A
Display Big, Small
Stop
Page 9 of 12 Computer Programming Lecture Note Compiled By: Department of Computer Science JKU
Computer Programming (CoSc 1012) Chapter One: Introduction to programming
4) Calculate grade for ten students based on the scale:
>80-A
Start
>60-B
>50-C
Count=1
>40-D
<40-F
Read NAME, MARK
Yes
MARK>80 ? GRADE-A
No
Yes GRADE-B
MARK>60 ?
No
Yes GRADE-C
MARK>50 ?
No
Yes GRADE-D
MARK>40 ?
No
GRADE-F
Increment Count
No
Count>10 ?
yes
Display NAME, GRADE Stop
Page 10 of 12 Computer Programming Compiled By: Department of Computer Science JKU
Computer Programming (CoSc 1012) Chapter One: Introduction to programming
Pseudo-code:
It is much similar to real code. We use verbs to write pseudo-code. Capitalize important words that
show actions.
Eg 1) A pseudo-code to calculate interest rate
ACCEPT Name, Principal, Rate
Interest=Principal X Rate
DISPLAY Name, Interest
2) A pseudo-code to calculate bonus
ACCEPT Name, Salary
Bonus=SalaryX0.1
ACCEPT Serviceyear
IF Serviceyear>10 Then
Bonus+100
ENDIF
DISPLAY Bonus
3) A pseudo-code that calculates grade
ACCEPT Mark, Name
IF Mark>80 Then
GradeA
ELSE IF Mark>=70 Then
GradeB
ELSE IF Mark>=60 Then
GradeC
ELSE IF Mark>=50 Then
GradeD
ELSE
GradeF
ENDIF
DISPLAY Grade, Name
Most algorithms incorporate three programming conventions
Sequence
Decision and
Page 11 of 12 Computer Programming Compiled By: Department of Computer Science JKU
Computer Programming (CoSc 1012) Chapter One: Introduction to programming
Repetition
Sequence
STOCK=STOCK+ QUANTITY
Decision
IF HOURS>40
THEN
PERFORM OVERTIME
ELSE
PERFORM REGULAR
ENDIF
Repetition
INITIALIZE SUM TO ZERO
WHILE SUM<100
GET DATA
ADD DATA TO SUM
ENDWHILE
Page 12 of 12 Computer Programming Compiled By: Department of Computer Science JKU