introduction to c programming ce00312-1 lecture 5 program design in c

25
Introduction to C Programming CE00312-1 Lecture 5 Program Design in C

Upload: alexina-richardson

Post on 21-Jan-2016

229 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Introduction to C Programming CE00312-1 Lecture 5 Program Design in C

Introduction to C Programming

CE00312-1

Lecture 5

Program Design in C

Page 2: Introduction to C Programming CE00312-1 Lecture 5 Program Design in C

Design theory

Difference between functional and object-oriented programming paradigm and design of resulting code.

Top-down design

Stepwise refinement

Functional decomposition

Page 3: Introduction to C Programming CE00312-1 Lecture 5 Program Design in C

Design of Processes

functional analysis used to design resulting code

Break down design into logical blocks        Stepwise refinement        Functional decomposition

Page 4: Introduction to C Programming CE00312-1 Lecture 5 Program Design in C

Functional Analysis

Get upGo to UniversityFor as long as there is a lecture to attend

Go to lectureTake notes attentively and enthusiastically!

Eat teaGo to townFor as long as you have money and are thirsty

Buy a drinkGo home

If you have spare money go by taxi

Else walk

Go Function

College Function

Food Function

Go out Function

Go home Function

Page 5: Introduction to C Programming CE00312-1 Lecture 5 Program Design in C

Design Notation

Top Down design Problem decomposition expressed as combinations of

sequence, selection, iteration both Inter-function and intra-function

Level of abstraction or degree of modularity determined by complexity of problem and design notation used

Object Design Uses object modeling to determine level of abstraction Problem decomposition determined by object definition Processes expressed as behaviour of objects

Page 6: Introduction to C Programming CE00312-1 Lecture 5 Program Design in C

Top-Down design

Take main functions Systematically break into smaller chunks Use a method to ‘represent’ solution Stop when resulting modules are small

enough to be easily implemented (but not too cumbersome)

‘Clever’ chunking results in reusable modules

Page 7: Introduction to C Programming CE00312-1 Lecture 5 Program Design in C

Top Down design notation

Representative of problem Diagrammatic notation;

sequence, selection, iteration JSP Nassi-Scneidermann

Pseudo Code/ Structured English Embeds descriptive language into basic construct

syntax

Page 8: Introduction to C Programming CE00312-1 Lecture 5 Program Design in C

Pseudocode/Structured English

While valid input If mark entry enter name enter mark Case {mark 0 - 39 “refer”

mark 40 – 59 “pass”mark 60 – 79 “merit”mark 80 – 100 “Distinction”}

else “Error”

output name and grade Else Clear

Page 9: Introduction to C Programming CE00312-1 Lecture 5 Program Design in C

0 < O R > 100 < 40 < 60 < 80 else

In p u tN am e & E xam

oE rro r

oR e fe rra l

oP ass

oM erit

oD is tin c tion

C h eckE xam

O u tp u tN am e & G rad e

oG rad e

oC lear

C h eck E ven t *

E xam G rad e

Jackson Structured Design

Inputs

Output

Page 10: Introduction to C Programming CE00312-1 Lecture 5 Program Design in C

Nassi-Schneidermann

Enter details

While user input

If Not Clear

Clear

RP M D E

Output mark & name

Page 11: Introduction to C Programming CE00312-1 Lecture 5 Program Design in C

Testing

Purpose of testing Designing test cases and test case data

with corresponding input and expected output

Black and White box testing Difference between these two categories

of test; when test data can be designed and purpose.

Page 12: Introduction to C Programming CE00312-1 Lecture 5 Program Design in C

Black Box testing

Black Box testing consists of Categories Functional Tests Invalid Tests Boundary Tests Special Tests

Page 13: Introduction to C Programming CE00312-1 Lecture 5 Program Design in C

White Box testing

Categories Path Analysis Complex Conditions

Page 14: Introduction to C Programming CE00312-1 Lecture 5 Program Design in C

Testing Example

Consider the example used as one of the week 2 practical exercises;

reading in percentage scores greater than 0 and printing out the corresponding letter grade. This is repeated until a negative value is read in. At this stage, the total number of values in each grade range is printed. An error message is printed for any input value greater than 100.

