[go: up one dir, main page]

0% found this document useful (0 votes)
19 views17 pages

Module 4 Algorithm and Flowchart

Uploaded by

anila
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)
19 views17 pages

Module 4 Algorithm and Flowchart

Uploaded by

anila
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/ 17

lOMoARcPSD|21220369

Module 5-Algorithm and flowchart

Bsc maths (University of Calicut)

Scan to open on Studocu

Studocu is not sponsored or endorsed by any college or university


Downloaded by Anila Janaki (anilapaloor@gmail.com)
lOMoARcPSD|21220369

CSC1C01 – Computer Fundamentals Module 5


Syllabus: Module 5
Planning a Computer program, purpose of program planning, algorithm,
flowchart - symbols, sample flowcharts, advantages and limitations.

Purpose of Program Planning


To produce an effective computer program it is necessary to write every instruction in
the correct sequence. Hence, to ensure that the instructions of a program are
appropriate for the problem and in correct sequence, we must plan the program before
writing it.

Algorithm
What is an Algorithm?
 Planning a program involves defining its logic (the correct sequence of
instructions needed to solve the corresponding problem).
 The Term algorithm refers to the logic of a program.
 It is the step by step description of instruction to solve a specified problem.
 In an algorithm, each instruction must process following characteristics:
1. Each instruction should be precise and unambiguous.
2. Each instruction should be executed in a finite time.
3. No instruction should repeat infinitely. This ensures the algorithm terminate
ultimately.
4. After executing the instructions, the desired results should obtained.

Properties of algorithm
Donald Ervin Knuth has given a list of five properties for an algorithm, these properties
are:
1) Finiteness: An algorithm must always terminate after a finite number of
steps.
2) Definiteness: Each step of an algorithm must be precisely defined.
3) Input: Input is the value/quantities are given to the algorithm before it
begins.
4) Output: One always expects output/result (expected value/quantities) in
terms of output from an algorithm.
5) Effectiveness: Algorithms to be developed/written using basic operations.

Downloaded by Anila Janaki (anilapaloor@gmail.com)


lOMoARcPSD|21220369

CSC1C01 – Computer Fundamentals Module 5


Sample Examples:

Problem 1: Find the area of a Circle of radius r.

Inputs to the algorithm: Radius r of the Circle.


Expected output: Area of the Circle

Algorithm:
Step1: Start
Step2: Read Radius r of the Circle
Step3: Area= PI*r*r
Step4: Print Area
Step5: End

Problem2: Write an algorithm to read two numbers and find their sum.

Inputs to the algorithm: num1, num2.


Expected output: Sum of the two numbers

Algorithm:
Step1: Start
Step2: Read input the num1
Step3: Read input the num2
Step4: Sum=num1+num2
Step5: Print Sum
Step6: End

Problem 3: Convert temperature in Fahrenheit to Celsius

Inputs to the algorithm: Temperature in Fahrenheit


Expected output: Temperature in Celsius

Algorithm:
Step 1: Start
Step 2: Read Temperature in Fahrenheit F
Step 3: C = 5/9*(F-32)
Step 4: Print Temperature in Celsius: C
Step 5: End

Problem4 : Write algorithm to find the largest number between two numbers

Inputs to the algorithm: First number A and Second number B


Expected output: Largest number

Algorithm:
Step1: Start
Step2: Read input A and B
Step3: If A greater than B then C=A
Step4: if B greater than A then C=B
Step5: Print C
Step6: End

Downloaded by Anila Janaki (anilapaloor@gmail.com)


lOMoARcPSD|21220369

CSC1C01 – Computer Fundamentals Module 5


Problem5: A algorithm to find the largest value of any three numbers.
Inputs to the algorithm: numbers A, B and C
Expected output: Largest number

