KEMBAR78
2 Algorithm, Flowchart | PDF | Algorithms | Computer Engineering
0% found this document useful (0 votes)
27 views38 pages

2 Algorithm, Flowchart

Uploaded by

kushal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views38 pages

2 Algorithm, Flowchart

Uploaded by

kushal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 38

Algorithm and Flowchart

Review
✓Problem solving

✓Logic and its importance in problem solving

✓Computational problems and its classifications

✓Computer organization and operating system

✓Different computer languages

✓Typical C program development environment

23-07-2025 CSE 1071 Problem Solving using Computers (PSUC)-2025-26 2


Application software, System software & Hardware

23-07-2025 CSE 1071 Problem Solving using Computers (PSUC)-2025-26 3


Objectives for today
To learn and appreciate the following concepts

✓Introduction to algorithms and flowcharts

✓Algorithms and flowcharts for simple problems


✓Simple C programs

23-07-2025 CSE 1071 Problem Solving using Computers (PSUC)-2025-26 4


Session outcome
• At the end of session the student will be able to write

• Algorithms and flowcharts for simple problems

• Simple C programs

23-07-2025 CSE 1071 Problem Solving using Computers (PSUC)-2025-26 5


Algorithm
✓A step by step procedure to solve a particular problem

✓Named after Arabic Mathematician Abu Jafar Mohammed Ibn Musa Al


Khowarizmi

23-07-2025 CSE 1071 Problem Solving using Computers (PSUC)-2025-26 6


Algorithmic Notations
Name of the algorithm [mandatory]
[gives a meaningful name to the algorithm based on the problem]
Start [Beginning of the algorithm]
Step Number [mandatory]
[indicate each individual simple task]
Explanatory comment [optional]
[gives an explanation for each step, if needed]
Termination [mandatory]
[tells the end of algorithm]

23-07-2025 CSE 1071 Problem Solving using Computers (PSUC)-2025-26 7


Properties of an algorithm

23-07-2025 CSE 1071 Problem Solving using Computers (PSUC)-2025-26 8


Steps to develop an algorithm

23-07-2025 CSE 1071 Problem Solving using Computers (PSUC)-2025-26 9


Compute the area of circle
Name of the algorithm : Compute the area of a circle
Step1: Start
Step 2: Input radius
Step 3: [Compute the area]
Area  3.1416 * radius * radius
Step 4: [Print the Area]
Print ‘Area of a circle =‘, Area
Step 5: [End of algorithm]
Stop

23-07-2025 CSE 1071 Problem Solving using Computers (PSUC)-2025-26 10


Interchange values of two variables
Name of the algorithm: Interchange values of 2 variables
Step 1: Start
Step 2: Input A,B
Step 3: temp  A
Step 4: AB
Step 5: Btemp
Step 6: Print ‘A=’ , A
Print ‘B=’ , B
Step 7: [End of Algorithm]
Stop

23-07-2025 CSE 1071 Problem Solving using Computers (PSUC)-2025-26 11


Largest of 3 numbers
Name of the algorithm: Find largest of 3 numbers
Step 1: Start Step 5: [Compare A and C for largest]
Step 2: [Read the values of A, B and C]
if A>C then
Read A, B, C
Print ‘A’ is largest’
Step 3: [Compare A and B] else
if A>B Go to step 5
Print ‘C’ is largest’
Step 4: [Otherwise compare B with C] Step 6: [End of the algorithm]
if B>C then Stop
Print ‘B’ is largest’
else
Print ‘C’ is largest’
Go to Step 6

23-07-2025 CSE 1071 Problem Solving using Computers (PSUC)-2025-26 12


Factorial of a number
Name of the algorithm: Compute the factorial of a number

Step1: Start
Step 2: Input N
Step 3: fact 1
Step 4: for count=1 to N in step of 1 do
begin
factfact*count
end
Step 5: Print ‘fact of N=‘, fact
Step 6: [End of algorithm]
Stop

23-07-2025 CSE 1071 Problem Solving using Computers (PSUC)-2025-26 13


