0% found this document useful (0 votes)
2 views

Unit 1 PSC Verified

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Unit 1 PSC Verified

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 177

Department of

Computer Science and Engineering

UNIT 1
INTRODUCTION TO COMPUTERS

School of Computing
Vel Tech Rangarajan Dr. Sagunthala R&D Institute of
Science and Technology
Agen
da

Problem Solving
Compilers Technique:
• Interprete Memory, Programming
Overview
Introducti rs and Variables, Logic,
of Number
on of Values, Sequence,
operating programm system Selection and
computer Instruction Repetition,
systems ing s. Flow chart-
Algorithm -
languages Pseudocode.
Introduction of
Computer
Our society depends upon computer systems and the people
who develop and maintain them.

3
What is Computer?

1/31/20 Department of Computer Science and 4


25 Engineering
Department of Computer Science and 5
Engineering
Basic operation of the Computer

1/31/20 Department of Computer Science and 6


25 Engineering
Basic operation of the Computer

1/31/20 Department of Computer Science and 7


25 Engineering
Components of a Computer System

Every computer system has the following three basic components:


1. Input unit
2. Central processing unit
3. Output unit

1/31/20 Department of Computer Science and 9


25 Engineering
HARDWARE VS SOFTWARE

1/31/20 Department of Computer Science and 1


25 Engineering 0
HARDWARE
Computer hardware includes the physical parts of a computer:

• Computer hardware is the physical components that a computer system requires


to function. It encompasses everything with a circuit board that operates within a
PC or laptop; including the motherboard, graphics card, CPU (Central Processing
Unit), ventilation fans, webcam, power supply, and so on.
• Central processing unit (CPU), monitor, mouse, keyboard
• Computer data storage, graphics card, sound card, speakers and motherboard.

1/31/20 Department of Computer Science and 1


25 Engineering 1
SOFTWARE
• Software is a collection of instructions and data that tell a
computer how to work. This is in contrast to physical hardware,
from which the system is built and actually performs the work

1/31/20 Department of Computer Science and 1


25 Engineering 2
Characteristics of Computer

1/31/20 Department of Computer Science and 1


25 Engineering 3
Characteristics of Computer
Diligence
• A computer can perform millions of tasks or calculations with the same
consistency and accuracy
• It doesn’t feel any tiredness or lack of concentration
• Its memory also make it superior to that of human being.

1/31/20 Department of Computer Science and 1


25 Engineering 4
Characteristics of Computer

Speed
• A computer works with much higher speed and accuracy compared to humans while
performing mathematical calculations.
• Computers can process millions (1,000,000) of instructions per second.
• The time taken by computers for their operations is microseconds and nanoseconds.

1/31/20 Department of Computer Science and 1


25 Engineering 5
Characteristics of Computer

Accuracy
• Computers perform calculations with 100% accuracy
• Errors may occur due to data inconsistency or inaccuracy

1/31/20 Department of Computer Science and 1


25 Engineering 6
Characteristics of Computer

Versatility
• It refers to the capability of a computer to perform different kinds of works with same
accuracy and efficiency
• For example at a moment a computer can be used to draft a letter. Next moment it can be
used print a document or play a music file etc..,

1/31/20 Department of Computer Science and 1


25 Engineering 7
Characteristics of Computer

1/31/20 Department of Computer Science and 1


25 Engineering 8
Characteristics of Computer

Automation
• Computer perform all the tasks automatically i.e. it performs tasks without
manual intervention.

1/31/20 Department of Computer Science and 1


25 Engineering 9
Characteristics of Computer

Storage
• A computer has built-in memory called primary memory where it stored data.
• Secondary storage are removable devices such as CD, pen drives, etc., which are
also used to store data.

1/31/20 Department of Computer Science and 2


25 Engineering 0
Application of Computer
Computers play a role in every field of life. They are used in business, educational
institutions, research organizations, medical field, government offices,
entertainment, sports etc.

1/31/20 Department of Computer Science and 2


25 Engineering 1
Evolution of Computer

1/31/20 Department of Computer Science and 2


25 Engineering 2
Generation of Computer