Algorithm:
Step1: Start
Step2: Read input A,B and C
Step3: If (A>=B) then goto Step 4 else goto Step 5
Step4: If (A>=C) then Max=A and GOTO Step 6 else Max= C and goto Step 6
Step5: If (B>=C) then Max=B and GOTO Step 6 else Max= C and goto Step 6
Step6: Print Max
Step7: End
Problem6: An algorithm to calculate even numbers between 0 and 99
Inputs to the algorithm: Read no input from input terminal
Expected output: Print even numbers between 0 and 99

Algorithm:
Step1: Start
Step2: Set I=2
Step3: IF (I<=98) then do Steps 4 to 5 else goto Step 6
Step4: Print I
Step5: I=I+2 goto Step 3
Step6: End
Problem7 : Design an algorithm which read a natural value N as its input and
calculates odd numbers equal or less than N. Then write them in the standard output:
Inputs to the algorithm: Read N
Expected output: Print ODD numbers between 0 and N

Algorithm:
Step1: Start
Step2: Read N
Step3: Set I=1
Step4: IF (I<=N) then do Steps 5 to 6 else goto Step 7
Step5: Print I
Step6: I=I+2 goto step 4
Step7: End

Downloaded by Anila Janaki (anilapaloor@gmail.com)


lOMoARcPSD|21220369

CSC1C01 – Computer Fundamentals Module 5


Problem8 : Design an algorithm to print sum of first N natural number.
Inputs to the algorithm: Read N
Expected output: SUM of first N natural number

Algorithm 1: Algorithm 2:
Step1: Start Step1: Start
Step2: Read N Step2: Read N
Step3: Set SUM=0 Step3: SUM= (N*(N+1))/2
Step4: Set I=1 Step4: Print SUM
Step5: IF (I<=N) then do Steps 6 to 7 else goto Step 8 Step5: End
Step6: SUM= SUM+I
Step7: I= I +1 goto Step 5
Step8: Print SUM
Step9: End

Representation of Algorithm
Commonly used ways to represent an algorithm are:
1. As flowchart
2. As pseudocode
3. As program

1) Algorithm representation using Flowchart


 A flowchart is a pictorial representation of an algorithm.
 It uses different types of shapes to represent different type of instruction.
 Directed solid lines connecting these shapes indicate flow of operations.

Flowchart Symbols

1. Terminal Symbol(Rounded rectangle) :-Terminal symbol indicates the beginning (


Start) and the end(Stop) in a program’s logic flow. It is the first and last symbol in
a flowchart.

Downloaded by Anila Janaki (anilapaloor@gmail.com)


lOMoARcPSD|21220369

CSC1C01 – Computer Fundamentals Module 5


2. Input/output Symbol (parallelogram) :- Input/output symbol indicate
instruction to input/output data from any of input/output device ( such as
keyboard, mouse, scanner, monitor, printer, etc..)
3. Processing Symbol ( Rectangle) :- A processing symbol represents arithmetic
and data movement instruction.
4. Decision Symbol ( Diagonal box)- A decision symbol indicates a decision point,
which is a point at which a branch to one of two or more alternative path is
possible. Figure 11.2 shows three different ways of using a decision symbol.

5. Flow lines (Directed Arrow):- Flow lines with arrow head indicates the flow of
operation, which is the sequence of execution of instruction.

6. Connectors ( Circle):- When a flowchart becomes so complex or spread over


more than one page, it is useful to utilize connector symbol as a substitute for flow
lines.

Sample Flowchart
Problem1: Find the area of a circle of radius r.

Downloaded by Anila Janaki (anilapaloor@gmail.com)


lOMoARcPSD|21220369

CSC1C01 – Computer Fundamentals Module 5

Problem 2: Convert temperature in Fahrenheit to Celsius.

Problem3: Flowchart for an algorithm which gets two numbers and prints
sum of their value

Problem5: Algorithm for find the greater number between two numbers.

Downloaded by Anila Janaki (anilapaloor@gmail.com)


lOMoARcPSD|21220369

CSC1C01 – Computer Fundamentals Module 5


