Вы находитесь на странице: 1из 24

Problem solving Techniques and Logic Development using C

Algorithm

Algorithm
An Algorithm refers to the logic of

program. Step by step description to arrive at the solution. Designed in such manner that if the steps are executed in the specified sequence, the desired result will be obtained.

From Algorithms to Programs


Problem

Algorithm: A sequence
of instructions describing how to do a task (or process)

C Program

Definition of Algorithm
In Computer Science Algorithm means, a precise method usable by computer for achieving the solution of a problem. It is the list of procedure or steps for solving a problem or to arrive at the solution.

What is a good Algorithm?


A good algorithm must be Correct - it computes the answer it is

supposed to; Feasible - each step can be executed in reasonable time Efficient - the algorithm uses as little running time and memory space as possible within the limits of an acceptable development time.

Properties of Algorithm
Correct always returns the desired output for all legal instances of the problem. Unambiguous Precise Efficient Can be measured in terms of Time Space Time tends to be more important

Rules of Algorithm
It should have an input. Steps of algorithm should be executable

by the computer. Time taken by steps must be finite and small. It must produce an output after executing the steps. Should be written in very simple language.

Role in Problem Solving


A problem can be solved by using a computer only

if an algorithm can be written for it. While writing an algorithm we can identify The step-by-step procedure The major decision points The variables necessary to solve

the

problem
Problem is reduced into a series of smaller

problems of more manageable size. Decision making becomes a more rational process. With the use of an algorithm, the same specified steps are used for performing the tasks that makes the process more consistent and reliable.

Examples of Algorithm
A cooking recipe Assembly instructions for a model The rules of how to play a game

VCR instructions
Description of a martial arts technique Directions for driving from A to B A knitting pattern A car repair manual

Algorithm Example 1

To find the sum of two numbers

To Add Two Numbers


Step 1: Start

Step 2: Take two different variables.


Step 3: Initialize two variables.

Step 4: Add those numbers.


Step 5: Store addition in third variable. Step 6: Display the value of third

variable. Step 7: End

Algorithm Example 2
To find largest of numbers. five

To Find the largest of 5 Num.

Defining actions in Algorithm

FindLargest Refined

Generalization of FindLargest

FindLargest Algorithm
1) Start 2) Read 5 numbers 3) Set largest to first number 4) if the next number is greater than largest then Set Largest to next number 5) Repeat Step (4) for 4 times 6) Display largest 7) End

Exercise
1. Write an algorithm to convert the temperature the temperature in Centigrade to Temperature in Fahrenheit. 2. Write an algorithm to display the sum of first ten natural numbers.

Three Constructs
Any algorithm can be developed using three constructs Sequence Decision Repetition

Sequence Construct
A series of steps or statements that are executed in the order they are written. do action 1 do action 2 do action N

Decision Construct
Defines one or more courses of action

depending on the evaluation of a condition. A condition is an expression that is either true or false. if condition is true do action 1 else do action 1 end if

Repetition Construct
Allows one or more statements to be repeated as long as a given condition is true. These statements will make up what is known as the body of a loop. While (condition is true) do action 1 do action 2 do action N End_while

Exercise
1. Write an algorithm to display the sum of first ten natural numbers. 2. Write an algorithm to display the table of given number.

Вам также может понравиться