04-09-2024
Computer Programming
DR. MADHUSMITA SAHU
ASSOCIATE PROFESSOR
DEPARTMENT OF COMPUTER SCIENCE AND INFORMATION TECHNOLOGY
Topics to be discussed
Computer
Problem Solving
Algorithm
Flowchart
Program and Programming Language
1
04-09-2024
Computer
The term computer is derived from the word compute.
A computer is an electronic device that takes data and instructions as an input from the users,
processes data and provides useful information known as output.
The electronic device is known as hardware and the set of instructions is known as software.
Basic operations of computer
Inputting: The process of entering data and instructions into the computer system.
Storing: Saving data and instructions so that they are available for initial or for additional
processing as and when required.
Processing: Performing arithmetic operations or logical operations on data in order to convert
them into useful information.
Outputting: The process of producing useful information or results for the user, such as a
printed report or visual display.
Controlling: Directing the manner and sequence in which all of the above operations are
performed.
2
04-09-2024
Block Diagram of a Computer
Input Unit
Accepts (or reads) the list of instructions and data from the outside world.
Converts these instructions and data in computer acceptable form.
Supplies the converted instructions and data to the computer system for further processing.
3
04-09-2024
Output Unit
Accepts the results produced by the computer which are in coded form and hence cannot be
easily understood by humans.
Converts these coded results to human acceptable form.
Supplies the converted results to the outside world.
Central Processing Unit (CPU)
It is the brain of the computer.
All major calculations and comparisons are made inside the CPU which is responsible for
activating and controlling the operations of other units of the computer system.
Components in CPU
Arithmetic and Logic Unit (ALU)
Control Unit (CU)
Memory Unit (MU)
4
04-09-2024
Arithmetic and Logic Unit (ALU)
It is the place in a computer where the actual execution of the instructions takes place during the
processing operation.
All types of arithmetic computations and logical operations are done inside ALU.
The data and instructions, stored in memory prior to processing, are transferred to ALU as and
when required when processing takes place.
Intermediate results generated in ALU are temporarily transferred back to the memory unit
needed at a later time.
After the completion of processing, the final results which are stored in memory unit are released
to an output device.
Control Unit (CU)
The purpose of control unit is to pass data into the ALU and then inform ALU which arithmetic
or comparison operation to perform.
Once ALU has computed the result, the control unit passes it into the memory.
It does not perform any actual processing on the data.
It acts as a central nervous system for the other components of the computer.
It manages and coordinates the entire computer system.
5
04-09-2024
Memory Unit (MU)
Does not any perform any type of processing.
Used to hold
All the data to be processed and the instructions required for processing (received from input devices).
Intermediate results of processing.
Final results of processing before these results are released to an output device.
Auxiliary Memory Unit
Main memory is found physically inside the computer.
It is often not practical to build a very large size primary memory to meet all the storage
requirements of the system to handle present day business, scientific and technical applications.
Thus, to store data and instructions on permanent basis, auxiliary memory is used.
It is physically located outside the computer.
6
04-09-2024
Types of Memory
Secondary (Auxiliary) memory devices
Magnetic Tape
Magnetic Hard Disk
Floppy Disk
Optical Disk
Rewrite Optical Disk
7
04-09-2024
Input devices
Keyboard Barcode Reader
Mouse Magnetic-Ink Character Recognition (MICR)
Trackball Modem
Joystick Touch Screen
Digitizer Tablet Voice Input and Recognition System
Light pen Voice Response System
Scanner Punching Card Machine
Optical Scanner Data Entry Machine
Optical Mark Reader (OMR)
Optical Character Reader (OCR)
Output Devices
Monitor
Printer
Plotter
Projector
Speaker
Headphones
Light/LED
8
04-09-2024
Problem Solving
Problem solving is an art in itself.
One guiding principle is to break the complex problem into several
comprehensible sub-problems.
The user has to identify the successive operations required for solving each of
these sub-problems.
Once this is successfully completed, a solution to each of the sub-problems is
arrived at.
These solutions are combined to form a solution to the original problem.
Steps in problem solving
Understanding what the problem is
Analysing the problem
Development of the solution
Coding and implementation
9
04-09-2024
Understanding what the problem is
Try to understand the problem to be solved.
Try to be sure about the objectives of the problem.
Analysing the problem
After understanding thoroughly the problem,
look for different ways of solving the problem and
evaluate each one of these methods.
The idea is to search an appropriate solution to the problem under consideration.
The end result of this stage is a broad overview of the sequence of operations that are to be
carried out to solve the problem.
10
04-09-2024
Development of the solution
The overview of the sequence of operations is expanded to form a detailed step by step
solution to the problem under consideration.
Coding and implementation
Convert the detailed sequence of operations into a language that the computer can
understand.
Here, each step is converted to its equivalent instruction or instructions in the programming
language that has been chosen for the implementation of the solution.
11
04-09-2024
Algorithm
An algorithm is a step by step sequence of instructions that give a complete solution to a
particular problem.
The number of instructions in an algorithm is always finite.
Thus, an algorithm is a collection of finite number of instructions arranged in a sequence to
solve a particular problem.
Algorithm is a well-defined procedure that allows a computer to solve a problem.
Coding
The process of conversion of various steps of the algorithm into its
equivalent instruction or instructions in the programming language
that has been chosen for the implementation of the solution is called
coding.
12
04-09-2024
Flowchart
Flowcharts are graphical means of describing a sequence of instructions or operations that are
carried out to perform a certain task.
It is just a pictorial representation of an algorithm.
Flowcharts use symbols that have geometrical shapes to indicate the different operations.
These symbols are connected by lines which indicate the order of execution of the various
activities.
A flowchart is a type of diagram that represents an algorithm, workflow or process, showing
the steps as boxes of various kinds, and their order by connecting them with arrows
Basic symbols in Flowchart
Name of Symbol Description Symbol Notation
Assignment symbol Used for assignment or process Rectangular box
Decision symbol Indicate a conditional transfer. It has single Diamond shaped box
entry and two or more exits.
Input/output symbol Used for input and output operations Parallelogram
Terminal symbol Used to indicate the entry or exit point of a A flat oval
flowchart
Connection symbol Used to connect two symbols in a flowchart Circle
on the same page
Arrow symbol Used to indicate the direction of flow
13
04-09-2024
Example 1
Find area of a triangle given three sides.
1. Start
2. Read a, b, c
3. s=(a+b+c)/2
4. area=sqrt(s*(s-a)*(s-b)*(s-c))
5. Print area
6. Stop
Example 2
Find largest of two numbers.
1. Start
2. Read a, b
3. If a>=b then goto step 6
4. Print b
5. goto step 8
6. Print a
7. goto step 8
8. Stop
14
04-09-2024
Example 3
Find sum of first 100 natural numbers.
1. start
2. sum=0
3. i=1
4. if i<=100 then goto 7
5. print sum
6. goto 10
7. sum=sum+i
8. i=i+1
9. goto step 4
10. stop
Pseudocode
Pseudocode is a simple way of writing programming code in English.
Pseudocode is not an actual programming language.
It uses short phrases to write code for programs before you actually create it in a specific
language.
Pseudocode is an artificial and informal language that helps programmers develop algorithms.
Pseudocode is very similar to everyday English.
15
04-09-2024
Example
Write an algorithm to determine a student’s final grade and indicate whether it is passing or
failing.
The final grade is calculated as the average of four marks.
Pseudocode
Input a set of 4 marks
Calculate their average by summing and dividing by 4
if average is below 50
Print “FAIL”
else
Print “PASS”
16
04-09-2024
Algorithm
1. Input M1,M2,M3,M4
2. GRADE (M1+M2+M3+M4)/4
3. if (GRADE < 50) then
Print “FAIL”
else
Print “PASS”
endif
Program
Program is a sequence of instructions in a particular programming language, written to
perform a specified task on a computer.
17
04-09-2024
Programming Language
A language is a system of communication.
A programming language is a means of communication used to communicate between people
and the computer.
The set of symbols used by a particular language is known as the vocabulary of that language.
All computer programming languages have their own vocabulary.
Each symbol of a computer language is used to tell the computer to do a particular job.
The process of writing program instructions for an analysed problem is called coding.
Types of programming language
Low-level (machine-dependent) High-level (machine-independent)
• Machine language • FORTRAN (FORmula TRANslation)
• Assembly language • COBOL (Common Business Oriented Language)
• BASIC (Beginners All-purpose Symbolic Instruction
Code)
• PASCAL
• PL/I (Programming Language One)
• RPG (Report Program Generator)
• ALGOL (ALGOrithmic Language)
• APL (A Programming Language)
• ADA
• LISP (LISt Processing)
• SNOBOL (StriNg Oriented Symbolic Language)
•C
• PROLOG (PROgramming LOGic)
• Java
• C++
18
04-09-2024
Machine language
It is easily understood by computers, machine languages are almost impossible for humans to
use because they consist entirely of numbers.
For example, an x86/IA-32 processor can execute the following binary instruction as expressed
in machine language:
Binary: 10110000 01100001 (Hexadecimal: 0xb061)
Assembly Level Language
An assembly language is a low-level language for programming computers.
It implements a symbolic representation of the numeric machine codes called mnemonics.
For example(Adds 2 numbers)
name “add”
mov al, 5;
mov bl, 10;
add bl, al;
19
04-09-2024
High-level language
High-level languages are used to solve problems and are often described as problem-oriented
languages.
Some High Level Languages are: Ada, Algol, LISP, Java.
Source code
A program written in a high-level language is called source code.
Source code is also called source program.
Computer cannot understand the statements of high-level language.
The source code cannot be executed by computer directly.
It is converted into object code and then executed.
20
04-09-2024
Object code
A program in machine language is called objet code.
It is also called object program or machine code.
Computer understands object code directly.
Translator
Used to convert high-level and low-level languages into machine language.
Types
Compiler
Interpreter
Assembler
21
04-09-2024
Compiler
A translating program that translates the instructions of a high-level language into machine
language.
It's a computer program(s) that transforms source code written in a programming language
into machine language that is the target language which usually has a binary form known as
object code.
It compiles a set of machine language instructions for every program instruction of a high-level
language.
A program written by a programmer in a high-level language is called a source program.
The program generated by a compiler after converting to machine language is called an object
program.
Compiler searches all errors in the code and lists them.
Interpreter
A translating program that translates the instructions of a high-level language into machine
language.
It translates high level instructions into an intermediate form, it translates the code into the
intermediate form line by line an caries out specific actions.
It takes one statement of a high-level language and translates it into a machine instruction
which is immediately executed.
It translates one instruction and the resulting machine code is executed, the next instruction is
translated and the resulting machine code is executed and so on.
Interpreter searches the error statement by statement
22
04-09-2024
Assembler
A translating program that translates the instructions of assembly language into machine
language.
It is a program that takes basic computer instruction(s) and converts then into a pattern of bits
that the computer's processor can use to perform it's basic operations.
The language used to program the assembler is called assembly language.
Loader
A program that loads the object program into memory for execution.
It is the part of an operating system that is responsible for loading programs from executables
(i.e., executable files) into memory, preparing them for execution and then executing them.
23
04-09-2024
Linker
A program that links various libraries and files to the source program.
It is a program that takes one or more objects generated by a compiler and combines them into
a single executable program.
Difference between Compiler and Interpreter
Compiler Interpreter
Compiler Takes Entire program as input. Interpreter Takes Single instruction as input
Intermediate Object Code is Generated No Intermediate Object Code is
Generated
Conditional Control Statements are Executes faster Conditional Control Statements are Executes slower
Memory Requirement: More(Since Object Code is Memory Requirement is Less
Generated)
Program need not be compiled every time Every time higher level program is converted into
lower level program
Errors are displayed after entire program is checked Errors are displayed for every instruction interpreted
(if any)
Example : C Compiler Example: BASIC
24
04-09-2024
Program Design
Designing is the first step for obtaining solution of a given problem.
Various designing methodologies are:
Top-down design
Bottom-up design
Modular approach
Top-down design
If we look at a problem as a whole, it may seem impossible to solve because it is so complex.
For example, writing a tax computation program.
Complex problems can be solved using top-down design, also known as stepwise refinement.
25
04-09-2024
Bottom up Design
It is the reverse of top-down design.
It starts from the lowest level components to the highest level components.
It first design the basic components and from these basic components the higher level
components are designed.
Modular Approach
It is better to divide a large system into modules.
Module is a well defined part of program.
It is easy to modify a program written with modular approach because changes in one module
don’t affect other modules of program.
Information hiding.
Easy debugging.
26
04-09-2024
Bit/Byte
1 bit = a binary digit 0 or 1
1 nibble=4 bit
1 byte(B)=8 bit
1 kilobyte(KB)=210 B=1024 B
1 megabyte(MB)=1048576 B=220 B=210KB
1 gigabyte(GB)=1073741824B=230B=210MB=220KB
1 terabyte(TB)=1099511627776B= 240B=210GB=220MB
1 petabyte(PB)=1125899906842624B =250B=210TB=220GB
1 exabyte(EB)=1152921504606846976B= 260B=210PB=220TB
1 zettabyte(ZB)=1180591620717411303424B= 270B=210EB=220PB
1 yottabyte(YB)=1208925819614629174706176B= 280B=210ZB=220EB
27