0% found this document useful (0 votes)
21 views20 pages

1626410776

Uploaded by

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

1626410776

Uploaded by

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

1

Algorithm:

An algorithm is a step-by-step problem solving procedure that can


be carried out by a computer.

Does algorithm depend on programming language? Justify your


answer.

No, an algorithm does not depend on the programming language.


Because, an algorithm expresses the steps sequentially for solving a
problem only, we can express the algorithm in our natural languages like
English etc.

Ex. 1. Write the algorithm to find the sum and product of two given
numbers.

Algorithm: To find the sum and product of two given numbers:


Step 1: Read A , B
Step 2: Let Sum= A+B
Step 3: Let Product=A*B
Step 4: Print Sum, Product
Step 5: Stop.

Ex 2: Develop an algorithm to interchange the values assigned to two


variables A and B. (For example, if A=2 and B=3, after interchange,
it should be A=3 and B=2).

Algorithm: To interchange the values.

1. [Initialize the variables]


A=2
B=3
2. [Peform the operations]
TEMP = A
A=B
B = TEMP
3. [Print the result]
Print A, B
4. [Finished]
Stop

Dr. Alok Das, Dept of Mathematics, J. N. College, Boko


2

Ex.3: Write an algorithm to find the largest number from 10 given


numbers.
Algorithm: To find the largest number from 10 given numbers.

1. [Initialize the variables]


large = 0
count = 0
2. [Increment the count]
count = count+1
3. [Enter number]
read x
4. [Perform the operations]
if (t>x) then
goto step 6
end if
5. large = x
6. if (count  10) then
goto step 2
end if
7. [Print the result]
Print large
8. [Finished]
Stop

Ex 4: Develop an algorithm to evaluate S using the relation:


S=1 + 4 + 9+ 16 + …..+ 100

Algorithm:
1. [Initialize the variables]
X=1
S=0
2. [Perform the operations]
Y = X*X
S=S + Y
X=X+1
3. [Check the condition]
If (X  100) Then
Goto step 2
End if
4. [Print the output]
Print S
5. [Finished]
Stop

Dr. Alok Das, Dept of Mathematics, J. N. College, Boko


3

Flow-chart?

(i) A flowchart is a diagrammatic representation of algorithm to


plan the solution to the problem.

(ii) Constructed by using special geometrical symbols where each


symbol represents an activity. The activity would be
input/output of data, computation/processing of data etc.

Advantages of using flowchart:


1. Communication: They are good visual aid to understand the
program.
2. Quicker grasps of relationships.
3. Effective analysis.
4. Synthesis
5. Proper program documentation
6. Efficient coding
7. Orderly debugging & testing of programs.
8. Efficient program maintenance.

Limitations of using flowchart:


1. Complex logic
2. Alterations & Modifications
3. Reproduction
4. No uniform practice
5. Link between conditions
6. Standardization

Flowchart symbols:

Start/Stop
Ellipse

Expression
Rectangle

Dr. Alok Das, Dept of Mathematics, J. N. College, Boko


4

I/O Statements

Parallelogram

Decision

Rhombus

Connecting lines (Flow lines)

Connector with label

Ex. Draw the flowchart to find the sum and product of two given
numbers.

Flowchart: To find the sum and product of two given numbers:

Start

Read A,B

Sum=A+B

Product=A*B

Print Sum, Product

Stop

Dr. Alok Das, Dept of Mathematics, J. N. College, Boko


5

Ex: Write the algorithm and draw the flowchart to find the sum and
product of two given numbers.

Solution:
Algorithm: To find the sum and product of two given numbers:
Step 1: Read A , B
Step 2: Let Sum= A+B
Step 3: Let Product=A*B
Step 4: Print Sum, Product
Step 5: Stop.

Flowchart: To find the sum and product of two given numbers:

Start

Read A,B

Sum=A+B

Product=A*B

Print Sum, Product

Stop

Ex: What are the advantages and limitations of flowcharts when


used to solve a problem?

Solution: Flowcharts have the following advantages when used to solve a


problem:
(a) They are brief and to the point.
(b) They express clearly the logic of a given procedure.
(c) They are unambiguous as there can be only one
direction of logic at any one time.
(d) They show readily whether all eventualities are
covered.
Limitations: Complex and detailed charts can be laborious to plan and
draw and the actions to be taken in specified situations can be difficult
when many decision paths are involved.