Problem6: Flowchart for the problem of printing even numbers between 9 and 100:

Problem7: Flowchart for the problem of printing odd numbers less than a given
number. It should also calculate their sum and count.

Downloaded by Anila Janaki (anilapaloor@gmail.com)


lOMoARcPSD|21220369

CSC1C01 – Computer Fundamentals Module 5


Problem8 : Design an algorithm to print sum of first N natural number.

problem8: Flowchart for the calculate the average from 25 exam scores.

Downloaded by Anila Janaki (anilapaloor@gmail.com)


lOMoARcPSD|21220369

CSC1C01 – Computer Fundamentals Module 5


Problem 9: Draw a flowchart to find the largest of three numbers A, B and C.

Problem 10 : Draw a flowchart to find first N Fibonacci numbers.

Hint : The Fibonacci Sequence is the series of numbers:


0, 1, 1, 2, 3, 5, 8, 13, 21, 34, ...
The next number is found by adding up the two numbers before it:
 the 2 is found by adding the two numbers before it (1+1),
 the 3 is found by adding the two numbers before it (1+2),
 the 5 is (2+3),
 and so on!

Downloaded by Anila Janaki (anilapaloor@gmail.com)


lOMoARcPSD|21220369

CSC1C01 – Computer Fundamentals Module 5

Levels of Flowcharts
There are no set standards on the amount of detail required in a flowchart. A
flowchart that outlines the main segments of a program (shows only broad details)
is a macro flowchart, and a flowchart with more details is a micro flowchart (also
called detailed flowchart).

Figure 11.10 shows a detailed flowchart for the operation in this processing box. The
detailed flowchart shows how program logic computes the value of Total.

Flowcharting Rules
American National Standards Institute (ANSI) has recommended a number of
general rules and guidelines to help standardize flowcharting process.
The basic flowcharting rules and guidelines are as follows:
1) First chart the main line of logic, and then incorporate details.
2) Maintain a consistent level of details for a given flowchart.
3) Use common statements that are easy to understand for words within
flowchart symbols. Use descriptive titles written in your own language, rather
than machine-oriented language.
4) Be consistent in using names and variables in a flowchart.
5) Go from left to right and top to bottom in constructing a flowchart.
6) Keep a flowchart as simple as possible, and avoid crossing of flow lines.
7) If you need a new page while drawing a flowchart, break the
flowchart at an input or output point. Moreover, use properly labeled
connectors to link portions of the flowchart on different pages.

Advantages and limitation of Flowchart


Advantages:

1. Better Communication :- A flowchart is a pictorial representation of a program.


Therefore, it is easier for a programmer to explain the logic of a program to others
through flowchart.
2. Effective analysis:- A macro flowchart, which charts main operation of software
system, becomes the systems model that can be broken down into detailed parts
for study and further analysis.

10

Downloaded by Anila Janaki (anilapaloor@gmail.com)


lOMoARcPSD|21220369

CSC1C01 – Computer Fundamentals Module 5


3. Effective synthesis :- Normally, a group of programmers is associated with the
design of a big software system. Each programmer is responsible for designing part
of the entire system. Hence, flowchart of all programmer put together can help
visualize the overall system design. At this stage, we can easily detect any problem
in linking various part of the system and modify the design accordingly. Thus we
often use flowchart as working model in the design of new program/software.
4. Proper documentation:- Flowchart of the software help to document the software
system properly.
5. Efficient Coding:- Once a flowchart is ready, programmers find it very easy to
write the corresponding program because the flowchart act as a road map for
them.
6. Systematic debugging:- Using flowchart it is easier to follow logic of the
program, so it is easier to detect, locate and remove bugs(mistakes) in the
program.
7. Systematic testing:- Testing is the process of confirming whether a program does
all its indented jobs under specified constraints. A flowchart is a very helpful tool
for designing test data for systematic testing of programs.

