in this chapter you will be learning...

16
CH- 3 ALGORITHMS AND FLOW CHART In this chapter you will be learning about Algorithms and its use Characteristics of a good algorithm Steps to develop an algorithm Advantages disadvantages of algorithm Flowchart with its advantages and disadvantages Various symbols used in flowchart Programming constructs sequence decision and repetition ALGORITHM An algorithm is a step by step sequence of logical instruction that produce output based on the given input. An algorithm is formulated before writing an actual computer programme THE algorithm is always written in PSEUDOCODE a pseudo code is an informal description of computer program that is written in simple English language ALGORITHM can perform calculating data processing and automated reasoning tasks CHARATERISTICS OF A GOOD ALGORITHM The characteristics of a good algorithm are listed below:- INPUT – THE algorithm accepts an input IT must have zero or more inputs OUTPUT –THE algorithm produce output it can have one or more outputs PRECISION- EACH step of algorithm must be precisely defined ; the actions to be carried out must be carefully and unambiguously specify for each case UNIQUENCESS- RESULT of each step are uniquely defined and only depends on the input and the result of preceding steps FIFITENESS – AN ALGORITHM must always terminate after a finite numbers of steps are executed EFFECTIVESNESS – EVERY step of an algorithm must be feasible and compatible with available resources STEPS TO DEVELOP AN ALOGRITHM THE following points need to be kept in mind while developing an algorithm:- AN algorithm is step by step process so remember to give the statement a sequence number such as STEP; 1 , STEP; 2 , STEP; 3 and so on ALWAYS begin an algorithm with the keyword ‘START’ and end it with keywords ‘STOP’

Upload: others

Post on 20-Jul-2020

2 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: In this chapter you will be learning aboutmetasofsda.in/school/wp-content/uploads/sites/4/2020/04/VIII-COMP… · Advantages disadvantages of algorithm Flowchart with its advantages

CH- 3 ALGORITHMS AND FLOW CHART

In this chapter you will be learning about

Algorithms and its use

Characteristics of a good algorithm

Steps to develop an algorithm

Advantages disadvantages of algorithm

Flowchart with its advantages and disadvantages

Various symbols used in flowchart

Programming constructs sequence decision and repetition

ALGORITHM

An algorithm is a step by step sequence of logical instruction that produce output based on

the given input. An algorithm is formulated before writing an actual computer programme

THE algorithm is always written in PSEUDOCODE a pseudo code is an informal description of

computer program that is written in simple English language ALGORITHM can perform

calculating data processing and automated reasoning tasks

CHARATERISTICS OF A GOOD ALGORITHM

The characteristics of a good algorithm are listed below:-

INPUT – THE algorithm accepts an input IT must have zero or more inputs

OUTPUT –THE algorithm produce output it can have one or more outputs

PRECISION- EACH step of algorithm must be precisely defined ; the actions to be carried out

must be carefully and unambiguously specify for each case

UNIQUENCESS- RESULT of each step are uniquely defined and only depends on the input and

the result of preceding steps

FIFITENESS – AN ALGORITHM must always terminate after a finite numbers of steps are

executed

EFFECTIVESNESS – EVERY step of an algorithm must be feasible and compatible with

available resources

STEPS TO DEVELOP AN ALOGRITHM

THE following points need to be kept in mind while developing an algorithm:-

AN algorithm is step by step process so remember to give the statement a sequence number

such as STEP; 1 , STEP; 2 , STEP; 3 and so on

ALWAYS begin an algorithm with the keyword ‘START’ and end it with keywords ‘STOP’

Page 2: In this chapter you will be learning aboutmetasofsda.in/school/wp-content/uploads/sites/4/2020/04/VIII-COMP… · Advantages disadvantages of algorithm Flowchart with its advantages

ALWAYS mention the steps clearly and aptly

Always start each statement from a new line

INCLUDE keywords [ such as for accepting variables , assigning values etc. ] and their usage

as when required

LOGIC of an algorithm should be clearly defined

ADVANTAGES OF AN ALGORITHM

There are various advantages of writing an algorithm for solving a problem. Some of them

are mentioned below:-

It is easy to understand as it is a stepwise representation of a solution to a given problem.

It is independent of any computer programming language and written in simple English

language. Therefore, it is easy to understand for anyone even without prior programming

knowledge.

It is easy to modify as every step in an algorithm has its own logical sequence.

A programmer can easily convert an algorithm into an actual program, since the problem is

already stepwise solved.

DISADVANTAGES OF AN ALGORITHM

There are also some disadvantages as mentioned below:-

Writing an algorithm is time consuming process

An algorithm is not a computer program; it is rather a concept of how programme should be

solved.

FLOW CHART

A flow is a graphical representation of sequences of step that is ALGORITHM required to

solve a particular problem. Using a flowchart, you can easily understand the logic of a

program .FLOWCHART is very helpful in writing program and explaining it to others

THE following table will describe the purpose and the name of the symbols used in the

constructing a flow chart

NAME SYMBOL DESCRIPTION

START\ END

IT is used at the beginning and end of the flow chart .when used in beginning, START is written in it and when used in end, STOP is written in it.

INPUT \OUTPUT

IT is used to represent the input of data and the output of the information

It holds the instruction required for the processing of input to get the desired

Page 3: In this chapter you will be learning aboutmetasofsda.in/school/wp-content/uploads/sites/4/2020/04/VIII-COMP… · Advantages disadvantages of algorithm Flowchart with its advantages

PROCESS

output FOREXAMPLE, instruction like SUM=A+B will be entered in the PROCESS box

DECISION

IT is used when compares has to be made and further action depends upon the result of it FOR EXAMPLE, statement like SUM>50 will be written in DECISION BOX

FLOWLINES

IT is used to represent the flow of control in the process

FEATURES OF A FLOWCHART

WHILE creating a flowchart, only a single flow line comes out from a process box

FLOW direction of a process should always be indicated either from left to right or from top

to bottom as shown in the figure

IN a decision box only one flow line can enter and any number of flow lines can come out

ONLY one flow line comes out from the start and enters into stop

EVERY flow chart contains a logical start and end

ADVANTAGES OF A FLOW CHART

THERE are many advantages of the flow chart while solving a problem .some of them is

mentioned below;

A flowchart is an excellent andeffective way of communicating the logic of a program

graphically

A flowchart is easy and efficient way to analyse a program

DURING program development phase, a flowchart act as guide or a blueprint which makes

programme development process easier

START END

Page 4: In this chapter you will be learning aboutmetasofsda.in/school/wp-content/uploads/sites/4/2020/04/VIII-COMP… · Advantages disadvantages of algorithm Flowchart with its advantages

IT helps the programmer to write the actual program code easily

DISADVANTAGE OF FLOWCHART

DRAWING flowchart is a time consuming task

DIFFICULT to alter the flowchart. THE programmer may need to redraw the complete

flowchart to change or alter the logic of the flowchart

DIFFICULT to understand the logic and process of the complex flowchart

IT is just graphical visualisation of a program and it cannot serve as an actual program

PROGRAMMING CONSTRUCTS

THERE are three types of programming constructs that provide protocols to help us in

creating a flowchart

SEQUENCE CONSTRUCT

A sequence construct tells the processors which statement is to be executed next. In a

sequence construct the program flow lines simply move from one statement to the next

EXAMPLE 1:- DEVELOP an algorithm and the flow chart to add two numbers

ALGORITHM FLOWCHART

STEP 1:- START STEP 2:- ACCEPT SECOND NUMBER [B] STEP 3:- ACCEPT SECOND NUMBER [B] STEP 4:- ADD TWO NUMBERS [A+B] AND STORE THE VALUE IN VARIABLE NAMED C STEP 5:- PRINT THE VALUE STORED IN THE VARIABLE C. STEP 6:- STOP

START

Accept A, B

C = A + B

Print C

Stop

Page 5: In this chapter you will be learning aboutmetasofsda.in/school/wp-content/uploads/sites/4/2020/04/VIII-COMP… · Advantages disadvantages of algorithm Flowchart with its advantages

DECISION CONSTRUCT

IN a decision construct the program control will be transferred one statement to another

depending upon whether the certain condition ismet.

EXAMPLE 2:- DEVELOP an algorithm and flowchart to find the greater number between the

two numbers

ALGORITHM FLOWCHART

STEP 1 :- START STEP 2 :- ACCEPT FIRST NUMBER [A] STEP 3 :- ACCEPT SECOND NUMBER [B] STEP 4 :- CHECK IF A>B STEP 5 :- PRINT AN IF A IS GREATER, ELSE PRINT B. STEP 6 :- STOP

YES NO

START

Accept A, B

If

A>B Accept A, B

Accept A, B

Stop

Page 6: In this chapter you will be learning aboutmetasofsda.in/school/wp-content/uploads/sites/4/2020/04/VIII-COMP… · Advantages disadvantages of algorithm Flowchart with its advantages

REPETION CONSTRUCT

IN repetition construct the statement is executed again and again till the given condition is

met .it makes the use of loops .a loop is used to repeat a set of statements again and again

till the given condition is met. A counter keyword is used to keep track of the loop

EXAMPLE 3:- Develop an algorithm and a flowchart to find the average of any five numbers.

If average is >75, print “very good” otherwise print “WORK HARD”.

ALGORITHM FLOWCHART

STEP 1 :- START STEP 2 :- ACCEPT FIRST NUMBER [A] STEP 3 :- ACCEPT SECOND NUMBER [B] STEP 4 :- ACCEPT THIRD NUMBER [C] STEP 5 :- ACCEPT FOURTH NUMBER [D] STEP 6 :- ACCEPT FIFTH NUMBER [E] STEP 7 :- CALCULATE THE AVERAGE

𝐴 + 𝐵 + 𝐶 + 𝐷 + 𝐸

5

AND STORE THE RESULT IN THE VARIABLE AVG STEP 8 :- CHECK IF AVG >75 THEN PRINT “VERY GOOD” ELSE PRINT “WORKHARD” STEP 9 :- STOP

YES NO

START

Accept A, B, C, D, E

If

AVG >75

𝐴𝑉𝐺 =𝐴 + 𝐵 + 𝐶 + 𝐷 + 𝐸

5

Print “very good”

Print “work hard”

Stop

Page 7: In this chapter you will be learning aboutmetasofsda.in/school/wp-content/uploads/sites/4/2020/04/VIII-COMP… · Advantages disadvantages of algorithm Flowchart with its advantages

CH- 3 ALGORITHMS AND FLOW CHART

EXERCISE

TECH TERMS (DEFINE):-

ALGORITHM :- An algorithm is a step by step sequence of logical instructions that produces

output based on the given input

PSEUDOCODE :- A Pseudo code is an informal description of computer program that is

written in English language

FLOWCHART :- A flowchart is a graphical representation of sequence of steps required to

solve a particular problem

Sequence construct :- In sequence construct program ,flow line simply moves more from

one statement to the next .It tells the processors which statement is to be executed next

DECISION CONSTRUCT :- In a decision construct the program control will be transferred from

one statement to another depending upon whether the specified condition is met

REPETITION CONSTRUCT :- In repetition construct , statement is executed again and again till

the specified condition is met

LOOP :- It is used to repeat a set of statement again and again till specified condition is met

RECALL TIME :-

An algorithm is a step by step sequence of logical instruction that produce output based on

given input

An algorithm should always begin wit START and wit the STOP keyword

Logic of an algorithm should be aptly defined

An algorithm is always written in PSEUDOCODE

A pseudo code is an informal description of an computer program that is written in simple

English language

A flowchart is a graphical representation of sequence of step required to solve a particular

problem

Various programming construct are ;sequence, decision and repetition constructs

Various symbols used in flow chart are: -START\END, INPUT/OUTPUT, PROCESS, DECISION

and FLOWLINES

Page 8: In this chapter you will be learning aboutmetasofsda.in/school/wp-content/uploads/sites/4/2020/04/VIII-COMP… · Advantages disadvantages of algorithm Flowchart with its advantages

PRACTICE TIME:-

TICK THE CORRECT OPTION:-

1. How many flow lines[s] can enter into decision box?

A) One

B) Two

C) Three

D) Four

Ans: - A) one

2. Which of these programming constructs make use of a loop?

A) Sequence Construct

B) Decision Construct

C) Repetition Construct

D) None of these

Ans: - C) Repetition Construct

3. Which of the following is a graphical representation of step required to solve a problem?

A) Algorithm

B) Pseudo code

C) Flowchart

D) None of these

Ans: - C) Flowchart

4. Which symbol is when comparison has to be made and further actions depends upon the

result of it?

A) Start\End oval

B) Decision box

C) Processing box

D) Input\Output box

Page 9: In this chapter you will be learning aboutmetasofsda.in/school/wp-content/uploads/sites/4/2020/04/VIII-COMP… · Advantages disadvantages of algorithm Flowchart with its advantages

Ans: -B) Decision box

5. Which keyword is used to begin and end an algorithm?

A) Start and End

B) Begin and End

C) Start and Stop

D) Begin and Stop

Ans: -C) Start and Stop

FILL UP THE BLANKS:-

1) A __________ is a sequence of logical instruction that produces output based on the given

input.

2) A __________is used to repeat a set of statements again and again till the specified

condition is met.

3) A __________ is used at the beginning and end of the flowchart.

4) The various programming constructs are __________, __________ and __________.

5) __________used to represent the flow of control in the process.

Ans: - 1) algorithm 2) loop 3) oval 4) [a]sequence [b]repetition [c]decision 5) flow lines

WRITE [T] FOR TRUE AND [F] FOR FALSE STATEMENT:-

1) An algorithm is easy to understand as it is a stepwise representation of san solution to a

given problem.

2) A flowchart is a graphical representation of sequence of a step required of a solution to a

given problem.

3) A sequence construct tells the processors which statement is to be executed next.

4) A Counter key word is used to keep track of loop.

5) In a decision box, multiple flow lines can enter.

Ans: - 1-f; 2-t; 3-t; 4-t; 5-f

Page 10: In this chapter you will be learning aboutmetasofsda.in/school/wp-content/uploads/sites/4/2020/04/VIII-COMP… · Advantages disadvantages of algorithm Flowchart with its advantages

MATCH THE COLUMNS:-

A. FLOW LINE. 1.

B. DECISION.

2.

C. START\END.

3.

D. INPUT\OUTPUT.

4.

E. PROCESS. 5.

Ans: - 1) C 2) D 3) E 4) B 5) A

ANSWER THE FOLLOWING QUESTION

1. Define the term algorithm. Write its two advantages and disadvantages each.

Ans: - An algorithm is a step by step sequence of logical instruction that produce output

based on the given input.

ADVANTAGES:-

It is easy to understand as it is a stepwise representation of a solution to a given problem.

It is independent of any computer programming language and written in simple English

language. Therefore, it is easy to understand for anyone even without prior programming

knowledge.

DISADVANTAGES:-

Writing an algorithm is time consuming process

An algorithm is not a computer program; it is rather a concept of how programme should be

solved.

Page 11: In this chapter you will be learning aboutmetasofsda.in/school/wp-content/uploads/sites/4/2020/04/VIII-COMP… · Advantages disadvantages of algorithm Flowchart with its advantages

2. What are the basic rules for writing flowchart?

Ans:- WHILE creating a flowchart, only a single flow line comes out from a process box

FLOW direction of a process should always be indicated either from left to right or from top

to bottom as shown in the figure

IN a decision box only one flow line can enter and any number of flow lines can come out

ONLY one flow line comes out from the start and enters into stop

EVERY flow chart contains a logical start and end

3. Define the term flow chart. Write its two advantages and disadvantages.

Ans: - A flow is a graphical representation of sequences of step that is ALGORITHM required

to solve a particular problem

ADVANTAGES:-

A flowchart is an excellent and effective way of communicating the logic of a program

graphically

A flowchart is easy and efficient way to analyse a program

DISADVANTAGES:-

DRAWING flowchart is a time consuming task

DIFFICULT to alter the flowchart. THE programmer may need to redraw the complete

flowchart to change or alter the logic of the flowchart

START END

Page 12: In this chapter you will be learning aboutmetasofsda.in/school/wp-content/uploads/sites/4/2020/04/VIII-COMP… · Advantages disadvantages of algorithm Flowchart with its advantages

4. Briefly state the purpose of each symbol used while constructing a flowchart.

Ans:-

NAME SYMBOL DESCRIPTION

START\ END

IT is used at the beginning and end of the flow chart .when used in beginning, START is written in it and when used in end, STOP is written in it.

INPUT \OUTPUT

IT is used to represent the input of data and the output of the information

PROCESS

It holds the instruction required for the processing of input to get the desired output FOREXAMPLE, instruction like SUM=A+B will be entered in the PROCESS box

DECISION

IT is used when compares has to be made and further action depends upon the result of it FOR EXAMPLE, statement like SUM>50 will be written in DECISION BOX

FLOWLINES

IT is used to represent the flow of control in the process

Page 13: In this chapter you will be learning aboutmetasofsda.in/school/wp-content/uploads/sites/4/2020/04/VIII-COMP… · Advantages disadvantages of algorithm Flowchart with its advantages

5. Briefly explain the different types of programming constructs

Ans:-

PROGRAMMING CONSTRUCTS

THERE are three types of programming constructs that provide protocols to help us in

creating a flowchart

SEQUENCE CONSTRUCT

A sequence construct tells the processors which statement is to be executed next. In a

sequence construct the program flow lines simply move from one statement to the next

DECISION CONSTRUCT

IN a decision construct the program control will be transferred one statement to another

depending upon whether the certain condition is met.

REPETION CONSTRUCT

IN repetition construct the statement is executed again and again till the given condition is

met .it makes the use of loops .a loop is used to repeat a set of statements again and again

till the given condition is met. A counter keyword is used to keep track of the loop

6. DEVELOP an algorithm and the flow chart to add two numbers

ALGORITHM FLOWCHART

Page 14: In this chapter you will be learning aboutmetasofsda.in/school/wp-content/uploads/sites/4/2020/04/VIII-COMP… · Advantages disadvantages of algorithm Flowchart with its advantages

STEP 1:- START STEP 2:- ACCEPT SECOND NUMBER [B] STEP 3:- ACCEPT SECOND NUMBER [B] STEP 4:- ADD TWO NUMBERS [A+B] AND STORE THE VALUE IN VARIABLE NAMED C STEP 5 :- PRINT THE VALUE STORED IN THE VARIABLE C. STEP 6:- STOP

7. Develop an algorithm and flowchart to find the greater number between the two numbers

ALGORITHM FLOWCHART

STEP 1 :- START STEP 2 :- ACCEPT FIRST NUMBER [A] STEP 3 :- ACCEPT SECOND NUMBER [B] STEP 4 :- CHECK IF A>B STEP 5 :- PRINT AN IF A IS GREATER, ELSE PRINT B. STEP 6 :- STOP

YES NO

START

Accept A, B

C = A + B

Print C

Stop

START

Accept A, B

If

A>B Accept A, B

Accept A, B

Stop

Page 15: In this chapter you will be learning aboutmetasofsda.in/school/wp-content/uploads/sites/4/2020/04/VIII-COMP… · Advantages disadvantages of algorithm Flowchart with its advantages

8. Develop an algorithm and a flowchart to find the average of any five numbers. If average is

>75, print “very good” otherwise print “WORK HARD”.

ALGORITHM FLOWCHART

STEP 1 :- START STEP 2 :- ACCEPT FIRST NUMBER [A] STEP 3 :- ACCEPT SECOND NUMBER [B] STEP 4 :- ACCEPT THIRD NUMBER [C] STEP 5 :- ACCEPT FOURTH NUMBER [D] STEP 6 :- ACCEPT FIFTH NUMBER [E] STEP 7 :- CALCULATE THE AVERAGE

𝐴 + 𝐵 + 𝐶 + 𝐷 + 𝐸

5

AND STORE THE RESULT IN THE VARIABLE AVG STEP 8 :- CHECK IF AVG >75 THEN PRINT “VERY GOOD”

YES NO

START

Accept A, B, C, D, E

If

AVG >75

𝐴𝑉𝐺 =𝐴 + 𝐵 + 𝐶 + 𝐷 + 𝐸

5

Print “very good”

Print “work hard”

Stop

Page 16: In this chapter you will be learning aboutmetasofsda.in/school/wp-content/uploads/sites/4/2020/04/VIII-COMP… · Advantages disadvantages of algorithm Flowchart with its advantages

ELSE PRINT “WORKHARD” STEP 9 :- STOP