1/31/20 Department of Computer Science and 2


25 Engineering 3
First Generation Computers (1956-1963) - Vacuum Tubes

• These computers were vacuum tube based machines.


• They used magnetic drums for memory.
• Input were fed into the computer using Punched cards
• The size of these computers were very large and it produce more heat.
• They were more expensive.
• 5000 additions and 350 multiplications/sec
• Ex: ENIAC, EDVAC, UNIVAC1

1/31/20 Department of Computer Science and 2


25 Engineering 4
Second Generation Computers (1959- 1965)-
Transistors
• Here the Transistor replaced the bulky vacuum tubes.
• Transistors are smaller than vacuum tubes and have higher operating speed.
• Thus the size of the computer got reduced considerably.
• Manufacturing cost was also very low.
• Ex: IBM7090, CDC3600

1/31/20 Department of Computer Science and 2


25 Engineering 5
Third Generation Computers (1964-1975)- Integrated
Circuit

• These computers were based on Integrated Circuits (ICs) Technology.


• A single IC has many transistors, registers and capacitors built on a single thin slice of silicon.
• So that the size of the computer got further reduced.
• These Computers were small in size, low cost, large memory and processing speed is very high.

1/31/20 Department of Computer Science and 2


25 Engineering 6
Fourth Generation Computer (1975- 1989)-
Microprocessors

• It uses very large scale Integrated Circuits (VLSI) built on a single silicon chip
called microprocessors .
• These computers are called microcomputers.
• Thus the size of the computer got reduced.
• The personal computer (PC) are comes under the Fourth Generation

1/31/20 Department of Computer Science and 2


25 Engineering 7
Fifth Generation Computer( 1989-present) Artificial
Intelligence

• The speed is extremely high in fifth generation computer.


• The concept of Artificial intelligence has been introduced to allow the computer to
take its own decision (self-reliant).
• It is still in a developmental stage

1/31/20 Department of Computer Science and 2


25 Engineering 8
Generation of Computer

1/31/20 Department of Computer Science and 2


25 Engineering 9
Classification of Computer

1/31/20 Department of Computer Science and 3


25 Engineering 0
Basic Computer Organisation

1/31/20 Department of Computer Science and 3


25 Engineering 1
Department of Computer Science and 3
Engineering 2
Department of Computer Science and 3
Engineering 3
Department of Computer Science and 3
Engineering 4
Primary Memory
Primary memory is computer memory that a processor or computer
accesses first or directly. It allows a processor to access running
execution applications and services that are temporarily stored in a
specific memory location.
Primary memory is also known as primary storage or main memory.

As soon as a computer starts, primary memory loads all running


applications, including the base operating system (OS), user
interface and any user- installed and running software utility. A
program/application that is opened in primary memory interacts
with the system processor to perform all application-specific
tasks.

Department of Computer Science and 3


Engineering 5
Difference Between RAM and ROM
Cache
memory
• Cache memory is an extremely fast memory type that acts as a buffer between RAM and the
CPU.
• It holds frequently requested data and instructions so that they are immediately available to the
CPU when needed.
• Cache memory is used to reduce the average time to access data from the Main memory.
Department of Computer Science and 38
Engineering
Department of Computer Science and 39
Engineering
ALU (Arithmetic Logic
Unit)
• This unit consists of two subsections namely,
• Arithmetic Section
• Logic Section

Arithmetic Section
• Function of arithmetic section is to perform arithmetic operations like
addition, subtraction, multiplication, and division.
• All complex operations are done by making repetitive use of the above
operations.

Logic Section
• Function of logic section is to perform logic operations such as comparing,
selecting, matching, and merging of data.
OUTPUT

DEVICES
An output device is any piece of computer hardware equipment which converts
information into human-readable form. It can be text, graphics, tactile, audio, and video.
The commonly used output devices are:
What is an Operating system
• An Operating System (OS) is a software that acts as an interface between computer hardware
components and the user.
• Every computer system must have at least one operating system to run other programs.
Applications like Browsers, MS Office, Notepad Games, etc., need some environment to run
and perform its tasks.