Page 15: Introduction to C Programming CE00312-1 Lecture 5 Program Design in C

Black Box Tests1. Functional Tests

F1: Read in a percentage score and print the corresponding letter grade

F2: Produce an error message if a positive number greater than 100 is input

F3: Print out the number of input values in each of the letter grade ranges

F4: Terminate input when a negative value is read in.

Page 16: Introduction to C Programming CE00312-1 Lecture 5 Program Design in C

2. Invalid Tests

I1: A positive number greater than 100 should produce an error message

Page 17: Introduction to C Programming CE00312-1 Lecture 5 Program Design in C

3. Boundary Tests

B1: Input largest valid value B2: Input smallest invalid positive value B3: Input lowest valid positive value B4: Input largest invalid negative value B5: Input lowest value in ‘A’ range B6: Input highest value in ‘B’ range B7: Input lowest value in ‘B’ range B8: Input highest value in ‘C’ range B9: Input lowest value in ‘C’ range B10: Input highest value in ‘D’ range B11: Input lowest value in ‘D’ range B12: Input highest value in ‘F’ range

Page 18: Introduction to C Programming CE00312-1 Lecture 5 Program Design in C

4. Special Test Cases

S1: First input value is negative S2: No values are input in a particular letter

grade range

Page 19: Introduction to C Programming CE00312-1 Lecture 5 Program Design in C

White Box Tests

Can only be designed after the code has been written.

Complex Conditions None in this example

Path Analysis

Page 20: Introduction to C Programming CE00312-1 Lecture 5 Program Design in C

P7P1

Not P2

P5

P3Not P3

P4 Not P4

Not P6

Not P5P6

P2

Path Analysis

Page 21: Introduction to C Programming CE00312-1 Lecture 5 Program Design in C

Path Analysis P1: Initial sequence P2: Value greater than 100 Not P2: Positive value less than or equal to 100 P3: A’ grade Not P3: value below an ‘A’ grade P4: ‘B’ grade Not P4: value below a ‘B’ grade P5: ‘C’ grade Not P5: value below a ‘C’ grade P6: ‘D’ grade Not P6: ‘F’ grade P7: Print out total number of values in each grade range

Page 22: Introduction to C Programming CE00312-1 Lecture 5 Program Design in C

Test Data F1: 90 -2 F2: 110 –2 F3: 67 78 89 90 93 53 54 65 72

73 -3 F4: As for F1 I1: As for F2 B1: 100 -1 B2: 101 -1 B3: 0 -1 B4: -1 B5: As for F1 B6: 89 -1 B7: 80 -1 B8: 79 -1 B9: 70 -2 B10: 69 -1 B11: 60 -1 B12: 59 –1 S1: As for B4 S2: 90 89 78 -1

P1: As for F1 P2: As for F2 P3: As for F1 Not P3: As for B12 P4: As for B7 Not P4: As for B12 P5: As for B9 Not P5: As for B12 P6: As for B12 Not P6: As for F3 P7: As for F1

Page 23: Introduction to C Programming CE00312-1 Lecture 5 Program Design in C

Complex ConditionsConsider the following if condition 

if (age<18 || age >=65) Resulting complex conditions:  age<18 age>=65C1: True TrueC2: True FalseC3: False TrueC4: False False C1: impossibleC2: age = 16C3: age = 70C4: age = 21

Page 24: Introduction to C Programming CE00312-1 Lecture 5 Program Design in C

The number of complex conditions is equal to 2N, where N is the number of parts to the condition.

 e.g.

 if ( age <18 || age >=65) && (gender==’m’ || gender==’f’)

              24 = 16 conditions

Page 25: Introduction to C Programming CE00312-1 Lecture 5 Program Design in C

Truth Table

age<18 age>=65 gender==’m’ gender==’f’ False False False FalseFalse False False TrueFalse False True FalseFalse False True TrueFalse True False FalseFalse True False TrueFalse True True FalseFalse True True True

Repeated with column 1 containing True and columns 2, 3 and 4 unchanged.

e.g.   C3: age=21, gender=’m’ C4: impossible (gender can’t be both ‘f’ and ‘m’) C6: age = 70, gender=’f’ C9: age = 16 gender =’z’