PROBLEM SOLVING AND
PROGRAMMING
Prepared By,
Jaseela Beevi S
Assistant Professor
Dept. of CSE, TKMCE 1
Dept. of CSE
Contents - Module I
Basics of Computer Architecture: Processor, Memory, Input& Output
devices.
Application Software & System software: Compilers, interpreters, High
level and low level languages, Introduction to structured approach to
programming, Flow chart, Algorithms, Pseudo code (bubble sort, linear
search - algorithms and pseudocode).
Dept. of CSE, TKMCE 2
Basics of Computer Architecture
Computer is an advanced
electronic device that takes raw
data as input from the user and
processes these data under the
control of set of instructions
(called program) and gives the
result (output) and saves output
for the future use.
Dept. of CSE, TKMCE 3
Basic Functional Units of a Computer
1.Central Processing Unit (CPU)
It is the brain of the computer system. All major calculation and
comparisons are made inside the CPU and it is also responsible for
activation and controlling the operation of other unit.
This unit consists of two major components that are arithmetic logic
unit (ALU) and control unit (CU)
Dept. of CSE, TKMCE 4
• Arithmetic Logic Unit (ALU)
Here arithmetic logic unit performs all arithmetic operations such as
addition, subtraction, multiplication and division. It also uses logic
operation for comparison such as less than, greater than etc.
• Control Unit (CU)
The control unit of a CPU controls the entire operation of the computer. It
also controls all devices such as memory, input/output devices connected
to the CPU.
Dept. of CSE, TKMCE 5
2.Input unit and Output Unit
The input and output unit consists of devices used to transmit information
between the external world and computer memory. The information fed through
the input unit is stored in computer's memory for processing and the final result
stored in memory can be recorded or display on the output medium.
Eg: Mouse, Keyboard, Printer, Monitor, etc.
3.Memory Unit
Computer memory is any physical device capable of storing information
temporarily, like RAM (Random Access Memory), or permanently, like ROM (Read-
Only Memory).
Memories can be classified into two categories
üPrimary Memory
üSecondary Memory
Dept. of CSE, TKMCE 6
Dept. of CSE, TKMCE 7
Primary memory(Main memory) is computer memory that is accessed
directly by the CPU. There are two types of primary memory.
ü Read Only Memory (ROM)
ü Random Access Memory (RAM)
The content of ROM cannot be changed and can be used only by CPU. It is
needed to store Basic Input Output System (BIOS), which is responsible
for booting. This memory is permanent in storage (non volatile) and is
very small in size.
The RAM is a volatile memory i.e. its contents get destroyed as soon as
the computers is switched off. All kinds of processing of CPU are done in
this memory.
Dept. of CSE, TKMCE 8
Dept. of CSE, TKMCE 9
Two main types of RAM are:
ü Static RAM
ü Dynamic RAM
Static RAM - In this type of RAM, data is stored using the state of a six
transistor memory cell. Static RAM is mostly used as a cache memory for
the processor (CPU).
Dynamic RAM - It is a type of RAM which allows you to stores each bit of
data in a separate capacitor within a specific integrated circuit. Dynamic
RAM is a standard computer memory of the many modern desktop
computers.
This type of RAM is a volatile memory that needs to be refreshed with
voltage regularly. Else it loses the information stored on it.
Dept. of CSE, TKMCE 10
Basic Functional Units of a Computer
RAM ROM
Temporary Storage Permanent Storage
Store data in GBs (1-128 GB per chip) Store Data in MBs(4-8MB per chip)
volatile Non volatile
Used in normal operations Used for startup process of computer
Writing data is faster Writing data is slower
Dept. of CSE, TKMCE 11
Types of ROM
1. PROM (Programmable read-only memory) – It can be programmed
by user. Once programmed, the data and instructions in it cannot be
changed.
2. EPROM (Erasable Programmable read only memory) – It can be
reprogrammed. To erase data from it, expose it to ultra violet light. To
reprogram it, erase all the previous data.
3. EEPROM (Electrically erasable programmable read only memory) –
The data can be erased by applying electric field, no need of ultra violet
light. We can erase only portions of the chip.
Dept. of CSE, TKMCE 12
Secondary Memory
Primary memory has limited storage capacity and is volatile. Secondary
memory overcome this limitationby providing permanent storage of data
and in bulk quantity.
Secondary memory is also termed as external memory and refers to the
various storage media on which a computer can store data and programs.
The Secondary storage media can be fixed or removable. Fixed Storage
media is an internal storage medium likehard disk that is fixed inside the
computer.
Storage medium that are portable and can be taken outside the computer
are termed as removable storage media.
eg: Hard disk, Solid state driveMagnetic Tapes, Pen drive
Dept. of CSE, TKMCE 13
Memory Hierarchy
Dept. of CSE, TKMCE 14
Registers
Purpose of having register is fast retrieval of data for processing by CPU. It
is high speed storage areas, least storage capacity, directly accessed &
manipulated by CPU during instruction execution.
Eg. of Registers: ACC, IR, PC, MAR, MDR ,GPR(R0,R1…)
Number of registers: Ten to hundreds
Examples of registers are:
1. Accumulator(ACC)
-used to store data taken from memory.
2. Memory Address Register(MAR)
-address of the location to be accessed from memory.
Dept. of CSE, TKMCE 15
3.Memory Data Register(MDR)
-Contains data to be written onto or to be readout from the addresses
location.
4.Program Counter(PC)
Used to keep the track of execution of the program. It contains the
memory address of the next instruction to be fetched from the memory.
5.Instruction Register(IR)
-It holds the instruction which is just about to be executed.
Dept. of CSE, TKMCE 16
Cache Memory
It is a small, fast memory unit located close to the CPU. It stores
frequently used data and instructions that have been recently accessed
from the main memory.
Magnetic Disk
Magnetic Disks are simply circular plates that are fabricated with either a
metal or a plastic or a magnetized material. The Magnetic disks work at a
high speed inside the computer and these are frequently used.
Magnetic Tape
Magnetic Tape is simply a magnetic recording device that is covered with
a plastic film. It is generally used for the backup of data. In the case of a
magnetic tape, the access time for a computer is a little slower and
therefore, it requires some amount of time for accessing the strip.
Dept. of CSE, TKMCE 17
Magnetic Tape & Magnetic Disk
Dept. of CSE, TKMCE 18
Types of Software
1) System Software
2) Application Software
System Software
It is the type of software that is the interface between application
software and the system. Low-level languages are used to write the
system software. System Software maintains the system resources and
gives the path for application software to run. An important thing is
that without system software, the system can not run. It is general-
purpose software.
eg., Operating Systems, Language Processor, Device Drivers
Dept. of CSE, TKMCE 19
Types of Software
Application Software
It is the type of software that runs as per user request. It runs on the
platform which is provided by system software. High-level languages
are used to write the application software. It’s a specific purpose
software.
eg., Word Processor, Web Browsers, Spread Sheets
Dept. of CSE, TKMCE 20
Types of Software
Dept. of CSE, TKMCE 21
Types of Software
Dept. of CSE, TKMCE 22
Computer Languages
Computer languages are the languages through which the user can communicate with the
computer by writing program instructions.
Dept. of CSE, TKMCE 23
Computer Languages
• Low-Level Language (Machine Language)
Low-Level language is the only language which can be understood by
the computer. Binary Language is an example of a low-level language.
Low-level language is also known as Machine Language. The binary
language contains only two symbols 1 & 0. Machine language is also
known as the Machine Code
- It is machine dependent
Dept. of CSE, TKMCE 24
Computer Languages
• Middle-Level Language (Assembly Language)
This is a computer language in which the instructions are created using
symbols such as letters, digits and special characters.
Eg., Assembly language
In assembly language, we use predefined words called mnemonics.
But the computer cannot understand mnemonics, so we use a
translator called Assembler to translate mnemonics into binary
language.
- it is machine-dependent.
Dept. of CSE, TKMCE 25
Computer Languages
• High-Level Language
A high-level language is a computer language which can be understood by the
users. The high-level language is very similar to human languages and has a set
of grammar rules that are used to make instructions more easily.
Every high-level language has a set of predefined words known as Keywords
and a set of rules known as Syntax to create instructions.
High-level language needs to be converted into the low-level language to make
it understandable by the computer. We use Compiler or interpreter to convert
high-level language to low-level language.
Eg., COBOL, FORTRAN, BASIC, C, C++, JAVA, etc.
Dept. of CSE, TKMCE 26
Translator Software
Converts program in High-level language & Assembly language to
Machine-level language
Three kinds:
— Assembler
— Compiler
— Interpreter
Assembler converts Assembly language program to Machine language.
Compiler & Interpreter converts High-level language program to
Machine language.
Dept. of CSE, TKMCE 27
Translator Software
Compiler Interpreter
Looks at entire source code Looks at a source code line-by-line
Entire source code converted into object code Line-by-line conversion
Object code executed multiple times For each execution, source code
interpreted and then executed
During execution, Source Code and Compiler not Source code and Interpreter needed during
needed execution
Compiled programs execute faster Programs execute slower
Dept. of CSE, TKMCE 28
Structured Approach to Programming
Structured programming is a programming paradigm aimed at
improving the clarity, quality, and development time of a computer
program by organizing it into logically structured blocks of code.
The structured program mainly consists of three types of elements:
ü Selection Statements
ü Sequence Statements
ü Iteration Statements
Dept. of CSE, TKMCE 29
Flowchart, Algorithm and Pseudo Code
Algorithm
It is a complete step by step representation of the solution of the problem,
represented in English like Languages. An algorithm can be abstract or quite
detailed. A detailed algorithm consists of every step, equivalent to one
instruction of a programming language.
Characteristics of good algorithm:
1. An algorithm should be finite.
2. Steps in an algorithm should be clear and unambiguous.
3. Algorithm should be precise.
4. Algorithm must have 0 or more inputs and 1 or more output.
Dept. of CSE, TKMCE 30
Write an algorithm to find the sum of two numbers
Step 1 : start
Step 2 : input first number as a
Step 3 : input second number as b
Step 4 : add a and b and assign to a variable sum
Step 5 : print sum
Step 6 : stop
Dept. of CSE, TKMCE 31
Flowchart, Algorithm and Pseudo Code
Pseudo Code
It is a more formal representation than the algorithm. Here, we
represent every step in a formal way which is very close to the actual
programming language representation.
Pseudocode is the basis of algorithm. All pseudocodes will start with
the keyword “START” or “BEGIN” and complete with keyword “STOP”
or “END”.
Dept. of CSE, TKMCE 32
Write pseudocode to find the sum of two numbers.
BEGIN
NUMBER a, b, sum
OUTPUT("Input number1:")
INPUT a
OUTPUT("Input number2:")
INPUT b
sum=a+b
OUTPUT sum
END
Dept. of CSE, TKMCE 33
Algorithm Vs Pseudo Code
Dept. of CSE, TKMCE 34
Flowchart - Symbols
Dept. of CSE, TKMCE 35
Flowchart
Very popular method to represent the steps of the solution is the
flowchart, which uses many graphical symbols and thus, is more
understandable. The symbols used for various different types of
statements are as shown
Dept. of CSE, TKMCE 36
Write an algorithm to find the area of a circle
Step 1 : start
Step 2 : input the radius as r
Step 3 : set pi = 3.14
Step 4 : calculate area = pi * r * r
Step 5 : print area
Step 6 : stop
Dept. of CSE, TKMCE 37
Write pseudocode to find the area of a circle.
BEGIN
NUMBER r, area
INPUT r
area=3.14*r*r
OUTPUT area
END
Dept. of CSE, TKMCE 38
Write an algorithm to check whether a number is positive, negative or
zero.
1. Start
2. Input number
3. If number > 0 then go to step 4, else go to step 5
4. print "Number is positive"
5. If number < 0 then go to step 6, else go to step 7
6. print "Number is negative"
7. print "Number is zero"
8. stop
Dept. of CSE, TKMCE 39
Flowchart to check whether a number is positive, negative or zero.
Dept. of CSE, TKMCE 40
Write an algorithm to find the largest among three numbers.
1. Start
2. Input A,B,C
3. If (A>B) and (A>C) then go to step 4 else go to step 5
4. print “A is greater”.
5. if (B>A) and (B>C) then go to step 6 else go to step 7
6. print “B is greater”.
7. print “C is greater”.
8. Stop
Dept. of CSE, TKMCE 41
Flowchart to find the largest among three numbers.
Dept. of CSE, TKMCE 42
Write an algorithm to print the numbers from 1 to 10
Step-1: Start
Step-2: set i=1
Step-3: If i ≤ 10, then go to step-4, Otherwise go to step-6
Step-4: Print value of i
Step-5: i=i+1 and repeat step-3 to 5 until i ≤ 10
Step-6: Stop
Dept. of CSE, TKMCE 43
Flowchart to print the numbers from 1 to 10
This symbol is not
essential
Dept. of CSE, TKMCE 44
Write an algorithm to check whether a natural number (all positive
integers from 1 to infinity) is prime or not.
1. start
2. read n
3. set i = 2
4. Repeat step 5 and 6 until i < n
5. if n%i = 0 then go to step 7 else go to step 6
6. set i = i + 1
7. if i = n go to step 8 else go to step 9
8. print number is prime
9. print number is not prime
10. stop
Dept. of CSE, TKMCE 45
Flowchart to check whether a natural number is prime or not.
Dept. of CSE, TKMCE 46
Flowchart to check an integer(that can be positive, negative, or zero)
is prime or not
Dept. of CSE, TKMCE 47
Thank You...
Dept. of CSE, TKMCE 48