PINCU1l1


    UNIT-I

PROGRAMMING FUNDAMENTALS

Algorithms

To solve any problem a plan is needed. An algorithm is a part of the plan for the computer program.

What is an Algorithm?

Computer scientist Niklaus Wirth stated that:

Program = Algorithms + Data.

An algorithm is ‘an effective procedure for solving a problem in a finite number of steps’.

“Effective” means that an answer is found and it has a finite number of steps. A well-designed algorithm should give an answer and also it must be terminated.

Definition:

An Algorithm is a finite set of instructions for solving a problem.

Criteria to be followed by an Algorithm

The following is the criteria to be followed by an algorithm:

Input: There should be zero or more values which are to be supplied.

Output: At least one result is to be produced.

Definiteness: Each step must be clear and unambiguous.

Finiteness: If we trace the steps of an algorithm, then for all cases, the algorithm must terminate after a finite number of steps.

Effectiveness: Each step must be sufficiently basic that a person using only paper and pencil can in principle carry it out. In addition, not only each step is definite, it must also be feasible.

Example Write an algorithm for finding the sum of any two numbers.

Step 1. START

Step 2. PRINT “ENTER TWO NUMBERS”

Step 3. INPUT A, B

Step 4. C A + B

Step 5. PRINT C

Step 6. STOP

Strategy for Designing Algorithms : The strategy for designing algorithms involves 3 steps:

1. Investigation step

2. Top-Down development step

3. Step-wise refinement

Investigation step: The investigation phase determines the requirements for the problem solution. Here you need to:

I. Identify the outputs.

II. Identify the input variables.

III. Identify the decisions and conditions.

IV. Identify the processes.

V. Identify the environment.

Top–down development: It plans the solution by breaking it into smaller modules.

Devise the overall problem solution: Divide the problem solution into small modules.

Verify the feasibility of solution: Check that the modules together form the whole solution to the Problem.

Stepwise refinement: It further decomposes the modules, defines the procedures and also verifies their correctness.

Work out all the details for each module.

Decompose any module into further meaningful smaller pieces.

Group common processes together

Group common variables together.

Test each small procedure for its correctness


Flowchart

A flowchart is a program design tool which is used before writing the actual program.

Flowcharts are generally developed in the early stages of formulating computer solutions.

Flowchart is a graphical representation of an algorithm.

It makes use of symbols which are connected among them to indicate the flow of information and processing.

It will show the general outline of how to solve a problem or perform a task.

It is prepared for better understanding of the algorithm.

Flow lines have arrows to indicate the direction of the flow of control between the boxes.

The activity to be performed is written within the boxes in English.

Once the flowchart is drawn, it becomes easy to write the program in any high-level language

Example: Draw a flowchart to find largest of three numbers?

No comments:

Post a Comment