You are on page 1of 9

PROBLEM SOLVING IN C UNIT – I I Semester B.Sc.

Introduction to Algorithms and Programming Languages

Algorithm
An algorithm is a step-by-step procedure to solve a given problem or an
algorithm as a set or list of instructions for carrying out some process step by step.

Features of Algorithm
An algorithm should have the below mentioned characteristics

• Input − An algorithm should have 0 or more well defined inputs.


• Output − An algorithm should have 1 or more well defined outputs, and
should match the desired output.
• Finiteness − Algorithms must terminate after a finite number of steps.
• Feasibility − Should be feasible with the available resources.
• Independent − An algorithm should have step-by-step directions which
should be independent of any programming code.
• Unambiguous − Algorithm should be clear and unambiguous. Each of its
steps, and their input/outputs should be clear and must lead to only one
meaning.
An algorithm must have one starting point and one or more ending point. Its
starting point can be labelled as START and its ending point can be labelled as STOP.

Department of Computer Science 1 A.S.N. Degree College, Tenali


PROBLEM SOLVING IN C UNIT – I I Semester B.Sc.

Example 1: Algorithm for adding two numbers:

Step 1: Start.
Step 2: Take three variables with names num1, num2, num3.
Step 3: Input the values for two variables: num1 = 10, num2 = 5.
Step 4: A
dd num1 & num2 and put its value to num3:
num3 = num1 + num2.
Here both operands num1 and num2 are added and its combined value is assigned
to num3.
Step 5: Now print the value of num3.
Step 6: Stop.

Example2: Algorithm for of finding biggest number of two numbers with one

START and two STOP


Step 1: START.
Step 2: Take two variables with names num1, num2.
Step 3: Read num1 and num2.
Step 4: if num1>num2 then
Step 5: print num1.
Step 6: STOP.
Step 7: else
Step 8: print num2.
Step 9: STOP.

Department of Computer Science 2 A.S.N. Degree College, Tenali


PROBLEM SOLVING IN C UNIT – I I Semester B.Sc.

Pseudo code:

Pseudo Code is simply an implementation of an algorithm in the form of


annotations and informative text written in plain English. It has no syntax like any of
the programming language . It can’t be compiled or interpreted by the computer.

Advantages of Pseudo code


• Improves the readability of any approach. It’s one of the best approaches to
start implementation of an algorithm.

• Acts as a bridge between the program and the algorithm or flowchart. Also
works as a rough documentation, so the program of one developer can be
understood easily when a pseudo code is written out. In industries, the
approach of documentation is essential. And that’s where a pseudo-code
proves vital.

• The main goal of a pseudo code is to explain what exactly each line of a
program should do, hence making the code construction phase easier for the
programmer.

In the problem-solving phase of computer programming, we will design


algorithms. This means that we will have to be conscious of the strategies we use to
solve problems in order to apply them to programming problems. These algorithms
can be designed though the use of flowcharts or pseudo code.

Example 1: Pseudo code for addition of two numbers

1. Read num1.num2

2. Compute num3 = num1 + num2

3. Print num2

Department of Computer Science 3 A.S.N. Degree College, Tenali


PROBLEM SOLVING IN C UNIT – I I Semester B.Sc.

Example 2: Pseudo Code for Computing greatest of two numbers

1. Read Num1, Num2


2. If (Num1>num2) then
Print Num1
Else
Print Num2

Flow Charts

A flowchart is a graphical representation of steps. It was originated from


computer science as a tool for representing algorithms and programming logic. It
shows steps in sequential order and is widely used in presenting the flow of
algorithms, workflow or processes.

Flowcharting Symbols

There are 6 basic symbols commonly used in flowcharting Terminal, Process,


input/output, Decision, Connector and Predefined Process.

Symbol Name Function

Process Indicates any type of internal operation


inside the Processor or Memory

input/output Used for any Input/Output(I/O)operation.


Indicates that the computer is to obtain
data or output results

Decision Used to ask a question that can


be answered in a binary format (Yes/No,
True/False)

Department of Computer Science 4 A.S.N. Degree College, Tenali


PROBLEM SOLVING IN C UNIT – I I Semester B.Sc.

Connector Allows the flowchart to be drawn


without intersecting lines or
without a reverse flow.

Predefined Process Used to invoke a subroutine or


An interrupt program.

Terminal Indicates the starting or ending of


the program, process, or interrupt
program.

Flow Lines Shows direction of flow.

