B Naresh, B Naresh, B Naresh, B Naresh, Dept., Of Computer ScienceDept., Of Computer ScienceDept., Of Computer ScienceDept., Of Computer Science, BVRICE, BVRICE, BVRICE, BVRICE Page 1 UNIT-IV
INTRODUCTION TO ALGORITHMS AND PROGRAMMING LANGUAGES
Algorithm:
An algorithm is a set of instructions for solving a problem.
The algorithm gives the logic of the program, that is, a step-by-step description of how to arrive at a solution.
Algorithms are mainly used to achieve Software re-use. Once we have an idea or a blueprint of a solution, we can implement it in any high-level language, such as C, C++, Java, and so on.
Control Structures used in Algorithms:
Generally an algorithm uses three control structures, namely Sequence, decision, and repetition.
Sequence:
Sequence means that each step of the algorithm is executed in the specified order.
An algorithm to add two numbers is shown below.
Algorithm to add two numbers:
Step 1: Input the first number as A
Step 2: Input the second number as B
Step 3: Set Sum=A+B
Step 4: Print Sum.
Step 5: End
Decision:
Decision statements are used when the execution of a process depends on the outcome of some condition.
B Naresh, B Naresh, B Naresh, B Naresh, Dept., Of Computer ScienceDept., Of Computer ScienceDept., Of Computer ScienceDept., Of Computer Science, BVRICE, BVRICE, BVRICE, BVRICE Page 2
If condition then process
A condition in this context is any statement that may evaluate either to a true or a false. A decision statement can also be stated in the following manner:
If(condition) then
process1
else
process2
This form is commonly known as the if-else construct. Here, if the condition is true then process1 is executed, else process2 is executed.
Example: Algorithm to test the equality of two numbers
Step 1: Input the first number as A
Step 2: Input the second number as B Step 3: if A=B
then print “Equal” else
Print “Not equal” Step 4: end
Repetition:
Repetition, which involves executing one or more steps for a number of times, can be implemented using constructs such as the while, do-while, and for loops. These loops execute one or more steps until some condition is true.
Example: Algorithm to print the first 10 natural numbers:
Step 1: [initialize] Set I=1, N=1
Step 2: Repeat steps 3 and 4 while I<=N
Step 3: Print I
Step 4: Set I=I+1
B Naresh, B Naresh, B Naresh, B Naresh, Dept., Of Computer ScienceDept., Of Computer ScienceDept., Of Computer ScienceDept., Of Computer Science, BVRICE, BVRICE, BVRICE, BVRICE Page 3
Some more Algorithms:
1. Write an algorithm to find the larger of two numbers
Step 1: Input first number as A
Step 2: Input second number as B
Step 3: if A>B
Then print A
Else if A<B
Then print B
Else
Print “The numbers are equal”
Step 4: End
2. Write an algorithm to find whether a number is even or odd
Step 1: Input first number as A
Step 2: if A%2=0
Then print “Even”
Else
Then print “Odd”
Step 3: End
B Naresh, B Naresh, B Naresh, B Naresh, Dept., Of Computer ScienceDept., Of Computer ScienceDept., Of Computer ScienceDept., Of Computer Science, BVRICE, BVRICE, BVRICE, BVRICE Page 4
Flowcharts
A flowchart is a graphical or symbolic representation of a process. It is basically used to design a document for complex processes to help the viewers to visualize the logic of the process, so that they can understand the process, flaws, and other features within it. Various symbols used in the flowchart are
Symbol Purpose Description
Flow line Used to indicate the flow of logic by connecting
symbols.
Terminal(Stop/Start) Used to represent start and end of flowchart.
Input/output Used for input and output operation.
Processing Used for arithmetic operations and
data-manipulations.
Decision Used to represent the operation in which there
are two alternatives, true and false.
On-page Connector Used to join different flow line
Off-page Connector Used to connect flowchart portion on different
page. Predefined
Process/Function
Used to represent a group of statements performing one processing task.
Significance of Flowcharts:
B Naresh, B Naresh, B Naresh, B Naresh, Dept., Of Computer ScienceDept., Of Computer ScienceDept., Of Computer ScienceDept., Of Computer Science, BVRICE, BVRICE, BVRICE, BVRICE Page 5
Example 1: Flow chart to add two numbers
B Naresh, B Naresh, B Naresh, B Naresh, Dept., Of Computer ScienceDept., Of Computer ScienceDept., Of Computer ScienceDept., Of Computer Science, BVRICE, BVRICE, BVRICE, BVRICE Page 6
Example 3: Flowchart to determine the largest of three numbers
Advantages and Disadvantages of Flowchart
• Flowchart analyses the problem in a more effective manner.
• They used for program documentation.
• Flowchart act as blueprint for the programmers to code the solution in any programming language.
• They can be used to debug programs that have errors.
Disadvantages:
• Drawing a flowchart is a time consuming activity.
• Flowchart of a complex program becoming complex and clumsy.
• A little bit of alteration in the solution may require complete re drawing of the flow chart.
B Naresh, B Naresh, B Naresh, B Naresh, Dept., Of Computer ScienceDept., Of Computer ScienceDept., Of Computer ScienceDept., Of Computer Science, BVRICE, BVRICE, BVRICE, BVRICE Page 7
PSEUDO CODE
Pseudo code is a form of structured English that describes algorithms. It describing the entire logic of algorithm, so that it can be translates into a programming language.
Keywords used while writing Pseudo codes
For looping and selection, the keywords that are Do While...EndDo; Do Until...Enddo; Case...EndCase; If...Endif; Call ... with (parameters); Call; Return ....; Return;
Do not include data declarations in your pseudo code.
IF-THEN-ELSE
The general form is:
IF condition THEN
sequence 1
ELSE
sequence 2
ENDIF
The ELSE keyword and "sequence 2" are optional. If the condition is true, sequence 1 is performed, otherwise sequence 2 is performed.
Example
IF age>18 THEN
Display Eligible to vote
ELSE
Display Not Eligible
B Naresh, B Naresh, B Naresh, B Naresh, Dept., Of Computer ScienceDept., Of Computer ScienceDept., Of Computer ScienceDept., Of Computer Science, BVRICE, BVRICE, BVRICE, BVRICE Page 8
CASE
A CASE construct indicates a multi-way branch based on conditions that are mutually exclusive.
The general form is:
CASE expression OF
condition 1 : sequence 1
condition 2 : sequence 2
...
condition n : sequence n
OTHERS:
default sequence
ENDCASE
Example
CASE day OF
0 : Print Sunday
1 : Print Monday
2 : Print Tuesday
3 : Print Wednesday
4 : Print Thursday
5 : Print Friday
6 : Print Saturday
B Naresh, B Naresh, B Naresh, B Naresh, Dept., Of Computer ScienceDept., Of Computer ScienceDept., Of Computer ScienceDept., Of Computer Science, BVRICE, BVRICE, BVRICE, BVRICE Page 9
WHILE
The WHILE construct, is used to specify a loop with a test at the top. The beginning and ending of the loop are indicated by two keywords WHILE and ENDWHILE. The general form is:
WHILE condition
sequence
ENDWHILE
The loop is entered only if the condition is true. After each iteration, the condition will be tested, and loop will continue as long as the condition is true.
Example
WHILE i<10
Print i
Increment i
ENDWHILE
REPEAT-UNTIL
This loop is similar to the WHILE loop except that the test is performed at the bottom of the loop instead of at the top. Two keywords, REPEAT and UNTIL are used. The general form is:
REPEAT
sequence
UNTIL condition
B Naresh, B Naresh, B Naresh, B Naresh, Dept., Of Computer ScienceDept., Of Computer ScienceDept., Of Computer ScienceDept., Of Computer Science, BVRICE, BVRICE, BVRICE, BVRICE Page 10
FOR
This loop is for iterating a sequence for a specific number of times. Two keywords, FOR and ENDFOR are used. The general form is:
FOR iteration bounds sequence ENDFOR
Example
FOR each student in the class Add 10 as bonus marks ENDFOR
Example Pseudo Codes
1. Write a pseudo code for calculating the price of a product after adding sales tax to its original price.
Solution
1. Read the price of the product
2. Read the sales tax rate
3. Calculate sales tax = price of the item * sales tax rate 4. Calculate total price= price of the product + sales tax 5. Print total price
6. End
2. Write a pseudo code to read the marks of 10 students, if marks are greater than 50, the student is passes, else the student fails.
1. Set pass to 0 2. Set fail to 0
3. Set no of students to 0 4. WHILE no of students < 10
a. Input the marks
b. IF marks >= 50 then
Set pass=pass+1
ELSE
Set fail=fial+1
END IF
END WHILE
5. End
B Naresh, B Naresh, B Naresh, B Naresh, Dept., Of Computer ScienceDept., Of Computer ScienceDept., Of Computer ScienceDept., Of Computer Science, BVRICE, BVRICE, BVRICE, BVRICE Page 11
GENERATIONS OF PROGRAMMING LANGUAGES
First Generation: Machine Language
Machine language is the lowest level of programming language and is the only language that a computer understands. All the commands and data values are expressed using 0’s and 1’s.
The main advantage of machine language is that the execution of the code is very
fast and efficient since it is directly executed by the CPU.
The main drawback is machine language is very difficult to learn and more
difficult to identify the error occurs.
The code written in machine language is not portable, and to transfer the code to a
different computer, it needs to be completely rewritten because machine language for one computer is different from other computer.
Advantages Disadvantages
• Code can be directly executed by the
computer.
• Execution is fast and efficient.
• Programs can be written to
efficiently utilize memory.
• Code is difficult to Write.
• Code is difficult to understand by other people.
• Code is difficult to maintain.
• It is difficult to detect and
correct errors.
• Code is machine dependent and
B Naresh, B Naresh, B Naresh, B Naresh, Dept., Of Computer ScienceDept., Of Computer ScienceDept., Of Computer ScienceDept., Of Computer Science, BVRICE, BVRICE, BVRICE, BVRICE Page 12
Second Generation: Assembly language
The second generation programming language, or 2GL, is assembly language.
Assembly language developed in the mid 1950’s.
In assembly language we are using the symbolic notation instead of machine
language instructions. Example ADD for add , CMP for compare, and MUL for multiply.
An assembly language program consist of series of instructions. Each instruction consists of label, an operation code and one or more operands.
The instructions in the assembly language will vary from machine to machine,
because the language is directly related to the internal architecture of the computer.
Assembly language is very efficient in terms of execution time and main
memory usage.
Programs written in assembly language need a translator known as assembler, to convert assembly language to machine language.
The following instructions are part of an assembly language language code to illustrate addition of two numbers:
MOV AX,4 Stores the value 4 in the AX register of the CPU
MOV BX,6 Stores the value 6 in the BX register of the CPU
ADD AX,BX Adds the contents of the AX and BX registers and stores the result in the AX register.
The following are the advantages and disadvantages of assembly language:
Advantages Disadvantages
• It is easy to understand.
• It is easier to write programs in
assembly language than in
machine language.
• It is easy to detect and correct errors.
• It is easy to modify.
• It is less prone to errors.
• Code is machine dependent and
thus non-portable.
• Programmers must have a good
knowledge of the hardware and internal architecture of the CPU.
• The code cannot be directly
B Naresh, B Naresh, B Naresh, B Naresh, Dept., Of Computer ScienceDept., Of Computer ScienceDept., Of Computer ScienceDept., Of Computer Science, BVRICE, BVRICE, BVRICE, BVRICE Page 13
Assembler:
The following diagram shows the working of assembler.
The assembler takes the assembly language program as input and gives a code in machine language as output. If there is an error , the assembler gives a list of errors. The object file is created only when the assembly language code is free from errors.
Third Generation: High- level language
The third generation of programming languages is not closely related to the
internal architecture of the computer. Hence these languages are referred to as high level languages.
Some high level languages were designed to a specific purpose such as controlling
industrial robots, whereas other languages were designed for general purpose to develop applications. The general purpose languages are BASIC, FORTRAN, Pascal, and COBOL, C++ or java.
A translator is needed to translate the instructions written in high level language into machine language. Such translators are commonly known as interpreters and compilers.
The 3GLs make it easy to write and debug a program and give a programmer
more time to think about its overall logic.
Programs written in the high level languages are portable between the machine. For example, a program written in C can be complied and executed on any computer that has a standard C compiler.
Advantages Disadvantages
• The Code is machine independent.
• It is easy to learn and use the language.
• There are few errors.
• It is easy to document and understand the code.
• It is easy to maintain the code.
• It is easy to detect and correct errors.
• The code is less
efficient.
• It is difficult to write a code that controls the
CPU,memory and
B Naresh, B Naresh, B Naresh, B Naresh, Dept., Of Computer ScienceDept., Of Computer ScienceDept., Of Computer ScienceDept., Of Computer Science, BVRICE, BVRICE, BVRICE, BVRICE Page 14
Compiler
A compiler is a special type of program that transforms the source code written in a programming language in machine language. The resultant code in 0’s and 1’s known as the object code. The object code is used to create an executable program.
If the source code contains errors, then the compiler will not be able to generate the machine code. There are two types of errors , 1. Syntax errors 2. Logical errors.
The syntax errors are spelling mistakes, illegal characters and use of undefined variables. Logical errors are occurs the program does not function accurately.
The following diagram shows the compiler
INTERPRETER:
Like the compiler, the interpreter executes instructions written in a high level language. The interpreter takes one statement of high level code, translates it into machine level code, executes it and then takes the new statement and repeat the process until the entire program is translate.
B Naresh, B Naresh, B Naresh, B Naresh, Dept., Of Computer ScienceDept., Of Computer ScienceDept., Of Computer ScienceDept., Of Computer Science, BVRICE, BVRICE, BVRICE, BVRICE Page 15
LINKER
For example, a program is divided into various modules. When a module is compiled, an object file of those modules is generated. The linker a program, that combines the object modules to form an executable program.
LOADER
A loader is a special type of program that copies programs from a storage device to the main memory, where they can be executed.
Differences between compiler and interpreter
Compiler Interpreter
• It translates the entire program in one go.
• It generates errors after translating the entire program.
• Execution of code is faster
• An object file is generated.
• Code need not be recompiled
every time it is executed.
• It merely translates the code.
• It requires more memory space.
• It interprets and executes one
statement at a time
• It stops translation after getting the first error.
• Execution of code is slower as
every time reinterpretation of statements has to be done.
• No object file is generated
• Code has to be reinterpreted every time it is executed.
• It translates as well as executes the code
B Naresh, B Naresh, B Naresh, B Naresh, Dept., Of Computer ScienceDept., Of Computer ScienceDept., Of Computer ScienceDept., Of Computer Science, BVRICE, BVRICE, BVRICE, BVRICE Page 16
Fourth Generation : Very High -level language
The fourth generation programming language or non-procedural language. While writing a code using a procedural language, the programmer has to tell the computer how a task is done , while using a non procedural language , programmer define what they want to computer to do but they do not supply details of how it has to be done.
Example of 4GL is the query language , which allows a user to request information from a database with precisely worded English like sentences.
4GL is very simple to learn and work .The same task if written in C or any other 3GL would require multiple lines of code.
Fifth Generation Programming language
5GLs are designed to make the computer to solve a given problem without the programmer. While working with the 4GLs programmers have to write a specific code to do the work, but with a 5GL they only have worry about what problem need to be solved and what conditions need to be met, without worrying about how to implement a routine or an algorithm to solve them.
Fifth generation programming allows people can talk to computers and the voice recognition systems can convert spoken sounds into written words. Prolog and Mercury are the best known fifth-generation languages.
***********************************************************************
CATEGORIZATION OF HIGH-LEVEL LANGUAGES
High level languages can be categorized into four groups
High-level
language
Unstructured
programming
Structured
programming
Logic-oriented
programming
B Naresh, B Naresh, B Naresh, B Naresh, Dept., Of Computer ScienceDept., Of Computer ScienceDept., Of Computer ScienceDept., Of Computer Science, BVRICE, BVRICE, BVRICE, BVRICE Page 17
Unstructured Programming
In unstructured programming, programmers write small and simple programs consisting only one main program. Here main() consists of statements that modify the data that is global throughout the whole program. This technique is not good for writing large programs.
Structured Programming Language
Structured programming also referred to as modular programming. Structural programming follows a top down approach in which the overall program structure is broken down into separate modules.
B Naresh, B Naresh, B Naresh, B Naresh, Dept., Of Computer ScienceDept., Of Computer ScienceDept., Of Computer ScienceDept., Of Computer Science, BVRICE, BVRICE, BVRICE, BVRICE Page 18
Advantages
1. Structured programming can be easy to write , understand and modify
2. Many programmers can work on a single large program , with each work on a single large program
3. A structured program can be written in less time than an unstructured program
4. Individual procedures are easy to change as well as understand.
Logic oriented Programming Language
Logic oriented programming is different from other programming. Logic oriented programming based on formal predicate logic. The predicate logic describes the nature of a problem by defining the relationships between rules and facts. Prolog, LISP, and Data log are few examples of logic oriented programming languages.
The following example shows the Predicate logic that defines a sister relationship.
Sister(A,B): /* A is the sister of B if there are two people F and M for which */
Father(F,A), /* F is the father of A */
Father(F,B), /* F is the father of B */
Mother(M,A), /* M is the Mother of A */
Mother(M,B), /* M is the Mother of B*/
Female(A) /* A is a female */
Object oriented Programming
B Naresh, B Naresh, B Naresh, B Naresh, Dept., Of Computer ScienceDept., Of Computer ScienceDept., Of Computer ScienceDept., Of Computer Science, BVRICE, BVRICE, BVRICE, BVRICE Page 19
Concepts of object oriented programming
• Class
• Object
• Inheritance
• Polymorphism
• Abstraction
• Encapsulation
Class
A class is used to describe the structure and behavior of a set of similar objects. A class is a combination of properties (data) and methods (function).
For example , consider a class student , a student has attributes such as roll_number, name, course, and aggregate marks. The operations that can be performed on the student data may be get_details, set_details and edit_details.
Object
Object is knowing but instance of class. Every object contains some data and procedures. Two distinct objects can have the same set of values.
Inheritance
B Naresh, B Naresh, B Naresh, B Naresh, Dept., Of Computer ScienceDept., Of Computer ScienceDept., Of Computer ScienceDept., Of Computer Science, BVRICE, BVRICE, BVRICE, BVRICE Page 20
Polymorphism
Polymorphism means having several different forms. There are two types of polymorphism
1. Function overloading – The same function name can be used for different functions but different arguments and different return types.
2. Operator overloading – For example a+b will give the result of adding a and b. If we overload the + operator to be used with strings , then str1 + str2 gives the result str2 concatenated with str1.
Data abstraction
Data abstraction refers to the process of defining data and functions in such way that only the essential details are provided and implementation details are hidden.
Encapsulation
Encapsulation organizes the data and methods into structure that prevent data access by any function not specified in the class.
********************************************************************
SOME POPULAR HIGH-LEVEL LANGUAGES
BASIC
BASIC (Beginner’s All purpose Symbolic Instruction Code) is a general purpose , high level programming language developed in 1964. BASIC was specifically designed to students in fields other than science and mathematics to use computers. It was easy to learn and use and was a very powerful language that was used for a wide range of applications.
FORTRAN
FORTRAN (Formula Translation) is one of the oldest general purpose programming languages. It was developed in 1957 at IBM by a team of programmers led by John Boakus. The programmers can easily develop the code and able to write programs 500 percent faster. Software developers to focus more on the problem solving aspects of a problem than on the coding aspect.
PASCAL
B Naresh, B Naresh, B Naresh, B Naresh, Dept., Of Computer ScienceDept., Of Computer ScienceDept., Of Computer ScienceDept., Of Computer Science, BVRICE, BVRICE, BVRICE, BVRICE Page 21
C
C is a high level general purpose programming language that was developed in the 1970’s by Dennis Ritchie at Bell Labs. It was originally designed for system programming. It can be used for wide range of applications varying from business programs and engineering.
The following reasons C language is a popular
• The C language programs occupy less memory.
• It is easy to understand.
• The C language programs are easily portable.
• The C language is closer to assembly language as well as low level feature
C++
C++ is a general purpose programming language developed by “Bjarne Strou Strup” staring in 1979 at Bell Labs. Like C , C++ is an intermediate level language , it support both high level and low level language features.
C++ is a very popular programming language and can be implemented on a wide variety of hardware and operating system platforms.
C++ support all features of C and also include other new features such as classes , objects, polymorphism, inheritance etc.,
JAVA
Java is general purpose , object oriented programming language developed by Sun Microsystems in 1995. Java is similar to C++ , it is more powerful than not only C++ but laso many other high level languges.
Java is first language to bring animation and Internet based applications.
Java is used in the following ares:
• Internet applications such as playing online games and chatting with friends
• View images in three dimensions.
Java programmers have to write a code only once and same code can be executed on any platform without any modification
LISP
B Naresh, B Naresh, B Naresh, B Naresh, Dept., Of Computer ScienceDept., Of Computer ScienceDept., Of Computer ScienceDept., Of Computer Science, BVRICE, BVRICE, BVRICE, BVRICE Page 22
manipulate symbolic expressions rather than numbers makes it convenient for artificial intelligence applications and for simulation of games.
********************************************************************
Important questions:
LAQ’s:
1. Explain High Level Programming languages
2. Define algorithm and explain its functions with examples.
3. Write about the Generations of Programming Languages.
SAQ’s
1. What is flowchart? Explain various symbols of flowchart