0% found this document useful (0 votes)
5 views40 pages

Algorithms and Flowcharts

The document explains algorithms and flowcharts as tools for programming, detailing their definitions, examples, and advantages. It covers the characteristics of algorithms, the basic symbols used in flowcharts, and common programming errors such as syntax and logical errors. Additionally, it discusses source code, object code, and executable code, highlighting their differences and roles in programming.

Uploaded by

hardikrozra
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
Download as pptx, pdf, or txt
0% found this document useful (0 votes)
5 views40 pages

Algorithms and Flowcharts

The document explains algorithms and flowcharts as tools for programming, detailing their definitions, examples, and advantages. It covers the characteristics of algorithms, the basic symbols used in flowcharts, and common programming errors such as syntax and logical errors. Additionally, it discusses source code, object code, and executable code, highlighting their differences and roles in programming.

Uploaded by

hardikrozra
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1/ 40

Algorithm and Flowchart

• The algorithm and flowchart are two types of tools to explain the
process of a program.
• Algorithms and flowcharts are two different tools that are helpful for
creating new programs, especially in computer programming.
• An algorithm is a step-by-step analysis of the process, while a
flowchart explains the steps of a program in a graphical way.
Definition of Algorithm
• Writing a logical step-by-step method to solve the problem is called
the algorithm.
• In other words, an algorithm is a procedure for solving problems. In
order to solve a mathematical or computer problem, this is the first
step in the process.
• An algorithm includes calculations, reasoning, and data processing.
• Algorithms can be presented by natural languages, pseudocode, and
flowcharts, etc.
• The word Algorithm means “a process or set of rules to be followed in
calculations or other problem-solving operations”. Therefore
Algorithm refers to a set of rules/instructions that step-by-step define
how a work is to be executed in order to get the expected results.
• An algorithm is a step-by-step description of how to solve any given
problem.
• Alternately, one could say that the algorithm of every given program
is a step-by-step solution description of the problem or program that
has been presented.
• In order to be considered an algorithm, a sequence of instructions
must have the following characteristics:
Each and every one of the written instructions ought to be clear and
unmistakable.
Each instruction should be carried out at a predetermined interval.
There should not be an unlimited amount of instructions that are repeated.
The intended end result must be achieved after carrying out all of the steps
outlined in the instructions.
Example of an Algorithm
• Create an algorithm for calculating the sum of the first N numbers

1. Read the value of n


2. Put i=1, sum=0
3. if(i>n), go to step 7
4. Update s = s + i
5. Update i = i+1
6. go to step 3
7. Print the value of s
8. Stop
• Start
• Declare variables a,b and c.
• Read variables a,b and c.
• If a > b
• If a > c
• Display a is the largest number.
• Else
• Display c is the largest number.
• Else
• If b > c
• Display b is the largest number.
• Else
• Display c is the greatest number.
• Stop
Example 4: Find Roots of a Quadratic Equation ax2 + bx + c
=0
• Start
• Declare variables a, b, c, D, x1, x2, rp and ip;
• Calculate discriminant
• D ← b2-4ac
• If D ≥ 0
• r1 ← (-b+√D)/2a
• r2 ← (-b-√D)/2a
• Display r1 and r2 as roots.
• Else
• Calculate real part and imaginary part
• rp ← -b/2a
• ip ← √(-D)/2a
• Display rp+j(ip) and rp-j(ip) as roots
• Stop
• Basic Symbols used in Flow Chart
• Standardization of these fundamental flow chart symbols has been
completed by the American National Standard Institute (ANSI).
• Terminal
• In order to denote the beginning and the conclusion of the program, the
terminal symbol is utilized. The following is the sign for the terminal:
• Input/Output
• The input and output of an operation are typically represented as two
sides of a parallelogram. The following is a demonstration of an
input/output symbol that may be used in a flowchart:
• Processing Symbol
• The rectangle symbol is required to be used whenever there is a need
to denote a processing operation in a flowchart. The following is a
demonstration of the symbol for the rectangle that is used in creating
flowcharts to represent activities involving storage or arithmetic:
• Decision Symbol

