programming ii procedural programming
TRANSCRIPT
![Page 2: Programming II Procedural Programming](https://reader031.vdocuments.mx/reader031/viewer/2022012916/61c68ecba6274c27501713d7/html5/thumbnails/2.jpg)
Procedural Programming
u Introduction
u Procedural Programming: General Overview
u Procedural Programming: Top-Down Design Method
u Computational Thinking
u Programming Tips
u Keep in Mind
2
![Page 3: Programming II Procedural Programming](https://reader031.vdocuments.mx/reader031/viewer/2022012916/61c68ecba6274c27501713d7/html5/thumbnails/3.jpg)
Introduction
u Algorithmu An algorithm is a method or process followed to solve a problem
u If the algorithm is viewed as a function, then it represents the implementation for the function that transforms an input to a corresponding output
3
Input Process Output
Function
![Page 4: Programming II Procedural Programming](https://reader031.vdocuments.mx/reader031/viewer/2022012916/61c68ecba6274c27501713d7/html5/thumbnails/4.jpg)
u Algorithm
u Input: An algorithm has zero or more inputs (acquire data) read from an external source either before that the algorithm starts or as the algorithm runs
u Output: An algorithm has one or more outputs (results). The output values are specifically determined by the input.
u Process: A computational process (performing arithmetic computations, comparisons, testing logical conditions, and so on.) that given a set of values as an input produces some value or a set of values as an output
4
Input Process Output
Function
Introduction
![Page 5: Programming II Procedural Programming](https://reader031.vdocuments.mx/reader031/viewer/2022012916/61c68ecba6274c27501713d7/html5/thumbnails/5.jpg)
u Algorithmu A sequence of computational steps that transform the input into the
output
u A recipe for solving a computational problem whose steps (instructions) should be concrete and completely understood
5Introduction
Algorithm
Step-by-step procedure
Input: acquire data
Output: desired result
Food recipe
Step-by-step procedure
Input: Ingredients
Output: Cake
An algorithm is like a food recipe: a step-by-step procedure (a list of instructions) to complete a task
![Page 6: Programming II Procedural Programming](https://reader031.vdocuments.mx/reader031/viewer/2022012916/61c68ecba6274c27501713d7/html5/thumbnails/6.jpg)
u Programu An instantiation (or implementation) of an algorithm in a computer
programming language (such as C/C++, Java, Python, etc.)
u A program mustu Be Correct: i.e. converting each input to the correct output
u Be of finite length: An algorithm should be composed of a finite number of steps and each step must be doable in a finite amount of time
u Unambiguous: Each step of an algorithm must be precisely defined and it must be no ambiguity as to which step will be performed next
u Terminate for all inputs: i.e. an algorithm should not go into an infinite loop.
6Introduction
![Page 7: Programming II Procedural Programming](https://reader031.vdocuments.mx/reader031/viewer/2022012916/61c68ecba6274c27501713d7/html5/thumbnails/7.jpg)
u Procedural Programming
u Standard approach used in traditional Computer programming languages such as C, Pascal, Fortran, and Basic
u Creates a step by step program that guides the application through a sequence of instructions
u Each instruction is executed in order
u Procedural Programming focuses on processes (operations, conditions and actions)
7Procedural Programming - General Overview
![Page 8: Programming II Procedural Programming](https://reader031.vdocuments.mx/reader031/viewer/2022012916/61c68ecba6274c27501713d7/html5/thumbnails/8.jpg)
Procedural Programming: Top-Down Design Method
u Top-Down Design Methodu A solution method for problem solving where
u The problem is broken down into smaller sub-problems,
u The sub-problems in turn are broken down into smaller sub-problems
u… continuing until
u Each sub-problem is straight forward enough to be solved individually in a few steps
u The smaller solutions are assembled (composed) into a big solution
u This method is called also Functional decomposition, Modular development, or Divide and Conquer
8
![Page 9: Programming II Procedural Programming](https://reader031.vdocuments.mx/reader031/viewer/2022012916/61c68ecba6274c27501713d7/html5/thumbnails/9.jpg)
Procedural Programming: Top-Down Design Method
u Graphical illustration of the Top-Design Method
9
The number of levels (functional decomposition) continues until the sub-problem is one that can be solved directly
Statement of the Problem
Sub-problem1 Sub-problem2
S-P 1.1 S-P 1.2 S-P 1.3 S-P 2.1 S-P 2.2
.
.
.
Level 0 – Top Level
Level 1
Level 2
Level N
Dec
ompo
sitio
n of
the
Prob
lem
into
sm
alle
r sub
-pro
blem
s
Com
posit
ion
of th
e gl
oba
l sol
utio
n
![Page 10: Programming II Procedural Programming](https://reader031.vdocuments.mx/reader031/viewer/2022012916/61c68ecba6274c27501713d7/html5/thumbnails/10.jpg)
10Procedural Programming: Top-Down Design Method
u Benefits of the Top-Design Method (1)u Make the Problem solving easier
u Smaller problems or tasks are more easier to understand than a big problem
u It is easier to solve small problems than solving big/complex ones
u If a problem can't be solved directly, decompose it into smaller tasks. There is usually a smaller problem that can be solved easier. Just Find it
u Make the Problem solving faster (team-work)u A team of programmers can be used to solve the big problem faster
u Each sub-problem is independent of other sub-problems and can be solved individually
u Possibility to design, write and test each module independently
u Different programmer can be working on different modules at the same time
![Page 11: Programming II Procedural Programming](https://reader031.vdocuments.mx/reader031/viewer/2022012916/61c68ecba6274c27501713d7/html5/thumbnails/11.jpg)
11Procedural Programming: Top-Down Design Method
u Benefits of the Top-Design Method (2)u Make it easier to ensure correctness
u Error can be detected and fixed in a logical manner
u Easier to isolate the cause of an error, localize the error (within a module or a function), and fix it
u Ensure reusabilityu The process of top-down design leads to a modular implementation: reusable modules.
These modules can be used in future applications
u Solutions to smaller problems (tasks) are more likely to be re-used elsewhere (in other programs) than solutions to bigger problems
u Libraries of software modules can be built to solve different tasks
u For example, you need to use a linked list data structure to solve your problem. The routine to manage and manipulate a linked list can be mostly available to re-use
![Page 12: Programming II Procedural Programming](https://reader031.vdocuments.mx/reader031/viewer/2022012916/61c68ecba6274c27501713d7/html5/thumbnails/12.jpg)
Procedural Programming: Top-Down Design Method
u Graphical illustration of the Top-Design Method
12
The number of levels (functional decomposition) continues until the sub-problem is one that can be solved directly
Statement of the Problem
Sub-problem1 Sub-problem2
S-P 1.1 S-P 1.2 S-P 1.3 S-P 2.1 S-P 2.2
.
.
.
Level 0 – Top Level
Level 1
Level 2
Level N
Dec
ompo
sitio
n of
the
Prob
lem
into
sm
alle
r sub
-pro
blem
s
Com
posit
ion
of th
e gl
oba
l sol
utio
n
![Page 13: Programming II Procedural Programming](https://reader031.vdocuments.mx/reader031/viewer/2022012916/61c68ecba6274c27501713d7/html5/thumbnails/13.jpg)
13Procedural Programming: Top-Down Design Method
u Main issue of the Top-Design Method
u The Top-Down design method starts with a specification of the system (statement of the problem) at the top level
u If the main problem is not well-defined (not defined correctly), changes made at the top level (the main program or procedure) will cascade to the sub-procedures, and the sub-sub-procedures, and so on…
uAny major change at an upper level may impact all the procedures (solutions to related sub-problems) in that branch of hierarchy
![Page 14: Programming II Procedural Programming](https://reader031.vdocuments.mx/reader031/viewer/2022012916/61c68ecba6274c27501713d7/html5/thumbnails/14.jpg)
Procedural Programming: Top-Down Design Method
u Practice ExampleProblem Description: An integer N is "perfect" if N is equal to the sum of the positive integers K such that K < N and K is a divisor of N.
Design an algorithm to determine if a given integer is perfect
14
![Page 15: Programming II Procedural Programming](https://reader031.vdocuments.mx/reader031/viewer/2022012916/61c68ecba6274c27501713d7/html5/thumbnails/15.jpg)
Procedural Programming: Top-Down Design Method
Problem Description: An integer N is "perfect" if N is equal to the sum of the positive integers K such that K < N and K is a divisor of N.
Design an algorithm to determine if a given integer is perfect
15
Problem Analysisu Input: an integer N
u Precondition: N should be positive
u Processu Determine the divisors of the number N
u Check if the divisors add up to the number N
u Outputif N is perfect
u Display “N is perfect” or return “true”
elseu Display “N is not perfect” or return “false”
![Page 16: Programming II Procedural Programming](https://reader031.vdocuments.mx/reader031/viewer/2022012916/61c68ecba6274c27501713d7/html5/thumbnails/16.jpg)
Procedural Programming: Top-Down Design Method
Problem Description: An integer N is "perfect" if N is equal to the sum of the positive integers K such that K < N and K is a divisor of N.
Design an algorithm to determine if a given integer is perfect
16
Continuing Problem Analysis: Problem Decompositionu Sub-problem 1
Read a positive integer N as input
u Sub-problem 2Determine if N is perfect and display the result
![Page 17: Programming II Procedural Programming](https://reader031.vdocuments.mx/reader031/viewer/2022012916/61c68ecba6274c27501713d7/html5/thumbnails/17.jpg)
Procedural Programming: Top-Down Design Method
Problem Description: An integer N is "perfect" if N is equal to the sum of the positive integers K such that K < N and K is a divisor of N.
Design an algorithm to determine if a given integer is perfect
17
Continuing Problem Analysis: Problem Decompositionu Sub-problem 1
Read a positive integer N as input
u Sub-problem 2Determine if N is perfect and display the result
Sub-problem2 Decomposition:u Determine the sum of the divisors of the number N
u Check if the divisors add up to the number N and display the result
![Page 18: Programming II Procedural Programming](https://reader031.vdocuments.mx/reader031/viewer/2022012916/61c68ecba6274c27501713d7/html5/thumbnails/18.jpg)
Procedural Programming: Top-Down Design Method
Problem Description: An integer N is "perfect" if N is equal to the sum of the positive integers K such that K < N and K is a divisor of N.
Design an algorithm to determine if a given integer is perfect
18
u Solving Sub-problem 1: Read a positive integer N as inputStep1: Output a prompt asking for a positive number
Step2: Input/Read the number called N
Step3: Check if N is positive otherwise ask again for a valid input
(Step 3 is a loop executed zero or more time;
stop condition: input value N is positive)
=> Step3: while loop
![Page 19: Programming II Procedural Programming](https://reader031.vdocuments.mx/reader031/viewer/2022012916/61c68ecba6274c27501713d7/html5/thumbnails/19.jpg)
Procedural Programming: Top-Down Design Method
Problem: Design an algorithm to determine if a given integer is perfect
19
u Coding a Solution for Sub-problem 1: Read a positive integer N as input
![Page 20: Programming II Procedural Programming](https://reader031.vdocuments.mx/reader031/viewer/2022012916/61c68ecba6274c27501713d7/html5/thumbnails/20.jpg)
Procedural Programming: Top-Down Design Method
Problem Description: An integer N is "perfect" if N is equal to the sum of the positive integers K such that K < N and K is a divisor of N.
Design an algorithm to determine if a given integer is perfect
20
Recall: Problem Decompositionu Sub-problem 1
Read a positive integer N as input
u Sub-problem 2Determine if N is perfect and display the result
Sub-problem2 Decomposition:u Determine the sum of the divisors of the number N
u Check if the divisors add up to the number N and display the result
![Page 21: Programming II Procedural Programming](https://reader031.vdocuments.mx/reader031/viewer/2022012916/61c68ecba6274c27501713d7/html5/thumbnails/21.jpg)
Procedural Programming: Top-Down Design Method
Problem Description: An integer N is "perfect" if N is equal to the sum of the positive integers K such that K < N and K is a divisor of N.
Design an algorithm to determine if a given integer is perfect
21
u Solving Sub-problem 2.1: Determine the sum of the divisors of the number NStep1: Set the Sum to 1 (since 1 certainly divides N)
Step2: Set div to 2
Step3: while div is less than or equal to N/2
if div is a divisor of N
add div to the Sum
add 1 to div
=> Step 3 is a for loop
![Page 22: Programming II Procedural Programming](https://reader031.vdocuments.mx/reader031/viewer/2022012916/61c68ecba6274c27501713d7/html5/thumbnails/22.jpg)
Procedural Programming: Top-Down Design Method
Problem Description: An integer N is "perfect" if N is equal to the sum of the positive integers K such that K < N and K is a divisor of N.
Design an algorithm to determine if a given integer is perfect
22
u Solving Sub-problem 2.2: Check the resultStep1: if Sum equals N
Output: Display “N is perfect”
else
Output: Display “N is not perfect”
![Page 23: Programming II Procedural Programming](https://reader031.vdocuments.mx/reader031/viewer/2022012916/61c68ecba6274c27501713d7/html5/thumbnails/23.jpg)
Procedural Programming: Top-Down Design Method
Problem: Design an algorithm to determine if a given integer is perfect
23
u Coding a Solution for Sub-problem 2: Determine if N is perfect and display the result
![Page 24: Programming II Procedural Programming](https://reader031.vdocuments.mx/reader031/viewer/2022012916/61c68ecba6274c27501713d7/html5/thumbnails/24.jpg)
Procedural Programming: Top-Down Design Method
Problem: Design an algorithm to determine if a given integer is perfect
24
u Composing a Solution for the Problem: Determine if given number N is perfect
![Page 25: Programming II Procedural Programming](https://reader031.vdocuments.mx/reader031/viewer/2022012916/61c68ecba6274c27501713d7/html5/thumbnails/25.jpg)
Procedural Programming
u Introduction
u Procedural Programming: General Overview
u Procedural Programming: Top-Down Design Method
u Computational Thinking
u Programming Tips
u Keep in Mind
25
![Page 26: Programming II Procedural Programming](https://reader031.vdocuments.mx/reader031/viewer/2022012916/61c68ecba6274c27501713d7/html5/thumbnails/26.jpg)
Computational Thinkingu Computational Thinking
u Processes involved in formulating problems and their solutions in a way that can be effectively performed by a computer
u Computational thinking is not thinking about computers. It is about looking at a problem in a way that a computer can help us to solve it
u A collection of diverse skills and techniques to help programmers solve problems using a computer effectivelyu Logical reasoningu Defining the best way to solve the problemu Decompositionu Abstractionu Patterns and generalization
26
![Page 27: Programming II Procedural Programming](https://reader031.vdocuments.mx/reader031/viewer/2022012916/61c68ecba6274c27501713d7/html5/thumbnails/27.jpg)
Computational Thinkingu Logical reasoning
u Logical reasoning is fundamental in computer programmingu Be careful: Computers are not natural logical thinkers.
u Logical reasoning is about constructing arguments and being able to explain uWhy does something happen ?
uWhy is something the way it is ?
uwhy something isn’t quite as it should be ?
27
![Page 28: Programming II Procedural Programming](https://reader031.vdocuments.mx/reader031/viewer/2022012916/61c68ecba6274c27501713d7/html5/thumbnails/28.jpg)
Computational Thinking
u Logical reasoningu Logical reasoning involves the ability to
uAnalyze problems and logically organize data
u Break problems into smaller ones,
u Figure out how repeatable processes can save time and improve quality,
uOrganize problems into the right size
u Test and debug a problem, search for errors/bugs, and fix them
28
![Page 29: Programming II Procedural Programming](https://reader031.vdocuments.mx/reader031/viewer/2022012916/61c68ecba6274c27501713d7/html5/thumbnails/29.jpg)
Computational Thinking
u Defining the best way to solve the problemu There are different solutions
uAnalyze the different solutions: which is the best one in terms of correctness, fastest way to solve the problem and using the least amount of resources (time and memory space)
u Promote re-usability: which solution can be used to solve other problems
29
![Page 30: Programming II Procedural Programming](https://reader031.vdocuments.mx/reader031/viewer/2022012916/61c68ecba6274c27501713d7/html5/thumbnails/30.jpg)
Computational Thinking
u Decompositionu Breaking-down a problem into smaller sub-problems or tasks,
easier to understand and to solve u Top-Down Design Method
30
![Page 31: Programming II Procedural Programming](https://reader031.vdocuments.mx/reader031/viewer/2022012916/61c68ecba6274c27501713d7/html5/thumbnails/31.jpg)
Computational Thinking
u Abstractionu Abstraction is simplifying things
u Allows a better management of the complexity of problem solvingu Simplifying things
u Identifying what is important without worrying about the details (determine what details to highlight and what details to ignore)
u Removing unnecessary details
u High-level view: abstract over details
31
![Page 32: Programming II Procedural Programming](https://reader031.vdocuments.mx/reader031/viewer/2022012916/61c68ecba6274c27501713d7/html5/thumbnails/32.jpg)
Computational Thinking
u Patterns and Generalizationu Generalization
u Looking for a general approach to a class of problems
u Identifying patterns allowsuMake predictions
uCreate rules
u Provide a reliable solution (model) to solve a general problem
u Spotting and using similarities
32
![Page 33: Programming II Procedural Programming](https://reader031.vdocuments.mx/reader031/viewer/2022012916/61c68ecba6274c27501713d7/html5/thumbnails/33.jpg)
Procedural Programming
u Introduction
u Procedural Programming: General Overview
u Procedural Programming: Top-Down Design Method
u Computational Thinking
u Programming Tips
u Keep in Mind
33
![Page 34: Programming II Procedural Programming](https://reader031.vdocuments.mx/reader031/viewer/2022012916/61c68ecba6274c27501713d7/html5/thumbnails/34.jpg)
Programming Tipsu Refine your program
u Use methods to increase the readability of your code and makes it easier to expand or change
u Format your codeu Code formatting is necessary to understand your code more easily
u Comment your codeu Good comments make your program more easier to understand,
expand and re-use
u Test your programu Test your program against a well-chosen set of input data and observe
the input-output relationships
34
![Page 35: Programming II Procedural Programming](https://reader031.vdocuments.mx/reader031/viewer/2022012916/61c68ecba6274c27501713d7/html5/thumbnails/35.jpg)
Programming Tips: Program Development Process 35Analyze the problem statement
Design a solution
Edit source code
Compile/link source code
Compile-time errors?
Test the program execution
Run-time errors?
Find error in source code(check syntax)
Find cause of execution or intent/logic errorcheck codecheck input datarethink analysis/design
Success with this input!
or
yes
yes
No
No
Test your program
execution with
another set of input
![Page 36: Programming II Procedural Programming](https://reader031.vdocuments.mx/reader031/viewer/2022012916/61c68ecba6274c27501713d7/html5/thumbnails/36.jpg)
Keep in Mind
u An algorithm is a repeatable set of steps that can transforminputs into a predictable, consistent output
u Top-Down Design Methodu breaking a main algorithm into parts (modules, procedures,
functions) that are more manageable and easier to solve.
u The smaller solutions are assembled into a big solution
36
![Page 37: Programming II Procedural Programming](https://reader031.vdocuments.mx/reader031/viewer/2022012916/61c68ecba6274c27501713d7/html5/thumbnails/37.jpg)
Keep in Mind
u Do not forget: You need to develop a solution to a task only once and then re-use it where-ever you need it
u Computational thinking is the key for organizing thoughts to solve problems efficiently using a computer
u Use logical reasoning while debugging a program
37