programming ii procedural programmingcs300-...uprogram uan instantiation (or implementation) of an...

26
Programming II (CS300) Chapter 01: Procedural Programming MOUNA KACEM 1 [email protected] Spring 2019

Upload: others

Post on 12-Mar-2020

5 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Programming II Procedural Programmingcs300-...uProgram uAn instantiation (or implementation) of an algorithm in a computer programming language (such as C/C++, Java, Python, etc.)

Programming II (CS300)

Chapter 01: Procedural Programming

MOUNA KACEM

1

[email protected] 2019

Page 2: Programming II Procedural Programmingcs300-...uProgram uAn instantiation (or implementation) of an algorithm in a computer programming language (such as C/C++, Java, Python, etc.)

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 Programmingcs300-...uProgram uAn instantiation (or implementation) of an algorithm in a computer programming language (such as C/C++, Java, Python, etc.)

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 Programmingcs300-...uProgram uAn instantiation (or implementation) of an algorithm in a computer programming language (such as C/C++, Java, Python, etc.)

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 Programmingcs300-...uProgram uAn instantiation (or implementation) of an algorithm in a computer programming language (such as C/C++, Java, Python, etc.)

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 Programmingcs300-...uProgram uAn instantiation (or implementation) of an algorithm in a computer programming language (such as C/C++, Java, Python, etc.)

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 Programmingcs300-...uProgram uAn instantiation (or implementation) of an algorithm in a computer programming language (such as C/C++, Java, Python, etc.)

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 Programmingcs300-...uProgram uAn instantiation (or implementation) of an algorithm in a computer programming language (such as C/C++, Java, Python, etc.)

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 Programmingcs300-...uProgram uAn instantiation (or implementation) of an algorithm in a computer programming language (such as C/C++, Java, Python, etc.)

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 Programmingcs300-...uProgram uAn instantiation (or implementation) of an algorithm in a computer programming language (such as C/C++, Java, Python, etc.)

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 Programmingcs300-...uProgram uAn instantiation (or implementation) of an algorithm in a computer programming language (such as C/C++, Java, Python, etc.)

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 Programmingcs300-...uProgram uAn instantiation (or implementation) of an algorithm in a computer programming language (such as C/C++, Java, Python, etc.)

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 Programmingcs300-...uProgram uAn instantiation (or implementation) of an algorithm in a computer programming language (such as C/C++, Java, Python, etc.)

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 Programmingcs300-...uProgram uAn instantiation (or implementation) of an algorithm in a computer programming language (such as C/C++, Java, Python, etc.)

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

14

Page 15: Programming II Procedural Programmingcs300-...uProgram uAn instantiation (or implementation) of an algorithm in a computer programming language (such as C/C++, Java, Python, etc.)

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

15

Page 16: Programming II Procedural Programmingcs300-...uProgram uAn instantiation (or implementation) of an algorithm in a computer programming language (such as C/C++, Java, Python, etc.)

Computational Thinking

u Logical reasoningu Logical reasoning is fundamental in computer programming

u 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 ?

16

Page 17: Programming II Procedural Programmingcs300-...uProgram uAn instantiation (or implementation) of an algorithm in a computer programming language (such as C/C++, Java, Python, etc.)

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

17

Page 18: Programming II Procedural Programmingcs300-...uProgram uAn instantiation (or implementation) of an algorithm in a computer programming language (such as C/C++, Java, Python, etc.)

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

18

Page 19: Programming II Procedural Programmingcs300-...uProgram uAn instantiation (or implementation) of an algorithm in a computer programming language (such as C/C++, Java, Python, etc.)

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

19

Page 20: Programming II Procedural Programmingcs300-...uProgram uAn instantiation (or implementation) of an algorithm in a computer programming language (such as C/C++, Java, Python, etc.)

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

20

Page 21: Programming II Procedural Programmingcs300-...uProgram uAn instantiation (or implementation) of an algorithm in a computer programming language (such as C/C++, Java, Python, etc.)

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

21

Page 22: Programming II Procedural Programmingcs300-...uProgram uAn instantiation (or implementation) of an algorithm in a computer programming language (such as C/C++, Java, Python, etc.)

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

22

Page 23: Programming II Procedural Programmingcs300-...uProgram uAn instantiation (or implementation) of an algorithm in a computer programming language (such as C/C++, Java, Python, etc.)

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

23

Page 24: Programming II Procedural Programmingcs300-...uProgram uAn instantiation (or implementation) of an algorithm in a computer programming language (such as C/C++, Java, Python, etc.)

Programming Tips: Program Development Process 24Analyze 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 25: Programming II Procedural Programmingcs300-...uProgram uAn instantiation (or implementation) of an algorithm in a computer programming language (such as C/C++, Java, Python, etc.)

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

25

Page 26: Programming II Procedural Programmingcs300-...uProgram uAn instantiation (or implementation) of an algorithm in a computer programming language (such as C/C++, Java, Python, etc.)

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

26