• It is necessary to make use of a box in the shape of a diamond in


order to denote the stage of the decision-making process. This is the
demonstration symbol for a box in the shape of a diamond:
• Flow of Lines
• A program's logic flow can be analyzed by tracing the path taken by
the lines of code. It is necessary to draw lines in the spaces between
each box on the flowchart to show the direction in which the program
will proceed. The example of the line flow symbol is as follows:
• Connector Symbol
• When connecting the various components of a flowchart, we need to
draw a circle. The demo symbol for a circle that is used in a flowchart
looks like this:
• Create a flowchart to determine the greatest of three numbers
• Make a flowchart that computes the sum of the first N numbers
• Advantages of Flow Charts
• Here are a few of the most important reasons why you should use flow charts
before applying any solution to a program:
• Effective Analysis: Using a flow chart, any problem can be analyzed in a more
effective way.
• Communication: Flowcharts are a much better way of communicating the
logic of a system to all parties concerned.
• Efficient Coding: Every flowchart acts as a guide or blueprint during the
systems analysis and program development phases.
• Proper Debugging: The flowcharts help in the debugging process.
• Efficient Program Maintenance: The maintenance of operating programs
becomes very easy using flowcharts.
• Proper Documentation: Program flowcharts serve as good program
documentation, which is needed for various purposes.
• Disadvantages of Flow Charts
• Here is a list of some primary or main disadvantages of using
flowcharts for any given problem:
• Complex logic: Sometimes the given program logic is quite
complicated. In that case, it becomes a very difficult task to design or
draw a flowchart for that problem or program. In that case,
algorithms help a lot.
• Alterations and modifications: If we want to alter or change any logic
of the program in the flowchart, then we have to re-draw the whole
flowchart, which is very irritating.
Syntax and Logical Error
• Error is an illegal operation performed by the user which results in
abnormal working of the program.
Programming errors often remain undetected until the program is
compiled or executed.
• Some of the errors inhibit the program from getting compiled or
executed. Thus errors should be removed before compiling and
executing.
• Syntax errors: Errors that occur when you violate the rules of writing
C/C++ syntax are known as syntax errors. This compiler error indicates
something that must be fixed before the code can be compiled. All
these errors are detected by compiler and thus are known as compile-
time errors.
Most frequent syntax errors are:
• Missing Parenthesis (})
• Printing the value of variable without declaring it
• Missing semicolon like this:
#include<stdio.h>
void main()
{
int x = 10;
int y = 15;

printf("%d", (x, y)) // semicolon missed


}
#include<stdio.h>
int main(void)
{
// while() cannot contain "." as an argument.
while(.)
{
printf("hello");
}
return 0;
}
• Logical Errors : On compilation and execution of a program, desired
output is not obtained when certain input values are given.
• These types of errors which provide incorrect output but appears to
be error free are called logical errors.
• These are one of the most common errors done by beginners of
programming.
• These errors solely depend on the logical thinking of the programmer
and are easy to detect if we follow the line of execution and
determine why the program takes that path of execution.
• Common Types of Logical Error:
• While writing C code, several logical mistakes might happen. Several
instances include:
Incorrect application of logical and relational operators
Misunderstanding the operators' associativity and order of
precedence
Using conditions in loops and conditional expressions incorrectly
Incorrect variable initialization
Using variable names or types that are incorrect
• Examples:-
• Misuse of pointers:
• Wrong pointer assignments or dereferencing null pointers are examples of
pointer-related problems that might lead to unpredictable behaviour, cause
your program to crash, or result in wrong output. Always make sure that
pointers are correctly initialised before using them.
• Incorrect conditions assignment:
• A common mistake is to assign a value to a conditional expression rather than
compare the values. Your program may branch improperly as a result of
employing a single equal sign (=) rather than a double equals sign (==) for
comparison.
• Nesting of control structures incorrectly:
• If, else, or loop constructions that are improperly nested can produce
undesirable results and muddled code flow. To prevent adding logical errors, be
cautious when aligning your code and make sure that control structures are
correctly nested.
// C program to illustrate
// logical error
int main()
{
int i = 0;

// logical error : a semicolon after loop


for(i = 0; i < 3; i++);
{
printf("loop ");
continue;
}
getchar();
return 0;
}
• Divide by Zero Errors
• It is a common problem that at the time of dividing any number,
programmers do not check if a divisor is zero and finally it creates a
runtime error.
• Run-time Errors : Errors which occur during program execution(run-
time) after successful compilation are called run-time errors. One of
the most common run-time error is division by zero also known as
Division error. These types of error are hard to find as the compiler
doesn’t point to the line at which the error occurs
• Semantic errors : This error occurs when the statements written in
the program are not meaningful to the compiler.

