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

1 Algorithm

An algorithm is a set of well-defined steps to solve a problem or complete a task. Algorithms have properties like inputs, outputs, being finite, correct, and precise. Flowcharts can be used to represent algorithms visually using standard symbols. Programming languages allow humans to communicate instructions to computers and come in machine languages, assembly languages, and high-level languages.

Uploaded by

api-3769732
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPS, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
327 views

1 Algorithm

An algorithm is a set of well-defined steps to solve a problem or complete a task. Algorithms have properties like inputs, outputs, being finite, correct, and precise. Flowcharts can be used to represent algorithms visually using standard symbols. Programming languages allow humans to communicate instructions to computers and come in machine languages, assembly languages, and high-level languages.

Uploaded by

api-3769732
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPS, PDF, TXT or read online on Scribd
You are on page 1/ 11

ALGORITHM

Definition & Properties

In mathematics and computing, an algorithm is a procedure (a finite set of well-defined instructions) for
accomplishing some task which, given an initial state, will terminate in a defined end-state. Generally the
word is used to include all definite procedures for solving problems or performing tasks.

Algorithms are essential to the way computers process information, because a computer program is
essentially an algorithm that tells the computer what specific steps to perform (in which sequence) in
order to carry out a specified task, such as calculating employees’ salaries or printing students’ result
cards.

Alternatively an algorithm may be defined as a specific set of instructions for carrying out a procedure or
solving a problem, usually with the requirement that the procedure terminate at some point. Algorithms
can be expressed in many kinds of notation, including natural languages like English or Gujarati,
flowcharts, and programming languages like C. Informally, the concept of an algorithm is often
illustrated by the example of a recipe, although many algorithms are much more complex; algorithms
often have steps that repeat (iterate) or require decisions (such as comparison).

Based on above discussion, an algorithm has following properties:


• Zero or more Inputs
• One or more Outputs
• Finiteness
• Correctness
• Preciseness
Developing well-known algorithms

Example of an algorithm to find whether the given number is odd or even

Step 1: BEGIN
Step 2: Read the value of n
Step 3: Divide n by 2 and store the remainder in rem
Step 4: If rem is 0, go to step 7
Step 5: Print "n is an odd number"
Step 6: Go to step 8
Step 7: Print "n is an even number"
Step 8: END

Example of an algorithm to find the greatest of three numbers

Step 1: BEGIN
Step 2: Read the value of a,b,c
Step 3: If a>b, go to step 4 else go to step 7
Step 4: If a>c, go to step 5 else go to step 6
Step 5: Print "a is greatest", go to step 9
Step 6: Print "c is greatest", go to step 9
Step 7: If b>c, go to step 8 else go to step 6
Step 8: Print "b is greatest"
Step 9: END
Example of an algorithm to find whether the given number is prime or composite

Step 1: BEGIN
Step 2: Read the value of n
Step 3: If n=2, go to step 9
Step 4: Let I=3, Repeat steps 5 through 8
Step 5: Divide n by I and store the remainder in rem
Step 6: If rem is 0, Print "n is a composite number", go to step 10
Step 7: Increment I by 1
Step 8: If I < n, go to step 5
Step 9: Print "n is a prime number"
Step 10: END

Example of an algorithm to find whether the given number is a multiple of 7

Step 1: BEGIN
Step 2: Read the value of n
Step 3: Divide n by 7 and store the remainder in rem
Step 4: If rem is 0, go to step 5, else go to step 6
Step 5: Print "n is a multiple of 7", go to step 7
Step 6: Print "n is not a multiple of 7“
Step 7: END
FLOWCHARTING
Meaning of a flowchart
A flowchart is a diagrammatic representation that illustrates the sequence of operations to be performed
to get the solution of a problem. Flowcharts are generally drawn in the early stages of formulating
computer solutions. These flowcharts play a vital role in the programming of a problem and are quite
helpful in understanding the logic of complicated and lengthy problems. Once the flowchart is drawn, it
becomes easy to write the program in any high level language. Often we see how flowcharts are helpful
in explaining the program to others. Hence, it is correct to say that a flowchart is a must for the better
documentation of a complex program.
Symbols used to draw flowcharts
Flowcharts are usually drawn using some standard symbols; however, some special symbols can also be
developed when required. Some standard symbols, which are frequently required for flowcharting, are
given below:

Input or output Decision


Start or End of a program making and
operation
branching

Computational steps or processing Connector


function of a program or joining Flow line
two parts of a
program
Flowchart to find greatest of three numbers

Start

Input a, b, c

Is a > b?
YES NO

Is a > c? Is b > c?
NO NO

Output c is greatest
YES YES

Output a is greatest Output b is greatest

End

End End
Flowchart to find sum of n numbers

Start

Input n

I=0
SUM = 0

I=I+1
SUM = SUM + I

Is I < n?
YES

NO

Output SUM

End
Advantages and disadvantages of using flowcharts

The benefits of flowcharts:


•Communication: Flowcharts are better way of communicating the logic of a system to all concerned.
•Effective analysis: With the help of flowchart, problem can be analyzed in more effective way.
•Proper documentation: Program flowcharts serve as a good program documentation
•Efficient Coding: The flowcharts act as a guide during program development phase.
•Proper Debugging: The flowcharts help in debugging process.
•Efficient Program Maintenance: The maintenance of program becomes easy with the help of flowchart.

