Top Banner
Programming II (CS300) Chapter 01: Procedural Programming MOUNA KACEM 1 [email protected] Spring 2018
39

Programming II Procedural Programmingcs300- Programming uIntroduction uProcedural Programming: General Overview uProcedural Programming: Top-Down Design Method uComputational Thinking

Mar 20, 2018

Download

Documents

ngothu
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Programming II Procedural Programmingcs300- Programming uIntroduction uProcedural Programming: General Overview uProcedural Programming: Top-Down Design Method uComputational Thinking

Programming II (CS300)

Chapter 01: Procedural Programming

MOUNA KACEM

1

[email protected] 2018

Page 2: Programming II Procedural Programmingcs300- Programming uIntroduction uProcedural Programming: General Overview uProcedural Programming: Top-Down Design Method uComputational Thinking

Procedural Programmingu Introduction

u Procedural Programming: General Overview

u Procedural Programming: Top-Down Design Method

u Computational Thinking

u Basic Debugging

u Programming Tips

u Keep in Mind

2

Page 3: Programming II Procedural Programmingcs300- Programming uIntroduction uProcedural Programming: General Overview uProcedural Programming: Top-Down Design Method uComputational Thinking

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- Programming uIntroduction uProcedural Programming: General Overview uProcedural Programming: Top-Down Design Method uComputational Thinking

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- Programming uIntroduction uProcedural Programming: General Overview uProcedural Programming: Top-Down Design Method uComputational Thinking

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 date

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- Programming uIntroduction uProcedural Programming: General Overview uProcedural Programming: Top-Down Design Method uComputational Thinking

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- Programming uIntroduction uProcedural Programming: General Overview uProcedural Programming: Top-Down Design Method uComputational Thinking

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- Programming uIntroduction uProcedural Programming: General Overview uProcedural Programming: Top-Down Design Method uComputational Thinking

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- Programming uIntroduction uProcedural Programming: General Overview uProcedural Programming: Top-Down Design Method uComputational Thinking

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- Programming uIntroduction uProcedural Programming: General Overview uProcedural Programming: Top-Down Design Method uComputational Thinking

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)uA 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 independentlyu Different programmer can be working on different modules at the same time

Page 11: Programming II Procedural Programmingcs300- Programming uIntroduction uProcedural Programming: General Overview uProcedural Programming: Top-Down Design Method uComputational Thinking

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 manneru 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 applicationsu Solutions to smaller problems (tasks) are more likely to be re-used elsewhere (in

other programs) than solutions to bigger problemsu Libraries of software modules can be built to solve different tasksu 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- Programming uIntroduction uProcedural Programming: General Overview uProcedural Programming: Top-Down Design Method uComputational Thinking

12Procedural 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…

u Any major change at an upper level may impact all the procedures (solutions to related sub-problems) in that branch of hierarchy

Page 13: Programming II Procedural Programmingcs300- Programming uIntroduction uProcedural Programming: General Overview uProcedural Programming: Top-Down Design Method uComputational Thinking

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

13

Page 14: Programming II Procedural Programmingcs300- Programming uIntroduction uProcedural Programming: General Overview uProcedural Programming: Top-Down Design Method uComputational Thinking

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

14

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 15: Programming II Procedural Programmingcs300- Programming uIntroduction uProcedural Programming: General Overview uProcedural Programming: Top-Down Design Method uComputational Thinking

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

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 16: Programming II Procedural Programmingcs300- Programming uIntroduction uProcedural Programming: General Overview uProcedural Programming: Top-Down Design Method uComputational Thinking

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

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 17: Programming II Procedural Programmingcs300- Programming uIntroduction uProcedural Programming: General Overview uProcedural Programming: Top-Down Design Method uComputational Thinking

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

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 18: Programming II Procedural Programmingcs300- Programming uIntroduction uProcedural Programming: General Overview uProcedural Programming: Top-Down Design Method uComputational Thinking

Procedural Programming: Top-Down Design Method

Problem: Design an algorithm to determine if a given integer is perfect

18

u Coding a Solution for Sub-problem 1: Read a positive integer N as input

Page 19: Programming II Procedural Programmingcs300- Programming uIntroduction uProcedural Programming: General Overview uProcedural Programming: Top-Down Design Method uComputational Thinking

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

19

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 20: Programming II Procedural Programmingcs300- Programming uIntroduction uProcedural Programming: General Overview uProcedural Programming: Top-Down Design Method uComputational Thinking

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

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 21: Programming II Procedural Programmingcs300- Programming uIntroduction uProcedural Programming: General Overview uProcedural Programming: Top-Down Design Method uComputational Thinking

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.2: Check the resultStep1: if Sum equals N

Output: Display “N is perfect”

else

Output: Display “N is not perfect”

Page 22: Programming II Procedural Programmingcs300- Programming uIntroduction uProcedural Programming: General Overview uProcedural Programming: Top-Down Design Method uComputational Thinking

Procedural Programming: Top-Down Design Method

Problem: Design an algorithm to determine if a given integer is perfect

22

u Coding a Solution for Sub-problem 2: Determine if N is perfect and display the result

Page 23: Programming II Procedural Programmingcs300- Programming uIntroduction uProcedural Programming: General Overview uProcedural Programming: Top-Down Design Method uComputational Thinking

Procedural Programming: Top-Down Design Method

Problem: Design an algorithm to determine if a given integer is perfect

23

u Composing a Solution for the Problem: Determine if given number N is perfect

Page 24: Programming II Procedural Programmingcs300- Programming uIntroduction uProcedural Programming: General Overview uProcedural Programming: Top-Down Design Method uComputational Thinking

Procedural Programmingu Introduction

u Procedural Programming: General Overview