Rules for Flow Charting

1. All boxes of the flowchart are connected with Arrows.

2. Flowchart symbols have an entry point on the top of the symbol with no
other entry points. The exit point for all flowchart symbols is on the bottom
except for the Decision symbol.

3. The Decision symbol has two exit points; these can be on the sides or the
bottom and one side.

4. Generally a flowchart will flow from top to bottom.

5. Connectors are used to connect breaks in the flowchart.

6. Subroutines and Interrupt programs have their own and independent


flowcharts.

7. All flow charts start with a Terminal or Predefined Process symbol.

8. All flowcharts end with a terminal or a contentious loop.

Department of Computer Science 5 A.S.N. Degree College, Tenali


PROBLEM SOLVING IN C UNIT – I I Semester B.Sc.

Example: Develop a Flow Chart for adding two numbers

START

READ NUM1,
NUM2

NUM3 = NUM1 +NUM2

PRINT NUM3

END

Department of Computer Science 6 A.S.N. Degree College, Tenali


PROBLEM SOLVING IN C UNIT – I I Semester B.Sc.

Pseudo Code for Flow Chart

1. Read Num1, Num2

2. Compute Num3 = Num1 + Num2

3. Print Num3

Generations of programming language

Programming languages have been developed over the years in a phased manner. The
programming language in terms of their performance reliability and robustness can
be grouped into five different generations.
1. First generation languages (1GL)
2. Second generation languages (2GL)
3. Third generation languages (3GL)
4. Fourth generation languages (4GL)
5. Fifth generation languages (5GL)

1. First Generation Language (Machine language)

The first generation programming language is also called low-level


programming language or machine language because they were used to program the
computer system at a very low level of abstraction. i.e. at the machine level. In the
machine language, a programmer only deals with binary codes.

Advantages of first generation language

• They are translation free and can be directly executed by the computers.
• The programs written in these languages are executed very speedily and
efficiently by the CPU of the computer system.
• The programs written in these languages utilize the memory in an efficient
manner because it is possible to keep track of each bit of data.

Department of Computer Science 7 A.S.N. Degree College, Tenali


PROBLEM SOLVING IN C UNIT – I I Semester B.Sc.

2. Second Generation language: Assembly Language


The second generation programming language also belongs to the
category of low-level- programming language. The second generation language
comprises assembly languages that use the concept of mnemonics for the writing
program. In the assembly language, symbolic names are used to represent the opcode
and the operand part of the instruction.

Advantages of second generation language

• It is easy to develop understand and modify the program developed in these


languages, compared to those developed in the first generation programming
language.
• The programs written in these languages are less prone to errors and therefore
can be maintained with a great case.

3. Third Generation languages: High-Level Languages


The third generation programming languages were designed to
overcome the various limitations of the first and second generation programming
languages. The languages of the third and later generation are considered as a high-
level language because they enable the programmer to concentrate only on the logic
of the programs without considering the internal architecture of the computer system.

Advantages of third generation programming language


• It is easy to develop, learn and understand the program.
• As the programs written in these languages are less prone to errors they are
easy to maintain.
• The program written in these languages can be developed in very less time
as compared to the first and second generation language.
Examples: FORTRAN, ALGOL, COBOL, C++, C

Department of Computer Science 8 A.S.N. Degree College, Tenali


PROBLEM SOLVING IN C UNIT – I I Semester B.Sc.

4. Fourth generation language: Very High-level Languages

The languages of this generation were considered as very high-level


programming languages required a lot of time and effort that affected the productivity
of a programmer. The fourth generation programming languages were designed and
developed to reduce the time, cost and effort needed to develop different types of
software applications.

Advantages of fourth generation languages


• These programming languages allow the efficient use of data by implementing
the various databases.
• They require less time, cost and effort to develop different types of software
applications.
• The programs developed in these languages are highly portable as compared
to the programs developed in the languages of other generation.
Examples: SQL, CSS, coldfusion

5. Fifth generation language: Artificial Intelligence Language


The programming languages of this generation mainly focus on
constraint programming. The major fields in which the fifth generation programming
language are employed are Artificial Intelligence and Artificial Neural Networks.
Advantages of fifth generation languages
• These languages can be used to query the database in a fast and efficient
manner.
• In this generation of language, the user can communicate with the computer
system in a simple and an easy manner.
Examples: mercury, prolog, OPS5

Department of Computer Science 9 A.S.N. Degree College, Tenali

You might also like