Tutorial on Algorithms
Write the algorithm to
• Find the area of triangle when three sides are given.
• Add two integers
• Find the sum of digits of a number
• Find the Sum and Mean of first N natural numbers

23-07-2025 CSE 1071 Problem Solving using Computers (PSUC)-2025-26 14


Flowcharts
✓In Computer Science, Flow chart is used to represent algorithm which
basically provides a solution to any computational problem.

• Flowchart: A graphical/pictorial representation of computation

23-07-2025 CSE 1071 Problem Solving using Computers (PSUC)-2025-26 15


Basic Flowchart Symbols
http://www.wiley.com/college/busin/icmis/oakman/outline/chap05/images/f5_02.gif

23-07-2025 CSE 1071 Problem Solving using Computers (PSUC)-2025-26 16


Area of the circle
Name of the algorithm:
Compute the area of a circle Flowchart

Step1: Input radius

Step 2: [Compute the area]


Area  3.1416 * radius*radius

Step 3: [Print the Area]


Print ‘Area of a circle =‘, Area

Step 4: [End of algorithm]


Stop

23-07-2025 CSE 1071 Problem Solving using Computers (PSUC)-2025-26 17


Comparing two numbers
Flowchart
Name of the algorithm: Comparing 2 numbers
Step 1: Start
Step 2: Input num1, num2
Step 3: if num1 > num2 then
Print num1 is bigger
else
Print num2 is bigger
Step 4: end

23-07-2025 CSE 1071 Problem Solving using Computers (PSUC)-2025-26 18


Swapping two numbers
Name of the algorithm: Swapping 2 numbers Flowchart

Step1: Input two numbers


Step 2: [swapping]
temp=a
a=b
b=temp
Step 3: [Print]
Print ‘after swapping=‘, a, b
Step 4: [End of algorithm]
Stop

23-07-2025 CSE 1071 Problem Solving using Computers (PSUC)-2025-26 19


Find Factorial of given no
Name of the algorithm: Compute the factorial of a number

Step1: Start
Step 2: Input data
Step 3: factorial 1
Step 4: for i=1 to N in step of 1 do
begin
factorialfactorial*i
end
Step 5: Print ‘fact of N=‘, factorial
Step 6: [End of algorithm]
Stop

23-07-2025 CSE 1071 Problem Solving using Computers (PSUC)-2025-26 20


Key features of flow chart
✓Diagrammatic / visual / graphical representation of computation of an
algorithm/pseudo code

✓Easier to understand and analyze the problem and it’s solution before
programming

✓Machine independent

✓ Well suited for any type of logic

23-07-2025 CSE 1071 Problem Solving using Computers (PSUC)-2025-26 21


Tutorial
• Write the flowchart to find the area of triangle when three sides are
given
• Write the flowchart to add two integers

23-07-2025 CSE 1071 Problem Solving using Computers (PSUC)-2025-26 22


Summary
✓What is Algorithm and Flowchart

✓Writing algorithms and drawing flowcharts for simple problems

23-07-2025 CSE 1071 Problem Solving using Computers (PSUC)-2025-26 23


Problem Solving Using Computers
Steps in Problem Solving
Problem solving is a part of our day to day
activity.

• Algorithm
• Flowchart
• Code
• Documenting

23-07-2025 CSE 1071 Problem Solving using Computers (PSUC)-2025-26 24


23-07-2025 CSE 1071 Problem Solving using Computers (PSUC)-2025-26 25
General Structure of C program

23-07-2025 CSE 1071 Problem Solving using Computers (PSUC)-2025-26 26


General Structure of C program
1. Documentation section:
The documentation section consists of a set of comment lines giving the name of
the program, the author and other details, which the programmer would like to
use later.
2. Link section: The link section provides instructions to the compiler to link
functions from the system library such as using the #include directive.
3. Definition section: The definition section defines all symbolic constants such
using the #define directive.
4. Global declaration section: There are some variables that are used in more than
one function. Such variables are called global variables and are declared in the
global declaration section that is outside of all the functions. This section also
declares all the user-defined functions.

23-07-2025 CSE 1071 Problem Solving using Computers (PSUC)-2025-26 27