u Procedural Programming: Top-Down Design Method

u Computational Thinking

u Basic Debugging

u Programming Tips

u Keep in Mind

24

Page 25: Programming II Procedural Programmingcs300- Programming uIntroduction uProcedural Programming: General Overview uProcedural Programming: Top-Down Design Method uComputational Thinking

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

25

Page 26: Programming II Procedural Programmingcs300- Programming uIntroduction uProcedural Programming: General Overview uProcedural Programming: Top-Down Design Method uComputational Thinking

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 ?

u Logical reasoning involves the ability to u Analyze problems and logically organize datau Break problems into smaller ones, u Figure out how repeatable processes can save time and improve quality,uOrganize problems into the right sizeu Test and debug a problem, search for errors/bugs, and fix them

26

Page 27: Programming II Procedural Programmingcs300- Programming uIntroduction uProcedural Programming: General Overview uProcedural Programming: Top-Down Design Method uComputational Thinking

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

27

Page 28: Programming II Procedural Programmingcs300- Programming uIntroduction uProcedural Programming: General Overview uProcedural Programming: Top-Down Design Method uComputational Thinking

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

28

Page 29: Programming II Procedural Programmingcs300- Programming uIntroduction uProcedural Programming: General Overview uProcedural Programming: Top-Down Design Method uComputational Thinking

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

29

Page 30: Programming II Procedural Programmingcs300- Programming uIntroduction uProcedural Programming: General Overview uProcedural Programming: Top-Down Design Method uComputational Thinking

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

30

Page 31: Programming II Procedural Programmingcs300- Programming uIntroduction uProcedural Programming: General Overview uProcedural Programming: Top-Down Design Method uComputational Thinking

Procedural Programmingu Introduction

u Procedural Programming: General Overview

u Procedural Programming: Top-Down Design Method

u Computational Thinking

u Basic Debugging

u Programming Tips

u Keep in Mind

31

Page 32: Programming II Procedural Programmingcs300- Programming uIntroduction uProcedural Programming: General Overview uProcedural Programming: Top-Down Design Method uComputational Thinking

Basic Debuggingu Debugging: the activity of locating and removing errors from programs

u Errors, defects, or bugs can be observed during the testing process of a program u Test the program against different sets of input values

u If the output of one test cases does not obey to the specification and does not produce the expected output then the program is incorrect

u Testing techniques just reveal the presence of the errors. They do not identify the causes of the errors or tell how to fix these errors

u Testing can never prove that the program produces correct output results in all cases, unless there are only a finite number of possible input values for the program to consider

32

Page 33: Programming II Procedural Programmingcs300- Programming uIntroduction uProcedural Programming: General Overview uProcedural Programming: Top-Down Design Method uComputational Thinking

Basic Debuggingu Bug Classification: Compile-time errors

u Token errorsu A token error occurs whenever a program contains a word or symbol not

included in Java's vocabularyu The Java compiler must recognize each token (identifier, symbol, literal, and

comment) in a program

u Syntax errorsu A syntax error occurs whenever an incorrect grammar or punctuation

(according to the syntax rules of the Java programming language) is present in a program

u Syntax Constraint Errorsu These errors occur when the Java compiler cannot determine the meaning

of a program or a set of instructions

The Java compiler recognizes any token, syntactic, or syntax constraint errors in a program and report them in the Errors & Warnings window

33

Page 34: Programming II Procedural Programmingcs300- Programming uIntroduction uProcedural Programming: General Overview uProcedural Programming: Top-Down Design Method uComputational Thinking

Basic Debugging

u Bug Classification: Run-time errorsu Execution Errors

u Execution errors occur when the Java runtime system is executing a program and discovers that it can't legally carry out one of our instructions

u Example: dividing by zero error

u Intent ErrorsuAn intent error occurs whenever Java successfully completes the

execution of a program, but the program doesn't compute the correct output

34

Page 35: Programming II Procedural Programmingcs300- Programming uIntroduction uProcedural Programming: General Overview uProcedural Programming: Top-Down Design Method uComputational Thinking

Basic Debugging

u Basic debugging techniquesu Visual inspection

u Inspecting each statement one-by-one to try to find a bug

u Inserting debug output statements

u Inserting debug output statements whose output helps determine whether the preceding statement has the bug

u Hierarchical debuggingu divide the statements into regions, and insert one debug output statement

after each region

35

Page 36: Programming II Procedural Programmingcs300- Programming uIntroduction uProcedural Programming: General Overview uProcedural Programming: Top-Down Design Method uComputational Thinking

Procedural Programmingu Introduction

u Procedural Programming: General Overview

u Procedural Programming: Top-Down Design Method

u Computational Thinking

u Basic Debugging

u Programming Tips

u Keep in Mind

36

Page 37: Programming II Procedural Programmingcs300- Programming uIntroduction uProcedural Programming: General Overview uProcedural Programming: Top-Down Design Method uComputational Thinking

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-useu Test your program

u Test your program again a well-chosen set of input data and observe the input-output relationships

37

Page 38: Programming II Procedural Programmingcs300- Programming uIntroduction uProcedural Programming: General Overview uProcedural Programming: Top-Down Design Method uComputational Thinking

Programming Tips: Program Development Process 38Analyze 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

Page 39: Programming II Procedural Programmingcs300- Programming uIntroduction uProcedural Programming: General Overview uProcedural Programming: Top-Down Design Method uComputational Thinking

Keep in Mindu An algorithm is a repeatable set of steps that can transform inputs

into a predictable, consistent outputu Top-Down Design Method

u Designers solve a problem by breaking a main algorithm into parts (modules, procedures, functions) that are more manageable and easier to solve. The smaller solutions are assembled into a big solution

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

39