Chapter 1 Introduction to Programming(1)
Chapter 1 Introduction to Programming(1)
Introduction to Computer
Programming
By
Ferhan Gursum
Contents
What is computer programming
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
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…
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.
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…
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.
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
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
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.
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