1/31/20 Department of Computer Science and 4


25 Engineering 2
Examples of Operating system

1/31/20 Department of Computer Science and 4


25 Engineering 3
Compilers
• A compiler is a computer program that translates computer code written in one
programming language (the source language) into another language (the target
language).

• The name "compiler" is primarily used for programs that translate source code from a
high-level programming language to a lower level language (e.g. assembly language,
object code, or machine code) to create an executable program

1/31/20 Department of Computer Science and 4


25 Engineering 4
How compiler works

1/31/20 Department of Computer Science and 4


25 Engineering 5
Interpreter
• An interpreter is a computer program that translates and executes instructions written in a
computer programming language line-by line, unit by unit etc.,
• An interpreter needs to be able analyze, or parse, instructions written in the source
language.

1/31/20 Department of Computer Science and 4


25 Engineering 6
How Interpreter works

1/31/20 Department of Computer Science and 4


25 Engineering 7
Interpreter vs
Compiler
Compiler vs Interpreter
Interpreter Compiler

Scans the entire program and


Translates program one translates it as a whole into
statement at a time. machine code.

Interpreters usually take less Compilers usually take a large


amount of time to analyze the amount of time to analyze the
source code. However, the source code. However, the
overall execution time is overall execution time is
comparatively slower than comparatively faster than
compilers. interpreters.

Generates Object Code which


No Object Code is generated, further requires linking, hence
hence are memory efficient. requires more memory.

Programming languages like


JavaScript, Python, Ruby use Programming languages like C,
interpreters. C++, Java use compilers.

1/31/20 Department of Computer Science and 4


25 Engineering 9
Department of Computer Science and 5
Engineering 0
NUMBER
SYSTEM
• Number System
• A number system is defined as a system of writing to express numbers. It is
the mathematical notation for representing numbers of a given set by
using digits or other symbols in a consistent manner. It provides a
unique representation of every number and represents the
arithmetic and algebraic structure of the figures.
• A computer is a digital system that stores and process data in the form
of 0’s and 1’s(Binary/Machine language)
• Hence there is a need to change the data in the form of 0’s &1’s
• Positional Number System
• Numbers are represented using some symbols called digits and the
values of these no’s can be determined by taking the position of
digits
• Catagories:
• Decimal System
• Binary System
• Hexa Decimal System
• Octal System
Number
System
• Number Base B => B symbols
• Base 16(Hexa):0, 1,……9, A ,B,C,D,E,F
• Base 10 (Decimal): 0, 1, 2,……, 7, 8, 9
• Base 8(Octal): 0, 1, 2, 3, 4,
5, 6, 7
• Base 2 (Binary): 0, 1
NUMBER
SYSTEM ….
Decimal to Other Bases

DECIMAL TO BINARY (Base 2)

1/31/20 Department of Computer Science and 5


25 Engineering 4
Decimal to Other Bases

DECIMAL TO OCTAL (Base 8)

1/31/20 Department of Computer Science and 5


25 Engineering 5
Decimal to Other Bases

DECIMAL TO HEXA DECIMAL


(Base 16)

1A
7

021
C

1/31/20 Department of Computer Science and 5


25 Engineering 6
Binary to Other Bases

BINARY TO DECIMAL (BASE 10)

1A
7

1/31/20 Department of Computer Science and 5


25 Engineering 7
Binary to Other Bases

BINARY TO OCTAL (BASE 8)

1/31/20 Department of Computer Science and 5


25 Engineering 8
Binary to Other Bases

BINARY TO HEXADECIMAL (BASE


16)

1/31/20 Department of Computer Science and 5


25 Engineering 9
Octal to Other Bases

OCTAL TO DECIMAL (BASE 10)

1/31/20 Department of Computer Science and 6


25 Engineering 0
Octal to Other Bases

OCTAL TO BINARY (BASE 2)

1/31/20 Department of Computer Science and 6


25 Engineering 1
Octal to Other Bases

OCTAL TO HEXADECIMAL (BASE


16)

1/31/20 Department of Computer Science and 6


25 Engineering 2
Hexadecimal to Other Bases