General Structure of C program
5. main () function section: Every C program must have one main function section. This
section contains two parts; declaration part and executable part
a) Declaration part: The declaration part declares all the variables used in the
executable part.
b) Executable part: There is at least one statement in the executable part. These two
parts must appear between the opening and closing braces. The program
execution begins at the opening brace and ends at the closing brace. The closing
brace of the main function is the logical end of the program. All statements in the
declaration and executable part end with a semicolon.
6. Subprogram section: If the program is a multi-function program then the subprogram
section contains all the user-defined functions that are called in the main () function.
User-defined functions are generally placed immediately after the main () function,
although they may appear in any order.
All sections, except the main () function section may be absent when they are not
required.
23-07-2025 CSE 1071 Problem Solving using Computers (PSUC)-2025-26 28
Hello world program

23-07-2025 CSE 1071 Problem Solving using Computers (PSUC)-2025-26 29


Formatting in C

• Statements are terminated with semicolons.

• Indentation to be used for increased readability.

• Free format: white spaces and indentation is ignored by compiler.

• New line is represented by \n (Escape sequence).

23-07-2025 CSE 1071 Problem Solving using Computers (PSUC)-2025-26 30


Formatting in C
• C is case sensitive – pay attention to lower and upper case letters
when typing !
• All keywords and standard functions are lower case
• Typing MAIN, Main etc instead of main is a compiler error

• Strings are placed in double quotes

23-07-2025 CSE 1071 Problem Solving using Computers (PSUC)-2025-26 31


C program for reading a number and display it on the screen
#include<stdio.h>
int main()
{
int num;
printf("\nEnter the number: ");
scanf("%d", &num);
printf("The number read is: %d", num);
return(0);
}

23-07-2025 CSE 1071 Problem Solving using Computers (PSUC)-2025-26 32


scanf()
• scanf ( ) is used to obtain the value from the user through an input
device (keyboard).

• Eg: scanf(“%d”, &number);


int main(){
int num;
printf("\nEnter the number: ");
scanf("%d", &num);
printf(“The number read is: %d", num);
return(0);
}
23-07-2025 CSE 1071 Problem Solving using Computers (PSUC)-2025-26 33
C program essentials: Format specifiers

Similarly, the format specifiers are used with printf( ) function for printing/displaying.
23-07-2025 CSE 1071 Problem Solving using Computers (PSUC)-2025-26 34
Input/ Output functions
• C provides the printf( ) to display the data on the output device
(monitor).

• It is included in stdio.h int main() {


int num;
• Examples are:
printf("\nEnter the number: ");
• printf("programming isscanf("%d",
an art"); &num);
printf("The number read is: %d", num);
• printf("%d", number);
return(0);
• printf("%f%f", p, q); }

23-07-2025 CSE 1071 Problem Solving using Computers (PSUC)-2025-26 35


Adding two integers
#include <stdio.h>
int main()
{/* start of function main */
int sum; /* variable in which sum will be stored */
int integer1; /* first number to be input by user */
int integer2; /* second number to be input by user */
printf( "Enter first integer\n" );
scanf( "%d", &integer1 ); /* read an integer */
printf( "Enter second integer\n" );
scanf( "%d", &integer2 ); /* read an integer */
sum = integer1 + integer2; /* assign total to sum */
printf( "Sum is %d\n", sum ); /* print sum */
return 0; /* indicate that program ended successfully */
} /* end of function main */

23-07-2025 CSE 1071 Problem Solving using Computers (PSUC)-2025-26 36


Syntax and Logical errors
➢Syntax errors: violation of programming language rules (grammar).
➢Detected by the compiler

➢Eg: printf (“hello world”) // semicolon missing

➢Logical errors: errors in meaning:


➢Programs are syntactically correct but don’t produce the expected output

➢User observes output of running program

23-07-2025 CSE 1071 Problem Solving using Computers (PSUC)-2025-26 37


Summary
✓Problem solving !

✓Program structure

✓Formatting

✓Input / Output

✓Simple C programs

23-07-2025 CSE 1071 Problem Solving using Computers (PSUC)-2025-26 38

You might also like