KEMBAR78
SPL 2 | Algorithms, Pseudo-code, and Flowchart | PDF
Structured
Programming
Language
ALGORITHM, PSEUDO CODE, FLOW CHART,
COMPILATION PROCESS
Mohammad Imam Hossain
Lecturer, CSE, UIU
Algorithm
• Complete step by step representation of the solution of the
problem, represented in English like language.
Pseudo
Code
• More formal representation than the algorithm
• Very close to actual programming language
Flow chart
• Uses many graphical symbols to represent the steps of the
solution of the problem.
Program
• A set of instructions
• Written in a particular sequence in a computer-related language
Problem:Write a program to find the average of n numbers.
Algorithm:
1. Read the value of n
2. Read n numbers
3. Add these n numbers
4. Divide the addition
result by n to get the
average
5. Print the average
Pseudo Code:
1. Start
2. Read n
3. sum  0
4. for i  1 to n do
5. read i-th number
6. sum  sum + i-th number
7. end for
8. average  sum/n
9. print average
10. stop
Flow Chart
• Graphical Symbols
Start, Stop
Read, Print
Processing statements
Conditional check
Direction of flow
Connectors
Problem:Write a program to find
the average of n numbers.
Flow Chart >>
start
Read n
sum  0
i  1
i<= n
?
Read numberi
sum  sum + numberi
ii+1
average  sum/n
Print average
stop
n=3 and the numbers are: 10,20,30
yes
no
Problem:Write a program to find
the average of n numbers.
Flow Chart >>
start
Read n
sum  0
i  1
i<= n
?
Read numberi
sum  sum + numberi
ii+1
average  sum/n
Print average
stop
n=3 and the numbers are: 10,20,30
yes
no
Problem:Write a program to find
the average of n numbers.
Flow Chart >>
start
Read n
sum  0
i  1
i<= n
?
Read numberi
sum  sum + numberi
ii+1
average  sum/n
Print average
stop
n=3 and the numbers are: 10,20,30
yes
no
Problem:Write a program to find
the average of n numbers.
Flow Chart >>
start
Read n
sum  0
i  1
i<= n
?
Read numberi
sum  sum + numberi
ii+1
average  sum/n
Print average
stop
n=3 and the numbers are: 10,20,30
yes
no
Problem:Write a program to find
the average of n numbers.
Flow Chart >>
start
Read n
sum  0
i  1
i<= n
?
Read numberi
sum  sum + numberi
ii+1
average  sum/n
Print average
stop
n=3 and the numbers are: 10,20,30
yes
no
Problem:Write a program to find
the average of n numbers.
Flow Chart >>
start
Read n
sum  0
i  1
i<= n
?
Read numberi
sum  sum + numberi
ii+1
average  sum/n
Print average
stop
n=3 and the numbers are: 10,20,30
yes
no
Problem:Write a program to find
the average of n numbers.
Flow Chart >>
start
Read n
sum  0
i  1
i<= n
?
Read numberi
sum  sum + numberi
ii+1
average  sum/n
Print average
stop
n=3 and the numbers are: 10,20,30
sum=0+10=10, i=2
yes
no
Problem:Write a program to find
the average of n numbers.
Flow Chart >>
start
Read n
sum  0
i  1
i<= n
?
Read numberi
sum  sum + numberi
ii+1
average  sum/n
Print average
stop
n=3 and the numbers are: 10,20,30
sum=0+10=10, i=2
yes
no
Problem:Write a program to find
the average of n numbers.
Flow Chart >>
start
Read n
sum  0
i  1
i<= n
?
Read numberi
sum  sum + numberi
ii+1
average  sum/n
Print average
stop
n=3 and the numbers are: 10,20,30
sum=0+10=10, i=2
yes
no
Problem:Write a program to find
the average of n numbers.
Flow Chart >>
start
Read n
sum  0
i  1
i<= n
?
Read numberi
sum  sum + numberi
ii+1
average  sum/n
Print average
stop
n=3 and the numbers are: 10,20,30
sum=0+10=10, i=2
sum=10+20=30, i=3
yes
no
Problem:Write a program to find
the average of n numbers.
Flow Chart >>
start
Read n
sum  0
i  1
i<= n
?
Read numberi
sum  sum + numberi
ii+1
average  sum/n
Print average
stop
n=3 and the numbers are: 10,20,30
sum=0+10=10, i=2
sum=10+20=30, i=3
yes
no
Problem:Write a program to find
the average of n numbers.
Flow Chart >>
start
Read n
sum  0
i  1
i<= n
?
Read numberi
sum  sum + numberi
ii+1
average  sum/n
Print average
stop
n=3 and the numbers are: 10,20,30
sum=0+10=10, i=2
sum=10+20=30, i=3
yes
no
Problem:Write a program to find
the average of n numbers.
Flow Chart >>
start
Read n
sum  0
i  1
i<= n
?
Read numberi
sum  sum + numberi
ii+1
average  sum/n
Print average
stop
n=3 and the numbers are: 10,20,30
sum=0+10=10, i=2
sum=10+20=30, i=3
sum=30+30=60, i=4
yes
no
Problem:Write a program to find
the average of n numbers.
Flow Chart >>
start
Read n
sum  0
i  1
i<= n
?
Read numberi
sum  sum + numberi
ii+1
average  sum/n
Print average
stop
n=3 and the numbers are: 10,20,30
sum=0+10=10, i=2
sum=10+20=30, i=3
sum=30+30=60, i=4
yes
no
Problem:Write a program to find
the average of n numbers.
Flow Chart >>
start
Read n
sum  0
i  1
i<= n
?
Read numberi
sum  sum + numberi
ii+1
average  sum/n
Print average
stop
n=3 and the numbers are: 10,20,30
sum=0+10=10, i=2
sum=10+20=30, i=3
sum=30+30=60, i=4
average=60/3=20
yes
no
Problem:Write a program to find
the average of n numbers.
Flow Chart >>
start
Read n
sum  0
i  1
i<= n
?
Read numberi
sum  sum + numberi
ii+1
average  sum/n
Print average
stop
n=3 and the numbers are: 10,20,30
sum=0+10=10, i=2
sum=10+20=30, i=3
sum=30+30=60, i=4
average=60/3=20
yes
no
Problem:Write a program to find
the average of n numbers.
Flow Chart >>
start
Read n
sum  0
i  1
i<= n
?
Read numberi
sum  sum + numberi
ii+1
average  sum/n
Print average
stop
n=3 and the numbers are: 10,20,30
sum=0+10=10, i=2
sum=10+20=30, i=3
sum=30+30=60, i=4
average=60/3=20
yes
no
Practice problems:
1. Draw flow chart to decide whether a number is even or odd.
2. Draw flow chart to find out the sum of the following series:
a) 1+3+5+… … + 99
b) 1+
1
3
+
1
5
+ … … +
1
27
c) 1+1+2+3+5+8+13+… … + upto 100th terms (Fibonacci series)
3. Draw flow chart to find out the factorial of n,
1*2*3*… … *n
4. Draw flow chart to find out the maximum of 3 numbers.
5. Draw flow chart to check whether a number is prime or not.
6. Draw flow chart to find out the gcd of 2 numbers.
GCD Problem:
15 ) 21 ( 1
15
---------------
6 ) 15 ( 2
12
--------------
3 ) 6 ( 2
6
------------
0
GCD Problem:
15 ) 21 ( 1
15
---------------
6 ) 15 ( 2
12
--------------
3 ) 6 ( 2
6
------------
0
a b
check
GCD Problem:
15 ) 21 ( 1
15
---------------
6 ) 15 ( 2
12
--------------
3 ) 6 ( 2
6
------------
0
a b
check
GCD Problem:
15 ) 21 ( 1
15
---------------
6 ) 15 ( 2
12
--------------
3 ) 6 ( 2
6
------------
0
a b
check
GCD Problem:
15 ) 21 ( 1
15
---------------
6 ) 15 ( 2
12
--------------
3 ) 6 ( 2
6
------------
0
a b
check
Start
input a , b
check b % a
check=0
?
ba
a check
print a
stop
No
Yes
check b % a
C Programs Building Process:
C program building process involves four stages. At
the end there should be a single executable file.
1. Preprocessing is the first pass of any C
compilation. It processes include-files, define,
conditional compilation instructions and macros.
Source Code
hello.c
C Preprocessor
C Programs Building Process:
C program building process involves four stages. At
the end there should be a single executable file.
1. Preprocessing is the first pass of any C
compilation. It processes include-files, define,
conditional compilation instructions and macros.
Source Code
hello.c
PreprocessedCode
hello.i
C Preprocessor
C Programs Building Process:
C program building process involves four stages. At
the end there should be a single executable file.
1. Preprocessing is the first pass of any C
compilation. It processes include-files, define,
conditional compilation instructions and macros.
Source Code
hello.c
PreprocessedCode
hello.i
C Preprocessor
C Compiler
2. Compilation is the second pass. It takes the
output of the preprocessor and the source
code and generates assembler source code.
C Programs Building Process:
C program building process involves four stages. At
the end there should be a single executable file.
1. Preprocessing is the first pass of any C
compilation. It processes include-files, define,
conditional compilation instructions and macros.
Source Code
hello.c
PreprocessedCode
hello.i
Assembly code
hello.s
C Preprocessor
C Compiler
2. Compilation is the second pass. It takes the
output of the preprocessor and the source
code and generates assembler source code.
C Programs Building Process:
C program building process involves four stages. At
the end there should be a single executable file.
1. Preprocessing is the first pass of any C
compilation. It processes include-files, define,
conditional compilation instructions and macros.
Source Code
hello.c
PreprocessedCode
hello.i
Assembly code
hello.s
C Preprocessor
C Compiler
Assembler
2. Compilation is the second pass. It takes the
output of the preprocessor and the source
code and generates assembler source code.
3. Assembly is the third stage that takes the
assembly source code and produces an assembly
listing with offsets.The assembler output is stored
in an object file.
C Programs Building Process:
C program building process involves four stages. At
the end there should be a single executable file.
1. Preprocessing is the first pass of any C
compilation. It processes include-files, define,
conditional compilation instructions and macros.
Source Code
hello.c
PreprocessedCode
hello.i
Object code
hello.o
Assembly code
hello.s
C Preprocessor
C Compiler
Assembler
2. Compilation is the second pass. It takes the
output of the preprocessor and the source
code and generates assembler source code.
3. Assembly is the third stage that takes the
assembly source code and produces an assembly
listing with offsets.The assembler output is stored
in an object file.
C Programs Building Process:
C program building process involves four stages. At
the end there should be a single executable file.
1. Preprocessing is the first pass of any C
compilation. It processes include-files, define,
conditional compilation instructions and macros.
Source Code
hello.c
PreprocessedCode
hello.i
Executable code
hello.exe
Object code
hello.o
Assembly code
hello.s
C Preprocessor
C Compiler
Assembler
Linker
2. Compilation is the second pass. It takes the
output of the preprocessor and the source
code and generates assembler source code.
3. Assembly is the third stage that takes the
assembly source code and produces an assembly
listing with offsets.The assembler output is stored
in an object file.
4. Linking is the final stage that takes one or more
object files or libraries as input and combines them to
produce a single (executable) file.
Library Files
Other Object files
Relocation object
code information
Compiling using command prompt:
1. Write your code into a text editor and name the file with .c extention.
2. Add your gcc compiler’s bin directory path into windows environment
variables path variable.
3. Open windows command prompt and write the following command
gcc –c “path of your .c file with file name”
This command will generate the object file with .o extension let,
object_file_name.o
4.Then give the following command
gcc –o exe_file_name object_file_name.o

SPL 2 | Algorithms, Pseudo-code, and Flowchart

  • 1.
    Structured Programming Language ALGORITHM, PSEUDO CODE,FLOW CHART, COMPILATION PROCESS Mohammad Imam Hossain Lecturer, CSE, UIU
  • 2.
    Algorithm • Complete stepby step representation of the solution of the problem, represented in English like language. Pseudo Code • More formal representation than the algorithm • Very close to actual programming language Flow chart • Uses many graphical symbols to represent the steps of the solution of the problem. Program • A set of instructions • Written in a particular sequence in a computer-related language
  • 3.
    Problem:Write a programto find the average of n numbers. Algorithm: 1. Read the value of n 2. Read n numbers 3. Add these n numbers 4. Divide the addition result by n to get the average 5. Print the average Pseudo Code: 1. Start 2. Read n 3. sum  0 4. for i  1 to n do 5. read i-th number 6. sum  sum + i-th number 7. end for 8. average  sum/n 9. print average 10. stop
  • 4.
    Flow Chart • GraphicalSymbols Start, Stop Read, Print Processing statements Conditional check Direction of flow Connectors
  • 5.
    Problem:Write a programto find the average of n numbers. Flow Chart >> start Read n sum  0 i  1 i<= n ? Read numberi sum  sum + numberi ii+1 average  sum/n Print average stop n=3 and the numbers are: 10,20,30 yes no
  • 6.
    Problem:Write a programto find the average of n numbers. Flow Chart >> start Read n sum  0 i  1 i<= n ? Read numberi sum  sum + numberi ii+1 average  sum/n Print average stop n=3 and the numbers are: 10,20,30 yes no
  • 7.
    Problem:Write a programto find the average of n numbers. Flow Chart >> start Read n sum  0 i  1 i<= n ? Read numberi sum  sum + numberi ii+1 average  sum/n Print average stop n=3 and the numbers are: 10,20,30 yes no
  • 8.
    Problem:Write a programto find the average of n numbers. Flow Chart >> start Read n sum  0 i  1 i<= n ? Read numberi sum  sum + numberi ii+1 average  sum/n Print average stop n=3 and the numbers are: 10,20,30 yes no
  • 9.
    Problem:Write a programto find the average of n numbers. Flow Chart >> start Read n sum  0 i  1 i<= n ? Read numberi sum  sum + numberi ii+1 average  sum/n Print average stop n=3 and the numbers are: 10,20,30 yes no
  • 10.
    Problem:Write a programto find the average of n numbers. Flow Chart >> start Read n sum  0 i  1 i<= n ? Read numberi sum  sum + numberi ii+1 average  sum/n Print average stop n=3 and the numbers are: 10,20,30 yes no
  • 11.
    Problem:Write a programto find the average of n numbers. Flow Chart >> start Read n sum  0 i  1 i<= n ? Read numberi sum  sum + numberi ii+1 average  sum/n Print average stop n=3 and the numbers are: 10,20,30 sum=0+10=10, i=2 yes no
  • 12.
    Problem:Write a programto find the average of n numbers. Flow Chart >> start Read n sum  0 i  1 i<= n ? Read numberi sum  sum + numberi ii+1 average  sum/n Print average stop n=3 and the numbers are: 10,20,30 sum=0+10=10, i=2 yes no
  • 13.
    Problem:Write a programto find the average of n numbers. Flow Chart >> start Read n sum  0 i  1 i<= n ? Read numberi sum  sum + numberi ii+1 average  sum/n Print average stop n=3 and the numbers are: 10,20,30 sum=0+10=10, i=2 yes no
  • 14.
    Problem:Write a programto find the average of n numbers. Flow Chart >> start Read n sum  0 i  1 i<= n ? Read numberi sum  sum + numberi ii+1 average  sum/n Print average stop n=3 and the numbers are: 10,20,30 sum=0+10=10, i=2 sum=10+20=30, i=3 yes no
  • 15.
    Problem:Write a programto find the average of n numbers. Flow Chart >> start Read n sum  0 i  1 i<= n ? Read numberi sum  sum + numberi ii+1 average  sum/n Print average stop n=3 and the numbers are: 10,20,30 sum=0+10=10, i=2 sum=10+20=30, i=3 yes no
  • 16.
    Problem:Write a programto find the average of n numbers. Flow Chart >> start Read n sum  0 i  1 i<= n ? Read numberi sum  sum + numberi ii+1 average  sum/n Print average stop n=3 and the numbers are: 10,20,30 sum=0+10=10, i=2 sum=10+20=30, i=3 yes no
  • 17.
    Problem:Write a programto find the average of n numbers. Flow Chart >> start Read n sum  0 i  1 i<= n ? Read numberi sum  sum + numberi ii+1 average  sum/n Print average stop n=3 and the numbers are: 10,20,30 sum=0+10=10, i=2 sum=10+20=30, i=3 sum=30+30=60, i=4 yes no
  • 18.
    Problem:Write a programto find the average of n numbers. Flow Chart >> start Read n sum  0 i  1 i<= n ? Read numberi sum  sum + numberi ii+1 average  sum/n Print average stop n=3 and the numbers are: 10,20,30 sum=0+10=10, i=2 sum=10+20=30, i=3 sum=30+30=60, i=4 yes no
  • 19.
    Problem:Write a programto find the average of n numbers. Flow Chart >> start Read n sum  0 i  1 i<= n ? Read numberi sum  sum + numberi ii+1 average  sum/n Print average stop n=3 and the numbers are: 10,20,30 sum=0+10=10, i=2 sum=10+20=30, i=3 sum=30+30=60, i=4 average=60/3=20 yes no
  • 20.
    Problem:Write a programto find the average of n numbers. Flow Chart >> start Read n sum  0 i  1 i<= n ? Read numberi sum  sum + numberi ii+1 average  sum/n Print average stop n=3 and the numbers are: 10,20,30 sum=0+10=10, i=2 sum=10+20=30, i=3 sum=30+30=60, i=4 average=60/3=20 yes no
  • 21.
    Problem:Write a programto find the average of n numbers. Flow Chart >> start Read n sum  0 i  1 i<= n ? Read numberi sum  sum + numberi ii+1 average  sum/n Print average stop n=3 and the numbers are: 10,20,30 sum=0+10=10, i=2 sum=10+20=30, i=3 sum=30+30=60, i=4 average=60/3=20 yes no
  • 22.
    Practice problems: 1. Drawflow chart to decide whether a number is even or odd. 2. Draw flow chart to find out the sum of the following series: a) 1+3+5+… … + 99 b) 1+ 1 3 + 1 5 + … … + 1 27 c) 1+1+2+3+5+8+13+… … + upto 100th terms (Fibonacci series) 3. Draw flow chart to find out the factorial of n, 1*2*3*… … *n 4. Draw flow chart to find out the maximum of 3 numbers. 5. Draw flow chart to check whether a number is prime or not. 6. Draw flow chart to find out the gcd of 2 numbers.
  • 23.
    GCD Problem: 15 )21 ( 1 15 --------------- 6 ) 15 ( 2 12 -------------- 3 ) 6 ( 2 6 ------------ 0
  • 24.
    GCD Problem: 15 )21 ( 1 15 --------------- 6 ) 15 ( 2 12 -------------- 3 ) 6 ( 2 6 ------------ 0 a b check
  • 25.
    GCD Problem: 15 )21 ( 1 15 --------------- 6 ) 15 ( 2 12 -------------- 3 ) 6 ( 2 6 ------------ 0 a b check
  • 26.
    GCD Problem: 15 )21 ( 1 15 --------------- 6 ) 15 ( 2 12 -------------- 3 ) 6 ( 2 6 ------------ 0 a b check
  • 27.
    GCD Problem: 15 )21 ( 1 15 --------------- 6 ) 15 ( 2 12 -------------- 3 ) 6 ( 2 6 ------------ 0 a b check Start input a , b check b % a check=0 ? ba a check print a stop No Yes check b % a
  • 28.
    C Programs BuildingProcess: C program building process involves four stages. At the end there should be a single executable file. 1. Preprocessing is the first pass of any C compilation. It processes include-files, define, conditional compilation instructions and macros. Source Code hello.c C Preprocessor
  • 29.
    C Programs BuildingProcess: C program building process involves four stages. At the end there should be a single executable file. 1. Preprocessing is the first pass of any C compilation. It processes include-files, define, conditional compilation instructions and macros. Source Code hello.c PreprocessedCode hello.i C Preprocessor
  • 30.
    C Programs BuildingProcess: C program building process involves four stages. At the end there should be a single executable file. 1. Preprocessing is the first pass of any C compilation. It processes include-files, define, conditional compilation instructions and macros. Source Code hello.c PreprocessedCode hello.i C Preprocessor C Compiler 2. Compilation is the second pass. It takes the output of the preprocessor and the source code and generates assembler source code.
  • 31.
    C Programs BuildingProcess: C program building process involves four stages. At the end there should be a single executable file. 1. Preprocessing is the first pass of any C compilation. It processes include-files, define, conditional compilation instructions and macros. Source Code hello.c PreprocessedCode hello.i Assembly code hello.s C Preprocessor C Compiler 2. Compilation is the second pass. It takes the output of the preprocessor and the source code and generates assembler source code.
  • 32.
    C Programs BuildingProcess: C program building process involves four stages. At the end there should be a single executable file. 1. Preprocessing is the first pass of any C compilation. It processes include-files, define, conditional compilation instructions and macros. Source Code hello.c PreprocessedCode hello.i Assembly code hello.s C Preprocessor C Compiler Assembler 2. Compilation is the second pass. It takes the output of the preprocessor and the source code and generates assembler source code. 3. Assembly is the third stage that takes the assembly source code and produces an assembly listing with offsets.The assembler output is stored in an object file.
  • 33.
    C Programs BuildingProcess: C program building process involves four stages. At the end there should be a single executable file. 1. Preprocessing is the first pass of any C compilation. It processes include-files, define, conditional compilation instructions and macros. Source Code hello.c PreprocessedCode hello.i Object code hello.o Assembly code hello.s C Preprocessor C Compiler Assembler 2. Compilation is the second pass. It takes the output of the preprocessor and the source code and generates assembler source code. 3. Assembly is the third stage that takes the assembly source code and produces an assembly listing with offsets.The assembler output is stored in an object file.
  • 34.
    C Programs BuildingProcess: C program building process involves four stages. At the end there should be a single executable file. 1. Preprocessing is the first pass of any C compilation. It processes include-files, define, conditional compilation instructions and macros. Source Code hello.c PreprocessedCode hello.i Executable code hello.exe Object code hello.o Assembly code hello.s C Preprocessor C Compiler Assembler Linker 2. Compilation is the second pass. It takes the output of the preprocessor and the source code and generates assembler source code. 3. Assembly is the third stage that takes the assembly source code and produces an assembly listing with offsets.The assembler output is stored in an object file. 4. Linking is the final stage that takes one or more object files or libraries as input and combines them to produce a single (executable) file. Library Files Other Object files Relocation object code information
  • 35.
    Compiling using commandprompt: 1. Write your code into a text editor and name the file with .c extention. 2. Add your gcc compiler’s bin directory path into windows environment variables path variable. 3. Open windows command prompt and write the following command gcc –c “path of your .c file with file name” This command will generate the object file with .o extension let, object_file_name.o 4.Then give the following command gcc –o exe_file_name object_file_name.o