Limitations of using flowcharts:


•Complex logic: Sometimes, the program logic is quite complicated. In that case, flowchart becomes
complex and difficult to understand.
•Alterations and Modifications: If alterations are required the flowchart may require re-drawing
completely.
PROGRAMMING LANGUAGES
A programming language is a language used to write computer programs, which instruct a computer to
perform some kind of computation. Programming languages differ from natural languages in that natural
languages are only used for interaction between people, while programming languages also allow
humans to communicate instructions to machines.
The main purpose of programming languages is to provide instructions to a computer. As such,
programming languages differ from most other forms of human expression in that they require a greater
degree of precision and completeness. When using a natural language to communicate with other
people, human authors and speakers can be ambiguous and make small errors, and still expect their
intent to be understood. However, computers do exactly what they are told to do, and cannot understand
the code the programmer "intended" to write. The combination of the language definition, the program,
and the program's inputs must fully specify the external behavior that occurs when the program is
executed. Like human languages, programming languages have syntactic and semantic rules used to
define meaning. Programming languages are used to facilitate communication about the task of
organizing and manipulating information, and to express algorithms precisely.
Programmers write instructions in various programming languages, some directly understandable by
computers and others requiring intermediate translation steps. Hundreds of computer languages are in
use today. These may be divided into three general types:
1.Machine languages
2.Assembly languages
3.High-level languages
Any computer can directly understand only its own machine language. Machine language is the “natural
language” of a computer and as such is defined by its hardware design. Machine language generally
consists of string of numbers that instruct computers to perform their most elementary operations one at
a time. Machine languages are machine dependent that is a particular machine language can be used on
only one type of computer. Such languages are cumbersome for humans, as they are in formats given
below. The following section of a machine-language program adds overtime salary to actual salary and
stores the result in gross salary:

011101001 // Load actual_salary


000111101 // Add overtime_salary
011010010 // Store gross_salary
Machine language programming was simply very slow, tedious and error-prone for most programmers.
Instead of using the strings of numbers that computers could directly understand, programmers began
using English-like abbreviations to represent elementary operations. These abbreviations formed the
basis of assembly languages. Translator programs called assemblers were developed to convert early
assembly-language programs to machine language at computer speeds. The following section of an
assembly-language program adds overtime salary to actual salary and stores the result in gross salary:

Load actual_salary
Add overtime_salary
Store gross_salary
Although such code is clearer to humans, it is incomprehensible to computers until translated to machine
language. An assembler is a translator program that translates assembly-language programs to machine
language. Computer usage increased rapidly with the arrival of assembly languages, but programmers
still had to use many instructions to accomplish even simpler tasks. To speed up the programming
process, high-level languages were developed in which single statements could be written to accomplish
big tasks. Translator programs called compilers convert high-level language programs into machine
language. High-level languages allow programmers to write instructions that look almost like everyday
English and contain commonly used mathematical notations. A program that calculates gross salary may
be written in a high-level language as follows:

gross_salary = actual_salary + overtime_salary;

From the programmer’s viewpoint, high-level languages are preferable to machine and assembly
language. C, C++ and Java are among the most widely used high-level programming languages.

The program that translates a high-level language program into machine language is called a compiler.
The process of compiling a high-level language program into machine language can take a considerable
amount of time. Interpreters were developed to execute high-level language programs directly.
Procedural Programming Vs. Object Oriented Programming
Divisions into functions : When programs become larger, a long single Introduction of objects : The fundamental idea
list of instructions is difficult to understand and so it is broken down into behind object-oriented languages is to combine
smaller units. For this reason, function was adopted as a way to make into a single unit both data and functions that
programs more comprehensible to their human creators. Later on, the idea operate on that data. Such a unit is called an
of breaking a program into functions can be further extended by grouping object.
a number of functions together into a large entity called a module.

Poor programming model : There are two related problems with Introduction of encapsulation : In OOP, an
procedural programming. First, functions have unrestricted access to object’s functions, called member functions,
global data. Second, unrelated functions and data. In large programs, typically provide the only way to access its data.
where there are many functions and many data variables, it becomes You cannot access data directly. The data is
difficult to manage both. Any accidental alteration to the global data may hidden, so it is safe from any accidental
change the logic of the program and thus result in an error. change. Data and functions are said to be
encapsulated into a single entity.

Poor real-world modeling : Third and more important problem with the Better real-world modeling : Complex real
procedural program paradigm is that its arrangement of separate data and world objects have both attributes and
functions does a poor job of modeling things in the real world. In the behavior. A car can have attributes like Color,
physical world we deal with objects such as people and cars. Such objects ModelNo etc. It also behaves in a certain
are not like data and they are not like functions. manner when we speed up or slow down. This
is better handled by objects.
New data types & extensibility : We cannot create new data types in Allows user-defined data types and thus
procedural programming. Computer languages have many built-in data extensible : By introduction of objects and
types but we cannot invent a data type of our own by mixing some or all operator overloading, you can not only create
of the built-in types. In other words, you cannot extend programs of your data types of your own but also make them
own. behave as similar as do built-in types.

You might also like