Limitations:
1. Flowcharts are very time consuming to draw with proper symbols and spacing,
especially for large complex programs
2. Any modification in logic of the program usually requires a completely new
flowchart. Redrawing a flowchart is very tedious task.
3. There is no standards determining the amount of detail that should be included in a
flowchart.

2) Algorithm representation using Pseudocode


 Pseudocode is another program planning tool used for planning the program logic.
 pesudocode uses a structure that resembles computer instructions.
 Pseudo code is not machine-readable.
 Pseudo code cannot be compiled or executed

11

Downloaded by Anila Janaki (anilapaloor@gmail.com)


lOMoARcPSD|21220369

CSC1C01 – Computer Fundamentals Module 5


Advantages and limitation of Pseudocode
Advantages

1. As compared to flowchart converting a psedocode to programming language is


much easier.
2. As compared to flowchart, it is easier to modify the psedocode of the program
logic when program modifications are necessary.
3. Writing of pseudocode involves much less time and effort than drawing an
equivalent flowchart.

Limitations:
1. There is no standard rules to follow in writing pseudocode. Different
programmers use their own style. Hence, communication problem occurs due to
lack of standardization
2. In case of pseudocode, a graphical representation of program logic is not
available

3) Algorithm representation using Program


 Algorithm represented in the form of a programming language is a
program.
 Hence any program is an algorithm, But the reverse is not true.

Sample C Program: Add two numbers

int main() {
int number1, number2, sum;

printf("Enter two integers: ");


scanf("%d %d", &number1, &number2);
sum = number1 + number2;
printf("Sum = %d", sum);
return 0;
}

Record
Collection of related data item treated as a unit is known as record.
Example: A student record consist of Roll Number , Student name, Class, Mark details.

Trailer Record

Trailer record is a special record, whose sole purpose is to indicate the end of input
data.
For example, In a student record assume that the data field Roll Number consist of 7
digits. Since 0000000 is never a valid Roll Number, we can use this value as Trailer
Record’s Roll Number value.
Here the value 0000000 is known as sentinel value for student trailer record

12

Downloaded by Anila Janaki (anilapaloor@gmail.com)


lOMoARcPSD|21220369

CSC1C01 – Computer Fundamentals Module 5


Check Your Progress:
1) What is an algorithm?
2) Write an algorithm to find average age of a group of 10 players?
3) Explain uses of Flowchart.
4) Draw a flowchart to find the sum of first 100 natural numbers.
5) Draw a flowchart to find the largest of three numbers x, y and z.
6) Draw flowchart to find factorial of a given number.
7) Draw flowchart to check whether the given number is amstrong number or not.
Hint: A positive integer is called an Armstrong if and only if the sum of cubes of
each digit is equal to the number itself. For example, 153 is an Armstrong number
because
153 = 1*1*1 + 5*5*5 + 3*3*3

8) Draw flowchart to find largest number among three numbers.


9) Draw flowchart to print sum of digits.
Hint: Input: 234
Output: 9

13

Downloaded by Anila Janaki (anilapaloor@gmail.com)


lOMoARcPSD|21220369

CSC1C01 – Computer Fundamentals Module 5

10) Draw a flowchart to reverse a given number.

14

Downloaded by Anila Janaki (anilapaloor@gmail.com)


lOMoARcPSD|21220369

CSC1C01 – Computer Fundamentals Module 5


11) Draw flowchart to print sum of squares of first N natural number.

Hint: N = 5 Then SUM = 1 +4 +9 +16+ 25

12) Draw flowchart to print squares of first N natural number.

Hint: N = 5 Then print 1, 4, 9, 16, 25

15

Downloaded by Anila Janaki (anilapaloor@gmail.com)


lOMoARcPSD|21220369

CSC1C01 – Computer Fundamentals Module 5


13) Draw flowchart to check a given number is prime or not.

16

Downloaded by Anila Janaki (anilapaloor@gmail.com)

You might also like