HEXADECIMAL TO DECIMAL (BASE


10)

1/31/20 Department of Computer Science and 6


25 Engineering 3
Hexadecimal to Other Bases

HEXADECIMAL TO BINARY (BASE 2)

1/31/20 Department of Computer Science and 6


25 Engineering 4
Hexadecimal to Other Bases

HEXADECIMAL TO OCTAL (BASE 8)

1/31/20 Department of Computer Science and 6


25 Engineering 5
Memory

• Memory refers to the processes that are used to


acquire, store, retain, and later retrieve information

1/31/20 Department of Computer Science and 6


25 Engineering 6
Types of Memory

1/31/20 Department of Computer Science and 6


25 Engineering 7
Storage

1/31/20 Department of Computer Science and 6


25 Engineering 8
Memory, Variables, Value

1/31/20 Department of Computer Science and 6


25 Engineering 9
Instruction

• Computer instructions are a set of machine language


instructions that a particular processor understands and
executes.

• A computer performs tasks on the basis of the instruction


provided
1/31/20 Department of Computer Science and 7
25 Engineering 0
Variables
in C
Topics

• Naming Variables
• Declaring Variables
• Using Variables
• The Assignment Statement

Reading

• Sections 2.3 - 2.4


What Are Variables
• Variables in C have the same meaning as variables in
in C?That is, they represent some unknown, or
algebra.
variable, value.

x=a+b
z + 2 = 3(y - 5)

• Remember that variables in algebra are represented by a


