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

Algorithm Flow Notes

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

Algorithm Flow Notes

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

INTRODUCTION TO COMPUTERS

Any programming language is implemented on a computer. Right form its inception, to the present
day, all computer system (irrespective of their shape & size) performs the following 5 basic
operations. It converts the raw input data into information, which is useful to the users.

➢ Input: It is the process of entering data & instructions to the computer system.
➢ Storing: The data & instructions are stored for either initial or additional processing,
as & when required.
➢ Processing: It requires performing arithmetic or logical operation on the saved data
to convert it into useful information.
➢ Output: It is the process of producing the output data to the end user.
➢ Controlling: The above operations have to be directed in a particular sequence to
be completed.

Based on these 5 operations, we can sketch the block diagram of a computer.

Fig 1: Block Diagram of a Computer


➢ Input Unit: We need to first enter the data & instruction in the computer system,
before any computation begins. This task is accomplished by the input devices.
(Eg: keyboard, mouse, scanner, digital camera etc). This device is responsible for
linking the system with the external environment. The data accepted is in a human
readable form. The input device converts it into a computer readable form.

➢ Storage Unit: The data & instruction that are entered have to be stored in the
computer. Similarly, the end results & the intermediate results also have to be
stored somewhere before being passed to the output unit. The storage unit
provides solution to all these issues. This storage unit is designed to save the initial
data, the intermediate result & the final result. This storage unit has 2 units:
Primary storage & Secondary storage.

Primary Storage: The primary storage, also called as the main memory, holds the data when the
computer is currently on. As soon as the system is switched off or restarted, the information held
in primary storage disappears (i.e. it is volatile in nature). Moreover, the primary storage normally
has a limited storage capacity, because it is very expensive as it is made up of semiconductor
devices.

Secondary Storage: The secondary storage, also called as the auxiliary storage, handles the
storage limitation & the volatile nature of the primary memory. It can retain information even when
the system is off.

➢ Central Processing Unit: Together the Control Unit & the Arithmetic Logic Unit are
called as the Central Processing Unit (CPU). The CPU is the brain of the computer.
Like in humans, the major decisions are taken by the brain itself & other body parts
function as directed by the brain. Similarly in a computer system, all the major
calculations & comparisons are made inside the CPU. The CPU is responsible for
activating & controlling the operation of other units of the computer system.

Arithmetic Logic Unit: The actual execution of the instructions (arithmetic or logical operations)
takes place over here. The data & instructions stored in the primary storage are transferred as &
when required.

Control Unit: This unit controls the operations of all parts of the computer but does not carry out
any actual data processing.It is responsible for the transfer of data and instructions among other
units of the computer.It manages and coordinates all the units of the system.It also communicates
with Input/Output devices for transfer of data or results from the storage units.
➢ Output Unit: The job of an output unit is just the opposite of an input unit. It accepts
the results produced by the computer in coded form. It converts these coded
results to human readable form. Finally, it displays the converted results to the
outside world with the help of output devices ( Eg :monitors, printers, projectors
etc..).

So when we talk about a computer, we actually mean 2 things:

➢ Hardware- This hardware is responsible for all the physical work of the computer.
➢ Software- This software commands the hardware what to do & how to do it.

Together, the hardware & software form the computer system.

This software is further classified as system software & application software.

System Software- System software are a set of programs, responsible for running the computer,
controlling various operations of computer systems and management of computer resources.
They act as an interface between the hardware of the computer & the application software. E.g.:
Operating System

Application Software- Application software is a set of programs designed to solve a particular


problem for users. It allows the end user to do something besides simply running the hardware.
E.g.: Web Browser, Gaming Software, etc.
Explain about Algorithm ?
or
Define Algorithm and State Properties of it ?

Algorithm :
Algorithm is a finite Step by step instructions to solve a problem. It is written
in normal English.

Advantages and disadvantages of algorithm

Advantages of Algorithms:
• It provides the core solution to a given problem. The solution can be implemented on a
computer system using any programming language of user’s choice.
• It facilitates program development by acting as a design document or a blue print of a given
problem solution.
• It ensures easy comprehension of a problem solution as compared to an equivalent
computer program.
• It eases identification and removal of logical errors in a program.
• It facilitates algorithm analysis to find out the most efficient solution to a given problem.

Dis advantages of Algorithms:


• In large algorithms the flow of program control becomes difficult to track.
• Algorithms lack visual representation of programming constructs like flowcharts;
• Thus understanding the logic becomes relatively difficult.

Examples

Example1 : To Add two numbers.


Step 1: Start
Step 2: Read 2 numbers as A and B
Step 3: Add numbers A and B and store
result in C
Step 4 : Display C
Step 5: Stop

Example2: To find Average of 3 numbers.


1. Start
2. Read the numbers a , b , c
3. Compute the sum and divide by 3
4. Store the result in variable d
5. Print value of d
6. End

Example3: Average of n inputted numbers.