Dr. Alok Das, Dept of Mathematics, J. N. College, Boko


6

Ex: What is meant by algorithm? Write an algorithm to find and


print the smaller number from two given numbers.

Solution: An algorithm is a logical list of procedures or steps for solving


a given problem.

Algorithm: To find the smallest number from two given numbers:


1. [Enter numbers]
Read A, B
2. [Perform the operations]
If (A<B) then
Print smallest number is A
Else
Print smallest number is B
End If
3. [Finished]
Stop

Ex: Write an algorithm to compute the arithmetic mean of n


numbers.

Solution:
Algorithm: To compute the arithmetic mean of n numbers.
1. [Enter N]
Read N
2. [Enter numbers]
For I=1 to N
Read A[I]
End For
3. [Initialize the variables]
Sum=0
4. [Perform the operations]
For I=1 to N
Sum=Sum+A[I]
End For
AM=Sum/N
5. [Print the Output]
Print Sum, AM
6. [Finished]
Stop

Dr. Alok Das, Dept of Mathematics, J. N. College, Boko


7

Ex: What is an algorithm? What are its essential properties?


Solution:
Algorithm: An algorithm is a step-by-step problem solving procedure that
can be carried out by a computer. The essential properties of an algorithm
are:
1. It should be simple.
2. It should be clear with no ambiguity.
3. It should lead to a unique solution of the problem.
4. It should involve a finite number of steps to arrive at a solution.
It should have the capability to handle some unexpected situations
which may arise during the solution of a problem (for example,
division by zero).

Ex: What are the advantages and disadvantages of algorithm?


Solution:
Advantages of Algorithm:
1. It is a step by step solution to a given problem which is very easy
to understand.
2. It has got a definite procedure which can be exacted within a set
period of time.
3. It is easy to first develop an algorithm, then convert it into a
flowchart and then into a computer program.
4. It has got a beginning and an end within which there are definite
procedures to produce output(s) within a specified period of time.
5. It is easy to debug as every step has got its own logical sequence.
6. It is independent of programming language.

Disadvantages of Algorithm:
It is time consuming and cumbersome as an algorithm is developed first
which is converted into a flowchart and then into a computer program.

Ex: What do you meant by an algorithm? Discuss the main features


of an algorithm.

Solution:
An algorithm is a step of instructions to be followed to solve a
problem in computers.
According to D.E. Knuth, a pioneer in the computer science
discipline, an algorithm has five important features.

Dr. Alok Das, Dept of Mathematics, J. N. College, Boko


8

i) Finiteness: An algorithm terminates after a fixed number of


steps.
ii) Definiteness: Each step of the algorithm is precisely defined,
i.e. the actions to be carried out should be specified
unambiguously.
iii) Effectiveness: All the operations used in the algorithm are basic
(division, multiplication, comparison., etc.) and can be
performed exactly in fixed duration of time.
iv) Input: An algorithm has certain precise inputs, i.e. quantities
which are specified to it initially, before the execution of the
algorithm begins.
v) Output: An algorithm has one or more outputs, that is the
results of operations which have a specified relation to the
inputs.

Ex: Develop an algorithm to read a value of N and to compute and print


the value of N!

Algorithm: To read a value of N and to compute and print the value of


N!
1. [Enter number]
read N
2. [Check, if the number is negative]
if(N<0) then
stop
end if
3. [Perform the operation]
K=1
For L=2 to N
K=K*L
End For
4. [Print the result]
Print K
5. [Finished]
Stop

Dr. Alok Das, Dept of Mathematics, J. N. College, Boko


9

Ex: Does an algorithm depend on the programming language?

Solution: No, an algorithm does not depend on the programming


language.

Ex: What is a flowchart? Why is it called a flowchart?


OR
What is a flowchart?

Solution: A flowchart is a picture or a diagram which helps us define a


procedure for solving a problem. It is called a flowchart since it shows the
logical flow of operations in various steps.

Ex: What is the purpose of a flowchart?

Solution: A flowchart is a picture or a diagram which helps us define a


procedure one step at a time, and arrange it in a logical sequence. . It is
necessary to draw a flowchart before writing a procedure because it gives
a picture of the logical flow of operations. If there is any logical error in
the procedure, we can easily identify the error just by looking at the
flowchart.