single alphabetic character.
Naming
• Variables in C may be given representations containing
Variables
multiple characters. But there are rules for these
representations.
• Variable names in C
• May only consist of letters, digits, and underscores
• May be as long as you like, but only the first 31 characters
are
significant
• May not begin with a number
• May not be a C reserved word (keyword)
Reserved Words
(Keywords) in C
• auto break int long
• case char regist retur
• const continue er n
• default do shor
• double else t static
• enum extern signe switc
• float d h
for sizeof union
• goto struct void
if typedef while
Naming
• C programmers generally agree on the following
Conventions
conventions for naming variables.
• Begin variable names with lowercase letters
• Use meaningful identifiers
• Separate “words” within identifiers with underscores or mixed
upper and lower case.
• Examples: surface_Area
surfaceArea
surface_area
• Be consistent!
Naming Conventions
• Use all uppercase for symbolic constants (used
in(con’t)
#define preprocessor directives).
• Examples:

#define PI 3.14159
#define AGE 52
Case
• CSensitivity
is case sensitive
• It matters whether an identifier, such as a variable name, is
uppercase or lowercase.
• Example:
area
Area
AREA
ArEa
are all seen as
different
variables by
the compiler.
Which Are Legal
Identifiers?
AREA area_under_the_curve
3D num45
Last-Chance #values
x_yt3 pi
num$ %done
lucky***
Declaring Variables
• Before using a variable, you must give the compiler
some information about the variable; i.e., you
must declare it.
• The declaration statement includes the data type
of the variable.
• Examples of variable declarations:
int meatballs ;
float area ;
Declaring Variables
• When we declare a variable
(con’t)
• Space is set aside in memory to hold a value of the specified
data type
• That space is associated with the variable name
• That space is associated with a unique address
• Visualization of the declaration
int meatballs ; float num;

meatballs

garbage

FE07
More About
C has three basic predefined data types:
Variables
• Integers (whole numbers)
• int, long int, short int, unsigned int
• Floating point (real numbers)
• float, double
• Characters
• char
• At this point, you need only be concerned with the data
types that are bolded.
Using Variables:
• Variables may be given initial values, or initialized, when
Initialization
declared. Examples:

length
int length = 7 ; 7

diamet
float diameter = 5.9 ; er 5.9

initial
char initial = ‘A’ ;
‘A’
Using Variables:
Initialization
(con’t)
• Do not “hide” the initialization
• put initialized variables on a separate line
• a comment is always a good idea
• Example:
int height ; /* rectangle height */
int width = 6 ; /* rectangle width
int area ; */
/* rectangle area */
NOT int height, width = 6,
area ;
Using Variables:
• Variables may have values assigned to them through the
Assignment
use of an assignment statement.
• Such a statement uses the assignment operator =
• This operator does not denote equality. It assigns
the value of
the right hand side of the statement (the
expression) to the variable on the left hand side.
• Examples:
diameter = 5.9 ;
area = length * width ;
Note that only single variables may appear on the left
hand side of the assignment operator.
Example: Declarations and
Assignments
#include <stdio.h> inche
sgarbage
int main( ) fee
tgarbage
{
fathom
int sgarbage
inches, feet,
fathoms
fathoms ; = 7 ; fathom
s 7
feet = 6 * fathoms ; fee
inches = 12 * feet ; t 42
inche

s 504


Example: Declarations and
Assignments (cont’d)



printf (“Its depth at sea: \
n”)(“; %d fathoms \n”, fathoms) ;
printf
printf (“ %d feet \n”, feet) ;
printf (“ %d inches \n”, inches) ;

return 0 ;
}
Enhancing Our Example
• What if the depth were really 5.75 fathoms? Our
program, as it is, couldn’t handle it.
• Unlike integers, floating point numbers can contain
decimal portions. So, let’s use floating point,
rather than integer.
• Let’s also ask the user to enter the number of
fathoms, rather than “hard-coding” it in.
How the instruction executes?

• The instruction execution takes place in the CPU registers.


• The CPU reads (fetches) instructions (codes) from the memory one at a time, and executes or
performs the operation specified by this. Instruction fetch involves reading of an instruction
from a memory location to the CPU register.

1/31/20 Department of Computer Science and 8


25 Engineering 8
Programs
• A computer program is a collection of instructions
that can be executed by a computer to perform
a specific task.
• The program is submitted to the computer.

1/31/202
COMPUTER SCIENCE 8
5
Problem Solving Techniques
Problem solving arriving
at decisions based prior
knowledge and reasoning.

COMPUTER SCIENCE 90
1/202
Problem Solving Techniques

COMPUTER SCIENCE 91
1/202
Problem Solving Techniques

Problem Working General


Getting The use of Similarities backwards problem
definition started on specific among from the solving
phase a problem examples problems solution strategies
1. Problem Definition Phase
• Understand the problem
• Work out what must be done rather than how to do it
• Lot of care must be taken in working our what must
be done
• Example
1. Finding the square root
2. Finding the greatest common divisor
• From the definition develop an algorithm
2. Getting started on a problem
• There may be many ways to solve the problem and also many
solutions to most problems.
• Have an idea of where to start the problem
• Try to understand the problem clearly and do not worry
about implementation
• Develop implementation-independent solution
• Postpone coding
• Sooner you start coding your program the longer it is going to
take
3.The use of specific examples

• Pick on a specific example of the general problem we are trying


to solve
• Use some properties to try to get a solution for the problem
• Apply the mechanism to solve the problem
Example : Find out the maximum number from the given set of
numbers
• Use some geometrical or schematic diagrams representing
certain aspects of the problem
4. Similarities among problems
• See if there are any similarities between the current
problem and other problems that we have
solved or we have seen solved
• Try to solve the problem independently
5. Working Backwards from the
Solution
6. General Problem-Solving Strategies
Divide and Conquer
Dynamic Programming

 Divide and Conquer:


• It is defined as one large complex problem is divided
into number of sub problems and finds the solution. The sub
problem solutions are combined to form the solution for
large problem.

• Example: MERGE SORT


6. General Problem-Solving Strategies
 Dynamic programming:
• Dynamic programming is an algorithm that can be used when the
solution to a problem can be viewed as the result of a sequence of
decisions.
• Example: TRAVELLING SALESMAN PROBLEM
 Backtracking: During the search if
infeasible solution is
sensed then backtrack to previous node.
• Example: Sudoku, 8-Queens Problem
 Branch and Bound:
• Branch- splitting procedure
• Bound- computes upper and lower bounds
• Example: Knapsack problem
 Greedy method: Find the feasible solution
from the set of solution for the given problem
• Example: job scheduling
Programming Logic
• Programming structures broken down into three
are simple
structures called
 Sequence structure
 Selection structure
 Repetition structure

COMPUTER SCIENCE 100


1/202
Programming Logic

COMPUTER SCIENCE 101


1/202
Sequence Control Structure

1/31/202 COMPUTER SCIENCE


Sequence Control Structure
• The instructions are computed in sequence of actions that is
completed in a specific order.

1/31/202 COMPUTER SCIENCE


Sequence Control Structure

1/31/202 COMPUTER SCIENCE


Sequence Control Structure

1/31/202 COMPUTER SCIENCE


Representation of statements in the sequential
structure

Flowchart Pseudocode
Selection Structure-if then
Selection Structure. Used to make a decision
or comparison and then, based on the result
of that decision or comparison, to select one
of two paths. The condition must result in
either a true (yes) or false (no) answer. If the
condition is true, the program performs one
set of tasks.

Ex:
If ..Then
If ..Then.. Else
Case Type
Selection Structure-if then

If ..Then
If ..Then.. Else
Case Type
Selection Structure-if
then
Selection Structure-if
then
If-
ThenIfThen
condition is True , the statements following
are executed

Flowchart
Pseudocode
Exam
ple
Start

Read a

yes
If a>0
n
Print a is Positive o

Stop
Department of Computer Science and 11
Engineering 2
Selection Structure-if-
then-else
Selection Structure-if-
then-else
If-Then-Else

Flowcha Pseudocod
rt e
Exam
ple Start

Read a,b

yes
If a>b
no
Print a is Greater
Print b is Greater

Stop
Biggest of 3 numbers
CASE
STRUCTURE
Case Structure
Syntax
Pseudocode Flowchart
Example: Finding the Grade
start
Read
m1,m2,m3
Avg=(m1+m2+m3
)/3
If Avg>=60 Print
First
Class
If Print
Avg>= Second
50 Class
Print
If Third
Avg>= Class
35
Fail

12
stoDpepartment of Computer Science and 0
Repetition
structure
Repetition
structure
Repetition
structure
• Repetition structures are used to repeat statements or
blocks of code.

• The decision whether to repeat the code is based on the


evaluation of a logical expression.

• If the expression is true, the code is executed. If false, the


code is not executed.

• WHILE loop

• Do…WHILE loop

• For loop
While Loop and Do-
WHILE
WHILE Loop Do-
Loo
WHILE
Loop p
While
Loop
• A while loop is one of the most common types of loop.
• The main characteristic of a while loop is that it will repeat a set of
instructions based on a condition.
• As far as the loop returns a boolean value of TRUE, the code inside it will keep
repeating.
• We use this kind of loop when we don't know the exact number of times a
code needs to be executed.

pseudocod Flowcha
e rt
While
Loop
• A while loop is one of the most common types of loop.
• The main characteristic of a while loop is that it will repeat a set of
instructions based on a condition.
• As far as the loop returns a boolean value of TRUE, the code inside it will keep
repeating.
• We use this kind of loop when we don't know the exact number of times a
code needs to be executed.

pseudocod
e
Do-WHILE Loop
• In the Do-While structure, the statement given in the Do block are executed
atleast once and after that repeatedly executed until the given condition is true.
• In a do…while loop, the condition is always executed after the body of a loop. It
is also called an exit-controlled loop.
• At each instance of execution of block statements, the condition is checked.
• If the condition is true, then only block statement are executed; otherwise the
repetition structure is terminated.

pseudocode Flowchart
Do-WHILE Loop
• In the Do-While structure, the statement given in the Do block are executed
atleast once and after that repeatedly executed until the given condition is true.
• In a do…while loop, the condition is always executed after the body of a loop. It
is also called an exit-controlled loop.
• At each instance of execution of block statements, the condition is checked.
• If the condition is true, then only block statement are executed; otherwise the
repetition structure is terminated.

pseudocode Flowchart
For Loop
• In a for loop, the initial value is performed only once, then the condition tests
and compares the counter to a fixed value after each iteration, stopping the for
loop when false is returned.
For Loop
Problem Solving in
computer
Problems that can be solved through a computer may range in
size and complexity.

The major steps that we need to follow for solving a problem :


1. Developing algorithm
2. Drawing Flowchart
3. Writing Pseudo code
Algorithm

1/31/202 COMPUTER SCIENCE


Algorithm - Example

1/31/202 COMPUTER SCIENCE


Algorithm - Example

1/31/202 COMPUTER SCIENCE


Algorithm – Put the book in the box

1/31/202 COMPUTER SCIENCE


Algorithm - Example

1/31/202 COMPUTER SCIENCE


Algorithm - Example

1/31/202 COMPUTER SCIENCE


Definition Algorithm
• A set of steps to accomplish a task
• An algorithm describes the step by step actions to solve a problem.
• An algorithm has a well defined sequence of steps, it gives you an
output, and it will eventually terminate.
• An algorithm is a sequence of computational steps that transform the
input into the output.
Definition Algorithm
• An algorithm is a step by step procedure or instructions
to solve a problem. A procedure is a finite sequence of
instructions, where each is carried out in a finite amount
of time.
• Every problem can be solved with the help of an
algorithm. For example, when the user wants to login to a
Facebook account, first he has to go to Facebook.com.
Then he has to give the correct username and password.
Then he has to click the login button. If the username and
password are correct, the user can enter his account.
Likewise, every problem has a sequence of steps to solve
it. This is also an algorithm because it provides a correct
sequence of steps to solve the problem.
Definition Algorithm
• When writing programs, it is important to identify the
algorithm for the program. For example, to add
two numbers, first sum variable is initialized to 0.
Then two numbers are entered. Then, the addition is
stored to the sum variable. Finally, the sum is
printed. That is the algorithm to add two
numbers.
Algorithm - Example

1/31/202 COMPUTER SCIENCE


Algorithms for Addition of two
numbers
Example:
Step1: Start
Step2: Read a, b
Step3: add two numbers
Step4: Display the value of c
Step5: Stop
Definition Flow
chart
• The flowchart is a diagram which visually presents the flow of data through
processing systems.
• This means by seeing a flow chart one can know the operations performed
and the
sequence of these operations in a system.

A flow chart can be used for representing an algorithm.

Flowchart Symbols
• There are 6 basic symbols commonly used in flowcharting of Programs:
1. Terminal
2. Process
3. input/output
4. Decision
5. Connector
6. Predefined Process.
Flowchart Symbols
Connector Symbol
Flowchart to calculate the sumof two
numbers.
Example:
Advantage and Disadvantage of Flowchart
Defining the pseudocode

• Pseudocode is a simple way of writing programming code in English.


• Pseudocode is not actual programming language.
• It uses short phrases to write code for programs before you actually create it in
a specific language.
• Once you know what the program is about and how it will function, then you
can use pseudocode to create statements to achieve the required results for
your program.
Defining the pseudocode

• Pseudocode is an informal way of writing a program. It is


not exactly a computer program. It represents the
algorithm of the program in natural language and
mathematical notations. Usually, there is no particular
code syntax to write a pseudocode. Therefore, there is no
strict syntax as a usual programming language. It uses
simple English language.

• It is a simpler version of a programming code in


plain English which uses short phrases to write code
for a program before it is implemented in a specific
programming language.
Guidelines for Preparing
Pseudocode
• Pseudocode will be enclosed by START (or BEGIN) and STOP (or END).
• Pseudocode should be concise so ignore unnecessary details.
• To accept data from user, generally used statements are INPUT, READ,
GET or OBTAIN.
• To display result or any message, generally used statements are PRINT,
DISPLAY, or WRITE.
• Generally used keywords are capitalized while preparing pseudocode.
Advantage and
Advantage:
Disadvantage
• It allows the designer to focus on main logic without being distracted by
programming languages syntax.
• Since it is language independent, it can be translated to any computer language
code.
• It allows designer to express logic in plain natural language.
• It is easier to write actual code using pseudocode.
• Unlike algorithms, pseudocodes are concise so pseudocodes are more readable and
easier to modify.
Disadvantage:
• There are no accepted standards for writing pseudocodes and designer use their
own style while writing pseudocodes.
• Pseudocode cannot be compiled and executed so its correctness cannot be verified
by using computers.
Pseudocode to add two
numbers
Example:
BEGIN
Get the value from the user(a,b)
c=a+b
print c
END
Algorithm for sequence
structure To find the
Average of 3 numbers
• Step 1: Start
• Step 2: Read the value a, b, c
• Step 3: Calculate sum <- a+b+c
• Step 4: Calculate avg <- sum/3
• step 5: Display sum & avg
• Step 6: Stop
Algorithm for sequence
structure To display the
area of circle

• Step 1: start
• Step 2: Read the value r
• Step 3: calculate area of the circle
area=3.14*r*r
• step 4: Display area
• step 5: Stop
Algorithm for sequence
structure To display the
square root of a number
• Step 1: Start
• Step 2: Get a number from the user (n)
• Step 3: calculate square root of n, result=sqrt(n)
• Step 4: Display result
• step 5: stop
Algorithm for selection structure
To find whether the given number is even
or odd
• Step 1: Start
• Step 2: Read the value Number
• Step 3: Check: If Number%2 == 0 then goto step 4
else goto step 5
• Step 4: Display "N is an Even Number" goto step 6
• Step 5: Display "N is an Odd Number”
• Step 6: stop
Algorithm for selection structure

Find out whether a given number is positive


or negative
• Step 1: Start
• Step 2: Get the value from the user (n)
• Step 3: Check if n is greater than 0 (n>0) then goto
5 else gotostep
step 6
• Step 5: Display "n is positive number" goto step 7
• Step 6: Display " n is negative number"
• Step 7: Stop
Algorithm for Repetition structure

Find sum of first n numbers (1+2+…+n)


• Step 1: Start
• Step 2: Read the value of n
• Step 3: Initialize sum=0, i=1
• Step 4: Repeat Steps 5-6 until i<=n, else goto
Step7
• Step 5: compute sum=sum+i
• Step 6: Increment i by 1
• Step
• Step7:
8:Display sum
Stop
Algorithm for Repetition structure
To find the sum of all odd numbers from 1
to 100

• Step1: Start
• Step2: Initialize i=1, sum=0
• Step3: Repeat Steps 4-5 until i<=100 is true, else
goto Step6
• Step4: Compute sum=sum+i
• Step5: Increment i=i+2
• Step6: Display the value of sum
• Step7:
Stop
Pseudocode for sequence
structure Display square
root of a number

BEGIN
READ: num
result=SQRT(num)
DISPLAY: result
END
Pseudocode for sequence
structure To find the area
of the circle
BEGIN
READ: radius
area=3.14*radius*radius
DISPLAY: area
END
Pseudocode for sequence
structure To find the
average of 3 numbers
BEGIN
READ: m1, m2, m3
average=(m1+m2+m3)/3
DISPLAY: average
END
Pseudocode for selection structure
To find the given number is positive
or negative
BEGIN
Get the value from the user (num)
if num>0
DISPLAY: “number
is positive”
ELSE
DISPLAY: “Number
is a negative”
END
END IF
Pseudocode for selection
structure To find the given
number is odd or even
BEGIN
READ: num
IF num%2==0
DISPLAY: “number is
even”
ELSE
DISPLAY: “Number is
END a odd”
END IF
Pseudocode for Repetition
structure Pseudocode for sum of first n
numbers (1+2+…+n)
BEGIN
Read the value (n)
Set sum=0, i=1
while i<=n
sum=sum+i
i=i+1
end while
Print sum
End
Pseudocode for Repetition structure
Pseudocode the sum of all odd numbers
from 1 to 100.

Begin
Set i=1, sum=0
While i<=100
sum=sum+i
i=i+2
End while
Print sum
End
Flowchart to Print your name 5 times
Print the natural number up to 10
Print the odd natural number up
to 100
Print the even natural number up to 100
Sum of the series of 1+2+3+---n
print the sum of all odd numbers from 1 to 100
Thank
You

You might also like