1. Start
2. Read the number n
3. Initialize i to zero
4. Initialize sum to zero
5. If i is greater than n
6. Read a
7. Add a to sum
8. Go to step 5
9. Divide sum by n & store the result in avg
10. Print value of avg
11. End

Explain about a Flowchart?

A flowchart is a visual representation of the sequence of steps for


solving a problem. A flowchart contains set of symbols that indicate various
operations in the program. For every process, there is a corresponding symbol in
the flowchart . Once an algorithm is written , its pictorial representation can be
done using flowchart symbols. In other words, a pictorial representation of a textual
algorithm is done using a flowchart.

A flowchart gives a pictorial representation of an algorithm.


The first flowchart is made by John Von Neumann in 1945.
It is a symbolic diagram of operations sequence, dataflow, control flow and processing
logic in information processing.
The symbols used are simple and easy to learn.
It is a very helpful tool for programmers and beginners.

Purpose of a Flowchart:
Provides Communication.
Provides an Overview.
Shows all elements and their relationships.
Quick method of showing Program flow.
Checks Program logic.
Facilitates Coding.
Provides Program revision.
Provides Program documentation.

Advantages of a Flowchart :
Flowchart is an important aid in the development of an algorithm itself.
Easier to Understand the Program itself.
Independent of any particular programming language.
Proper documentation.
Proper debugging.
Easy and Clear presentation.

Limitations of a Flowchart :
Complex logic.
Drawing is time consuming.
Difficult to draw and remember.
Technical detail.
Explain the Symbols in a Flowchart ?

Some of the common symbols used in flowcharts are shown below :

Fig: Flow chart Symbols

with flowchart , essential steps of an algorithm are shown using the shapes
above. The flow of data between steps is indicated by arrows or flow lines.

Write an algorithm and flow chart for swapping two numbers

a.To Swap two integer numbers a using third variable

Step 1 : Start
Step 2 : Input num1 , num2
Step 3 : temp = num1
Step 4 : num1 = num2
Step 5 : num2 = temp
Step 6 : Output num1 , num2
Step 7 : Stop

b.with out using third variable

Step 1 : Start
Step 2 : Input num1 , num2
Step 3 : calculate num1 = num1 + num2
Step 4 : calculate num2 = num1 – num2
Step 5 : calculate num1 = num1 – num2
Step 6 : Output num1 , num2
Step 7 : Stop

Flowcharts :

Write an algorithm and flowchart to find the largest among two numbers.
Ans:
Algorithm:

Step 1 : Start
Step 2 : Input A , B
Step 3 : if A > B then output A
else output B
Step4: Stop
Flowchart :

Write an algorithm and flowchart to find the largest among three numbers.

a.Algorithm: (method 1)
Step 1 : Start
Step 2 : Input A, B, C
Step 3 : if A > B go to step 4 , otherwise go to step 5
Step 4 : if A > C go to step 6 , otherwise go to step 8
Step 5 : if B > C go to step 7, otherwise go to step 8
Step 6 : print “ A is largest ” , go to step 9
Step 7 : print “ B is largest ”, go to step 9
Step 8 : print “ C is largest ” , go to step 9
Step 9 : Stop
Flowchart:

b.Algorithm:
. Step 1 : Start
Step 2 : Input A, B, C
Step 3 : Let max = A
Step 4 : if B > max then max = B
Step 5 : if C > max then max = C
Step 6 : output max is largest
Step 7 : Stop
Flowchart:

Write an algorithm and flowchart to find factorial of a


number Hint: fact (4) = 1 * 2* 3* 4

Algorithm:
Step 1: Start
Step 2: Input n
Step 3: Initialize counter variable, i , to 1 and factors = 1
Step 4: if i <= n go to step 5 otherwise go to step 7
Step 5: calculate factors = factors * i
Step 6: increment counter variable, i, and go to step 4
Step 7: output factors.
Step 8: stop
Flow chart:

Write an algorithm and flowchart to find whether a number is prime or not.

Hint: A number is said to be prime number for which the only factors are 1 and itself

Algorithm:
Step 1: Start
Step 2: Input n
Step 3: Let i = 1, count=0
Step 4: if i > n/2 go to step 7
Step 5: if (n % i = = 0) count = count + 1
Step 6: increment i and go to step 4
Step 7: if count=2 then print “prime number” else print “not prime
number”
Step 8: Stop

Flow chart:

Write an algorithm and flowchart to find reverse of a number.

Algorithm:
Step 1: Start
Step 2: Input n
Step 3: Let rev=0
Step 4: if n<=0 go to step 8
Step 5: digit = n % 10
Step 6: rev = (rev * 10) + digit
Step 7: n = n / 10 then go to step 4
Step 8: output rev
Step 9: Stop

Write an algorithm and flowchart to find the Sum of individual digits if a given number
Algorithm :-
Step 1 : Start
Step 2 : read n
Step 3 : Sum = 0
Step 4 : While n > 0 do
4.1 : r = n % 10
4.2 : Sum = Sum + r
4.3 : n = n/10
Step 5 : End while
Step 6 : Write Sum
Step 7 : Stop
Flowchart :-

You might also like