Dr. Alok Das, Dept of Mathematics, J. N. College, Boko


10

Fig. Flowchart for writing and posting a post card.

Start

Pick up a postcard

Pick up the pen

Write on the post card

Write the address

Goto the post office

Post the card

Stop

Dr. Alok Das, Dept of Mathematics, J. N. College, Boko


11

Fig: Summing of N given numbers.

Start

Read N

Put S = 0
Initialization and I = 0

Read A

Computation Add A into S


(S=S+A)

Looping
Increase I by 1
Modification (I=I+1)

is
Testing I=N
No

Write S

Stop

Dr. Alok Das, Dept of Mathematics, J. N. College, Boko


12

Flowchart: To find the biggest number of three given numbers:

Start

Read A, B, C

No Yes
Is
A>B

Yes No No Yes
Is Is
B>C A>C

Print B Print C Print A

Stop

Dr. Alok Das, Dept of Mathematics, J. N. College, Boko


13

Q.1. Draw a flowchart to find the sum of n given numbers


OR
Draw a flowchart to find the sum of n integers given as input.

Solution:

Start

Read n

Sum=0

For i=1 to n

Read x[i]

Sum=Sum + x[i]

End for

Print Sum

Stop

Dr. Alok Das, Dept of Mathematics, J. N. College, Boko


14

Q.2. Draw a flowchart to find the average, maximum and minimum values of n given
numbers.
Solution:
Start

Read n

For i=1 to n

Read x[i]

End for

Max=x[1], Min=x[1], Sum=0

For i=1 to n

No Is Yes
Max<x[i]
?
Max=x[i]

Is
Min>x[i] Yes
? Min=x[i]

No Sum=Sum+x[i]

End for

Average=Sum/n

Write Max, Min, Average

Stop

Dr. Alok Das, Dept of Mathematics, J. N. College, Boko


15

Q.3. Draw a flowchart to read and find the maximum of n given numbers.

Solution:
Start

Read n

For i=1 to n

Read x[i]

End for

Max=x[1]

For i=1 to n

If
No max<x[i] ? Yes

Max=x[i]

End for

Print Max

Stop

Dr. Alok Das, Dept of Mathematics, J. N. College, Boko


16

Q.4. Draw a flowchart to find the maximum of n given numbers.

Solution:
Start

Read n

For i=1 to n

Read x[i]

End for

Max=x[1]

For i=1 to n

If
max<x[i] ?
No Yes

Max=x[i]

End for

Print Max

Stop

Dr. Alok Das, Dept of Mathematics, J. N. College, Boko


17

Q.7: Draw a flowchart to print the maximum of n given numbers.

Solution:

Start

Read n

For i=1 to n

Read x[i]

End for

Max=x[1]

For i=1 to n

No If max<x[i] Yes
?
Max=x[i]

End for

Print Max

Stop

Dr. Alok Das, Dept of Mathematics, J. N. College, Boko


18

Q.8: Draw a flowchart to determine and print the average of 30 given numbers.

Solution:
Flowchart: To determine and print the average of 30 given numbers.
Start

Sum =0, Count =1

Read Num

Sum = Sum+Num

Count = Count+1

Is
Count 30 ?
Yes

No
Average =Sum/(Count-1)

Print Average

Stop

Dr. Alok Das, Dept of Mathematics, J. N. College, Boko


19

Q.9: Draw a flowchart to read N numbers and then to compute and print the
arithmetic mean of the numbers.
Solution: The flowchart to compute the arithmetic mean of n numbers is shown
below.
Start

Read n

For I=1 to N

Read A[I]

End For

Sum = 0

For I=1 to N

Sum=Sum+A[I]

End For

AM=Sum/N

Print AM

Stop

Dr. Alok Das, Dept of Mathematics, J. N. College, Boko


20

Q.13: Draw a flowchart to read a value of N and to compute and print the value of N!

Solution:
Flowchart: To read a value of N and to compute and print the value of N!

Start

Read N

Is
N <0 ?
Yes No
Fact = 1
Stop

For L = 2 to N

Fact=Fact*L

End For

Print Fact

Stop

Dr. Alok Das, Dept of Mathematics, J. N. College, Boko

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy