COMPUTER PROGRAMMING UNIT 1 Lecture 4

Preview:

Citation preview

COMPUTER PROGRAMMING

Lecture 04

Prepared By Mr. V. S. Patil Dept (CSE)/AEC

Unit 1 Problem SolvingSyllabus

Prepared By Mr. V. S. Patil Dept (CSE)/AEC

2

1Understand the logic of algorithm and flow of

control.

Understand the basics symbols of flowcharts

Objectives

Algorithm

•An algorithm is well defined, finite set of computational

instructions that accomplishes a particular task, which may or

may not take inputs and produces some value or a set of

values as output. In addition all algorithms must satisfy the

following criteria:

1.Zero or more quantities are externally supplied: input

2.At least one quantity is produced: output

3.Each instruction is clear and unambiguous: Definiteness

4.The algorithm terminates after a finite number of steps:

Finiteness

5.For every input instance, it halts with the correct output:

Correct.

Conventions used in writing Algorithms

•Name of Algorithm

•Introductory Comments

•Steps

•Comments

1 Step 1: [input the three integers]read x, y, z

.

Step 2: [compute the largest of three numbers]big = x;If(y>big) big=yIf(z>big) big=z

Step 3: [Write the largest number]Write (big)

Step 4: [Finished] exit

Algorithm Development

Ex- Find maximum of 3 numbers. The variables used are:x, y, z : type integerbig : Storing the value of the largest number, type integer

2

3

4

Flowchart

• Definition:- A flowchart is graphical or pictorial

representation of an algorithm.

• It shows the logic of algorithm and flow of control.

• Flowchart uses the symbol to represent the specific

action and arrow to indicate the flow of control.

Basic symbols of flowchart

Oval

Parallelogram

Rectangle

Diamond

Hybrid

Name Symbol Use in Flowchart

Denotes the beginning or end of the program

Denotes an input operation

Denotes an output operation

Denotes a decision (or branch) to be made. The program should continue along one of two routes. (e.g. IF/THEN/ELSE)

Denotes a process to be carried oute.g. addition, subtraction, division etc.

Flow line Denotes the direction of logic flow in the program

• Notice there are three types of symbols in this flowchart:

–rounded rectangles

–parallelograms

–a rectangle

• Each symbol represents a different type of operation.

Basic Flowchart Symbols

•Sequence

•Decision

•Repetition

•Case

Four Flowchart Structures

A series of actions are performed in sequence

The pay-calculating example was a sequence flowchart.

Ex.- Write an Flowchart for finding the addition of two number.

Sequence Structure

Decision Structure

•One of two possible actions is taken, depending on a condition.

•A new symbol, the diamond, indicates a yes/no question. If the

answer to the question is yes, the flow follows one path. If the

answer is no, the flow follows another path.

•Ex-

if (x < y)

a = x * 2;

else

a = x + y;

A repetition structure represents part of the program that repeats.

This type of structure is commonly known as a loop.

A loop tests a condition, and if the condition exists, it performs an

action.

Then it tests the condition again. If the condition still exists,

the action is repeated. This continues until the condition no

longer exists.

•Ex- while (x < y)

x++;

Repetition Structure

x < y? Add 1 to

x

YES

Case Structure

One of several possible actions is taken, depending on the

contents of a variable.

Ex- Find maximum of 3 nos

Question Bank

1.What is algorithm?

2. Explain Steps involved in algorithm development with

suitable example.

3.What is flowchart?

4.Describe the symbol of flowcharts.

5.Draw flowchart for Fibonacci Series.