0% found this document useful (0 votes)
28 views

C Language Chapter 1

Uploaded by

Ekta Singh
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views

C Language Chapter 1

Uploaded by

Ekta Singh
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Structured Programming through ‘C’

ALGORITHM
Introduction to Algorithm

Every programming language has its own construction or instructions to understand. The
most popular technique used to get a plan for the solution of a given problem after analyzing in
multiple dimensions is an algorithm.

An algorithm is a step by step representation of a given problem in English like


statements. Algorithm is a problem solving activity.

Standard definition of an algorithm

An algorithm consist of set of explicit unambiguous, finite steps, which takes input and
produce output definitely in a finite time.

Every algorithm must be made up of following steps.

Step1: Start

Step2: Body of the algorithm.

Step3: Stop.

Factors of an Algorithm

Every algorithm is influences with the following five factors.

1. Input
2. Output
3. Definiteness.
4. Finiteness.
5. Effectiveness.

Input: Input is nothing but the data given to the problem.

Output: Output is nothing but the problem generated result.

Definiteness: The algorithm should definitely find the solution for the given problem.

Finiteness: The number of steps in algorithm is finite.

Effectiveness: The efficient one that find the solution for a given problem than remaining
solutions.

Examples for an Algorithm:

1. Write an algorithm to print “Hello World” on the console.


Department of MCA, B.V.R.I.C.E Page 1
Structured Programming through ‘C’

Step1: Start

Step2: Print “Hello World” by using printf statement.

Step3: Stop.

2. Write an algorithm to find the addition of two numbers.

Step1: Start

Step2: Input two values into the variables ‘a’ and ‘b’ from the keyboard.

Step3: Find the addition of ‘a’ and ‘b’ i.e., a+b and assign the resultant value to the variable ‘c’

Step4: Print the addition value ‘c’.

Step5: Stop

3. Write an algorithm to swap two numbers by using the third variable.

Step1: Start

Step2: Read ‘a’ and ‘b’ values from the keyboard.

Step3: Before swapping print the values of ‘a’ and ‘b’.(For example a=10 and b=20).

Step4: Assign the value of ‘a’ to the variable ‘temp’ i.e., temp=a.

Step5: Assign the value of ‘b’ to ‘a’ i.e., a=b.

Step6: Assign the value of ‘temp’ to ‘b’ i.e., b=temp.

Step7: After swapping print the values of ‘a’ and ’b’.(In this a=20 and b=10).

Step8: Stop.

Q. Explain about the Top-Down Approach and Step-wise refinement.

Top-Down Approach

The One of the programming approach that has proven to be most productive is called
top-down approach or top-down decomposition. Top-down decomposition is the process of
breaking the overall procedure or task into component parts (modules) and then subdivides each
component module until the lowest level of detail has been reached. It is called top-down design
or top-down decomposition since we start "at the top" with a general problem and design specific
solutions to its sub problems. In order to obtain an effective solution for the main problem, it is
desirable that the sub problems (subprograms) should be independent of each other. Then each
sub-problem can be solved and tested by itself. This is shown in below

Department of MCA, B.V.R.I.C.E Page 2


Structured Programming through ‘C’

Using this method, a complex problem is separated into simpler parts, which can be
programmed easily.

Example top-down decomposition

The payroll system of a company can contain the following modules or tasks

• Master file

• Earnings

• Deductions

• Taxing

• Net earning

• Print reports

The tasks given above can be represented in a hierarchical chart as shown below.

Department of MCA, B.V.R.I.C.E Page 3


Structured Programming through ‘C’

Identifying and diagramming the relationship between modules in this fashion allows
programmers to focus on the overall organization and logic of the program.

It is the set of principles that enable a problem to be solved by breaking it down into
manageable parts, step-by-step from the overall problem specification.

Step-wise Refinement

Stepwise refinement is a top-down design strategy. The program is implemented by


successively refining levels of procedural detail. In each step of the refinement, one or several
instructions of the given program are decomposed into more detailed instructions. This
successive decomposition or refinement terminates when all instructions are expressed in terms
of any underlying computer or programming language.

Every refinement step implies some design decisions. It is important that the programmer
is aware of the underlying criteria.

Department of MCA, B.V.R.I.C.E Page 4

You might also like