// C program to illustrate
// semantic error
void main()
{
int a, b, c;
a + b = c; //semantic error
}
#include<stdio.h>
void main()
{
int n = 9, div = 0;

// wrong logic
// number is divided by 0,
// so this program abnormally terminates
div = n/0;

printf("result = %d", div);


}
• What is a Source Code?
• Source code refers to high-level code or assembly code that is generated by
humans/programmers. Source code is easy to read and modify.
• It is written by the programmer by using any High-Level Language or
Intermediate language which is human-readable.
• The source code contains comments that the programmer makes for better
understanding.
• Source code is provided to a language translator which converts it into machine-
understandable code which is called machine code or object code.
• The computer can not understand direct source code, a computer understands
machine code and executes it.
• It is considered a fundamental component of a computer. In simple we can say
source code is a set of instructions/commands and statements which is written
by a programmer by using a computer programming language like C, C++, Java,
Python, Assembly language, etc.
• So statements written in any programming language are termed source code.
• Key Features of Source Code
• They are coded in high-level programming languages.
• Frequently found in a format that can be read and understood by
human beings.
• Has to be assembled or set out to be usable on a machine.
• Both are fixed and alterable by developers.
• What is an Object Code?
• Object code refers to low-level code that is understandable by machine.
Object code is generated from source code after going through a
compiler or other translator.
• It is in executable machine code format. Object code contains a
sequence of machine-understandable instructions that which
Central Processing Unit understands and executes.
• The computer does not understand the source program or the source
code. Therefore, the compiler converts the source program into an
object program.
• It contains the instructions for the machine to perform. These
instructions are in the form of binary digits. The machine understands
this object code.
• Therefore, the computer can execute this code. Generally, the object
code is specific to the system architecture.
• Note:-
• Furthermore, if the programmer does any modifications to the source
code, it is necessary to compile the program to make those changes
appear in the object code.
• Key Features of Object Code
• It is coded in machine language which is in the form of binary language
of 0s and 1s.
• It can only be read by another computer or other programmed
machines and is not comprehensible by human beings.
• Executed directly from the computer hardware and can be operated on
with complex logical as well arithmetic operations.
• Created by linking the source code files to create an executable file with
the help of a compiler utility program.
• What is Executable Code
• Executable code is a file or a program that indicates tasks according to
encoded instructions. The CPU can directly execute an executable file
to defined tasks.
• In other words, it is machine code instructions for a physical CPU. As
a CPU can directly execute an object code, we can also consider the
object code as an executable code.
• Furthermore, it is sometimes possible to consider a bytecode or
scripting language instructions as an executable code.
• Difference Between Object Code and Executable Code
• Object code is a sequence of statements in binary that is generated
after compiling the source program. In contrast, an executable code is
a file or a program that indicates tasks according to encoded
instructions which are directly executed by the CPU.

You might also like