Chapter One
Introduction to Computer
Programming
By
Ferhan Gursum
Contents
What is computer programming
Types of computer languages
Program Development Stages
Analysis, Design, Coding, Testing, maintenance
Algorithms and Flowcharts
Examples
Computer programs, known as software, are instructions
What is a Computer Program?
to the computer.
A sequence of statements that instruct a computer in how to solve
a problem.
You tell a computer what to do through programs.
Without programs, a computer is an empty machine.
For a computer to be able to do anything (start up, play a
song, run a word processor, etc…), it must first be given the
instructions to do so.
What is a Computer Program?...
Computers do not understand human languages,
so you need to use computer languages to communicate with
them.
The programmers can communicate with the computers
effectively with the help of Programming Languages apart
from the operating systems,
and specify his own needs in a standard form and develop
new computer program/applications which can achieve
particular tasks, with the maximum accuracy and speed.
Computer programming
Often shortened to programming or coding -
is the process of designing, writing, testing,
debugging, and maintaining computer
programs.
The purpose of programming is to create a set
of instructions that computers use to perform
specific operations
requires expertise in many different subjects,
including knowledge of the application domain,
specialized algorithms and formal logic.
Types of Programming Languages
Types of programming languages:
a) Machine Language
b) Assembly Language
c) High-Level Language
6
Types of Programming Languages…
Machine language is a set of primitive instructions built
into every computer.
The instructions are in the form of binary code, so you
have to enter binary codes for various instructions.
Program with native machine language is a tedious
process.
Moreover the programs are highly difficult to read and
modify.
For example, to add two numbers, you might write an
instruction in binary like this:
1101101010011010
7
Types of Programming Languages…
Machine language is machine dependent.
A program written in machine language cannot be
run on another type of computer without
significant alterations.
Machine language is sometimes also referred as the
binary language.
The language of 0 and 1 where 0 stands for the
absence of electric pulse and 1 stands for the
presence of electric pulse.
8
Types of Programming Languages…
Advantages of Machine Language
It makes fast and efficient use of the computer.
It requires no translator to translate the code i.e. directly
understood by the computer.
Disadvantages of Machine Language:
All operation codes have to be remembered
All memory addresses have to be remembered.
It is hard to amend or find errors in a program written in
the machine language.
9
Types of Programming Languages…
Assembly Languages
were developed to make programming easy.
Assembly languages began using English like
abbreviation to represent the elementary operation
instead of using 0s and 1s.
Since the computer cannot understand assembly
language, however, a program called assembler is
used to convert assembly language programs into
machine code.
10
Types of Programming Languages…
Advantages of Assembly Language
It is easier to understand and use as compared to
machine language.
It is easy to locate and correct errors.
Disadvantages of Assembly Language
Like machine language it is also machine dependent.
Since it is machine dependent therefore programmer
should have the knowledge of the hardware also.
11
Types of Programming Languages
The high-level languages
The assembly languages started using English like words,
but still it is difficult to learn these languages.
High level languages are the computer language in which
it is much easier to write a program than the low level
language.
A program written in high level language is just like
corresponding instruction to person in daily life.
Since the computer cannot understand high level
language, however, a program called translator is used to
convert high level language programs into machine code.
12
Types of Programming Languages
For example, the following is a high-level language
statement that computes the area of a circle with radius 5:
area = 5 * 5 * 3.1415;
Advantages of High Level Language
Similar to English with vocabulary of words and symbols
Therefore it is easier to learn, write, and maintain.
Disadvantages of High Level Language
A high-level language has to be translated into the
machine language by a translator and thus a price in
computer time is paid.
13
Popular High-Level Languages
COBOL (COmmon Business Oriented Language)
FORTRAN (FORmula TRANslation)
BASIC (Beginner All-purpose Symbolic Instructional
Code)
Pascal (named for Blaise Pascal)
Ada (named for Ada Lovelace)
C (whose developer designed B first)
Visual Basic (Basic-like visual language developed by
Microsoft)
Delphi (Pascal-like visual language developed by Borland)
C++ (an object-oriented language, based on C)
Java (We use it in the book)
C#
etc
14
Program Development
Generally, we need programming language in order to
solve a problem.
Computer problem solving is an intricate process
requiring careful planning and attention to detail.
To be able to get a quality software product or program
we need to pass the following stages.
Collection & Requirement Analysis
Design
Coding
Testing
Maintenance
Requirement Analysis
Feasibility study (often carried out
Understanding the problem at hand and analysis of the
data and procedures needed to achieve the desired
result.
Customers typically know what they want, but not
what software should do.
To understand what are the requirements of a system
requirement engineering includes the following
activities
Feasibility Study
Requirements Elicitation
Requirements analysis
Requirements specification
Requirement Validation
Design
Once the requirements of the program are defined. The
next stage is to work on design of a system.
Design is part of solution phase
Represent the software system functions in a form that
can be transformed into one or more executable programs.
Some Types of Design
Architectural design - Identify sub-systems
Interface design - Describe sub-system interfaces
Data structure design - Design data structures to hold problem
data
Algorithm design - Design algorithms for problem functions
Coding/Implementation
Is the stage where each statement for the design is
translated into a target programming language.
The selection of the target language may depend on
several factors.
An important task of coding, other than the actual
algorithm, is documenting the internal design of
software
for the purpose of future maintenance and enhancement.
Testing
Once a program is implemented , the next step is to
testing it.
Program testing involves two steps, namely,
debugging, and testing.
Testing: – making sure that the program does what is
intended for.
Debugging:- Error in a program are called bug and
the process of locating and removing errors is called
debugging.
Maintenance
Is the process of changing a software after it has
been delivered
There are many reasons why programs must be
continuously modified and maintained
new user needs
previously undiscovered bugs
changes in hardware platform
efficiency improvements
Documenting, commenting, etc.
Algorithms
Suppose you are requested to put the following
words in alphabetical order: apple, zebra, abacus,
and bag.
1. Look for the word that starts with "A".
2. If you found a word beginning with "A", put that word at
the beginning of the list (in your mind).
3. Look for another word beginning with "A".
4. If there's another word beginning with "A", compare its
second letter with the second letter of our first "A" word.
5. If the second letters are different, put the two words in
alphabetical order by their second letter. If the second
letters are the same, proceed to the third letter, and so
on.
6. Repeat this whole process for "B" and each other letter
in alphabetical order, until all the words have been
moved to the appropriate place.
Algorithms...
What is an Algorithm?
Algorithm (after Al Kho-war-iz-mi a 9th century Persian
mathematician) is a finite set of unambiguous steps (instructions)
for solving a problem.
A procedure or formula for solving a problem.
Therefore to solve a certain problem we need an algorithm.
An algorithm should be:
Simple
Correct
Efficient
Finite
Algorithms…
Though instructions in an algorithm appears
in sequence they may not execute in that
order.
An instruction that alters the order of an
algorithm is called a control structure
Three Categories of Algorithmic Operations
or control structure:
sequential operations
instructions are executed in order
conditional ("question asking") operations
a control structure that asks a true/false question and
then selects the next instruction based on the answer
iterative operations (loops)
a control structure that repeats the execution of a
block of instructions
How to represent algorithms?
Use natural languages
too verbose
too "context-sensitive"- relies on experience of
reader
Use formal programming languages
too low level
requires us to deal with complicated syntax of
programming language
Flowcharts
a graphical tool that diagrammatically depicts the
steps and structure of an algorithm or program
Pseudo-Code
natural language constructs modeled to look like
statements available in many programming
Flowcharts
A flowchart is a diagram made up of
boxes, diamonds and other shapes, connected
by arrows - each shape represents a step in
the process, and
the arrows show the order in which they
occur.
Flowchart shows the operations and logical decisions of a
computer program.
Once the flowchart is drawn, it becomes easy to write the
program in any high level language.
In computing, there are dozens of different
symbols used in flowcharting
However, there are about six most common
Note :
Every program flowchart should begin with the oval symbol
containing START or BEGIN and end with STOP or END.
In every program flowchart crossing flow lines should be
avoided.
Only one flow line should come out from a I/O and process
symbols.
Only one flow line should enter a decision symbol, but two
flow lines, one for each possible answer, should leave the
decision symbol.
All boxes of the flowchart are connected with Arrows. (Not
lines)
Only one flow line is used in conjunction with terminal symbol.
Examples
Example 1
Draw a flowchart for a problem that can calculates sum
and average of two numbers.
Solution: The algorithm description is
1. Read the values of the two numbers(x & y)
2. Add two numbers x and y and assign to sum variable.
(sum=x+y).
3. Divide the sum by 2 and assign to average ( average=
sum/2).
4. Display the output (average).
Examples
Flowchart
Start
Read x, y
sum x+y
average
sum/2
Print Sum,
average
End
Example 2
Write a pseudo-code and draw a flowchart for a
problem that identifies the given number is odd or
even.
Solution:
Algorithm Description:
1. Read the value of x.
2. Check if x is even
Yes: print x is even
No: print x is odd
Start
Flowchart
Read x
Yes
X is Even
X%2
=0?
No Stop
X is Odd
Stop
Example 3
Write down an algorithm and draw a
flowchart to find and print the largest of
three numbers. Read numbers one by one.
Solution:
Step 1: Input N1
Step 2: Max N1
Step 3: Input N2
Step 4: If (N2>Max) then Max = N2
Step 5: Input N3
Step 6: If (N3>Max) then Max = N3
Step 7: Print “The largest number is:”,Max
Flowchart
Example 4
Draw a flowchart of an algorithm for summing the
first 50 integer.
Solution: Algorithm Description:
1. Initialize sum to 0 and counter to 1
2. If the counter is less than or equal to 50 go to step 3 else
go to step 6
3. Add counter to sum
4. Increase counter by 1
5. Repeat step 2
6. print sum
Flowchart
Start
Sum 0
Counter 0
Counter> Write
50 sum
COUNTER Stop
COUNTER + 1
SUM SUM
+ COUNTER
Example 5
Write down an algorithm and draw a flowchart to find
and print the largest of N (N can be any number)
numbers. Read numbers one by one.
Answer
Step 1: Input N
Step 2: Input Current
Step 3: Max Current
Step 4: Counter 1
Step 5: While (Counter < N) Repeat steps 5
through 8
Step 6: Counter Counter + 1
Step 7: Input Next
Step 8: If (Next > Max) then Max Next
Step 9: Print Max
Flowchart
START
Input
N, Current
Max Current
Counter 1
N
Counter <
N
Y Print
Counter Max
Counter +1
Input
Next
STOP
Next N
>Max
Y
Max Next
Exercises
1. Write an algorithm and draw a flowchart for a
problem that will read the two sides of a rectangle
and calculate its area.
2. Write an algorithm and draw a flowchart that will
find and print the product of 3 numbers.
3. Write an algorithm and draw a flowchart that will
find and print the factorial of a NUMBER.
4. Write an algorithm and draw a flowchart for a
problem that accepts a set of integers and finds the
number of positive and negative numbers. The
program stops when the user enters 0 for the
integer value.
5. Write a flowchart where it prints all the number till
N which divisible by 5 or 10 or 3
6. Write pseudo code and draw flowchart for printing
Sequesnce:1,3,5,9,…. N
Exercises…
6. Write a pseudocode and draw a flowchart for
fibbonacci series.(ex: 0,1,1,2,3,5,8,13.....Nnth
where Nnth = N(nth-1)+N(nth-2)
7. Write and algorithm and draw a flowchart to
read an employee name (NAME), overtime hours worked
(OVERTIME), hours absent (ABSENT) and
determine the bonus payment (PAYMENT).
Assignment 1
1. Write an algorithm and draw a flowchart to print the
square of all numbers from LOW to HIGH. Test with
LOW=1 and HIGH=10.
2. Write an algorithm for a program that accepts a set of
numbers and finds the smallest among them and
computes the sum of the numbers accepted. The
program stops when the user enters number 999.
Convert the algorithm into flowchart
3. Write an algorithm and draw a flowchart for the problem
that will find and print the number of vowels in a given
set of characters and print there number of occurrences
4. Write pseudo code and draw flowchart for converting
decimal whole number to binary number