Cse 20cs11t U5 Notes
Cse 20cs11t U5 Notes
• ALGORITHM
• An algorithm is defined as a finite sequence of instructions that when
provided with a set of input values produces an output and then
terminates.
• To be an algorithm, the steps must be unambiguous and after a finite number
of steps, the solution of the problem should be achieved.
• However, algorithms can have steps that repeat (iterate) or require decisions
(logic and comparison) until the task is completed.
• The need of an algorithm can be understood as it provides a logical
structure to plan the solution.
1. Algorithm for adding two numbers
Step 1: Start
Step 2: Declare variables num1, num2 and sum.
Step 3: Read values for num1, num2.
Step 4: Add num1 and num2 and assign the result to a variable sum.
Step 5: Display sum
Step 6: Stop
Step 3: Answer = A - B
Step 5: Stop
1. Start
2. Read three numbers A, B, C
3. Find the larger number between A and B and store it in MAX_AB
4. Find the larger number between MAX_AB and C and store it in MAX
5. Display MAX=4
6. Stop
1. Start
2. Read three numbers A=1, B=3, C=5
3. Find the smaller number between A and B and store it in MIN_AB=1
4. Find the smaller number between MIN _AB=1 and C=5 and store it in MIN=1
5. Display MIN=1
6. Stop
Step 1 : Start
Step 2 : Read how many numbers are to be considered for sum i.e addition &
store it in variable N
Step3 : Initialize the variable sum=0 & count=0
Step 4: Read the first number & store it in A
Step 5: Compute sum=sum+A
Step 6 :Compute count=count+1
Step 7: Check whether the value of COUNT is less than n i.e “count<N”, if
yes repeat the steps 4,5,6 other go to next step i,e 8
Step 8 : Print sum
Step 9 : Stop
FLOWCHART
• A flowchart is a pictorial representation of an algorithm.
• Here each steps are drawn in the form of different shapes of boxes and the
logical flow is indicated by arrows or flow lines.
• The boxes represent operations and the arrows represent the sequences
• The use of the flowchart is to help the programmer understand the logic of the
program.
Importance of Flowcharts
• A flowchart helps to check how to work on problem and how they could be
improved.
• It help to draw clear lines between where one process ends and the next one
starts.
• Flowchart Symbols
• Flowchart Structures
• Sequence
• As the name implies, in a sequence structure, the instructions are computed one
by one.
• Selection
• A selection structure allows the program to make a choice between two alternate
paths, depending upon the condition, whether it is true or false.
The flowchart, finds out the maximum / LARGEST of three numbers.
• Repetition
• Repetition or loop pattern causes an interruption in the normal sequence of
processing and directs the system to loop back to a previous statement in the
program, repeating the same sequence over and again, usually with new data.
Example: The flowchart, finds out the average of N numbers.
Generations of Languages
There are five generation of Programming languages. They are:
First Generation Language: Machine language
▪ These are low-level languages like machine language.
▪ Computers are electronic devices which can understand only binary codes.
▪ These binary codes are 0s and 1s also called as machine codes.
▪ Machine language is the only language a computer is able to understand.
▪ Language translators are not used here.
▪ This language is machine dependent.
▪ They are difficult to remember and understand for human beings
Ex: 00000011001
Advantages of machine language:
• Translation free: They are translation free and can be directly executed by the
computers. Because computer can easily understand.
• High Speed: The programs written in these languages are executed very fast
as no translation is required.
• Efficient memory usage: The programs written in these languages utilize the
memory in an efficient manner.
Disadvantages of machine language:
• Machine dependent: A program developed in one computer cannot be run in
another computer. So it is very costly.
• Complex Or difficult language: It is difficult to read and write this
language.
• Error prone: Since programmer has to remember all instructions in binary
code, more chances of making errors.
Second Generation Languages [2GL]: Assembly language
▪ These assembly languages allows user to interact directly with hardware.
▪ These use mnemonic codes.
▪ Better readable than binary or machine language.
▪ Assembly language is also low-level programming language for a computer
▪ These are machine dependent languages.
▪ Translators called assemblers are used here.
▪ Assemblers are translators used to translate Assembly language into machine
language Ex: NASM, MASM, etc.
▪ It looks like MOV A ,B
• In this generation of language, the user can communicate with the computer
system in a simple and an easy manner.
Variables
•
Variables are the names given to computer memory locations.
•
They are used to store values in a computer program.
Examples of valid variable names:
age, gender, x25, reg_num, _book.
Floating-point Literals
A floating-point literal has an integer part, a decimal point, a fractional part, and an
exponent part. Example:3.14159
Character Constants
Character literals are enclosed in single quotes, e.g., 'x' can be stored in a simple
variable of char type.
String Literals
String literals or constants are enclosed in double quotes " ".
“Hello”.