the art of design

17
The Art of Design Ying Wu Electrical Engineering & Computer Science Northwestern University [email protected] ECE230 Lectures Series

Upload: arthur-norris

Post on 02-Jan-2016

36 views

Category:

Documents


0 download

DESCRIPTION

ECE230 Lectures Series. The Art of Design. Ying Wu Electrical Engineering & Computer Science Northwestern University [email protected]. Outline. Data flow Top-down vs. bottom-up Coarse to fine Two examples MP#2 MP#4. Data Flow. Data - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: The Art of Design

The Art of Design

Ying Wu Electrical Engineering & Computer

ScienceNorthwestern [email protected]

ECE230 Lectures Series

Page 2: The Art of Design

Outline

Data flow Top-down vs. bottom-up Coarse to fine Two examples

– MP#2– MP#4

Page 3: The Art of Design

Data Flow

Data– data representation describe your

inputs/outputsE.g. 1: an inputted sentence?E.g. 2: a segment of a command line?

Inputs and outputs of the building blocks– what are given?– what are expected?

Data flow– blocks are connected through data flow

Page 4: The Art of Design

Flow chart

Coding is a very very small part of programming– design 40%– coding 10%– debugging 50%

The starting point of design– always draw flow charts– organize your thoughts by flow charts– use testing cases to validate your flow charts

Page 5: The Art of Design

Top-down “Incremental” programming

– Divide-and-conquer Task decomposition

– No matter how small a task is, you can always divide it into a set of small sequential subtasks

– You need to understand your tasks Focus

– Make the structure clear and neat– Always use the basic controls

Sequential Selectionrepetitive

Page 6: The Art of Design

Coarse to Fine Coarse Fine Coarse design

– determines the structure of the program– tells the basic idea– details are all ignored– concentrate on data flow– Organize it by subtasks

Refinement– A rough “block” can always by replaced by a

refined flowchart – It is much easier, because the tasks are

simpler

Page 7: The Art of Design

Bottom-up

“Brain-storming” programming Bottom design

– creating a set of small “tools”– thinking based on “basic operations”

Bottom Up– putting tools together– this needs more experiences

Page 8: The Art of Design

“Tools”

Extracting those basic operations– E.g. 1: bypass all “white spaces”– E.g. 2: check digits

Forming functions– inputs– outputs– error checking

Page 9: The Art of Design

Before you start …

Data representation?– How do you represent a “segment”?

st/ed

Your objectives?– Find a segment determine st/ed

Page 10: The Art of Design

A Coarse Design

Command line buffer[500]

Find the first element of a seg.

Find the last element of the seg.

Copy the segment to piece[100]

piece screen

End of the string?

Start the next segment

Y

N

done

Page 11: The Art of Design

Code itint main(){

cout << “Welcome bla bla bla” << endl;

char buffer[500];cout << "\nInput a command line: ";cin.getline(buffer, 500);

char piece[500];int st = 0, ed = 0;

while( buffer[st]!=0 ){st = _find_the_first_element( );

ed = _find_the_last_element( st );

_copy_segment(piece, st, ed);

_display_segment(piece);

st = ed + 1;}return 0;

}

Page 12: The Art of Design

Checking after actions

Command line buffer[500]

Find the first element of a seg.

Successful?

Find the last element of the seg.

Successful?

Copy the segment to piece[100]

piece screen

End of the string?

Start the next segment

Error

Y

N

N

Y

Y

N

Page 13: The Art of Design

int main(){

cout << “Welcome bla bla bla” << endl;

char buffer[500];cout << "\nInput a command line: ";cin.getline(buffer, 500);

char piece[500];int st = 0, ed = 0, err_code = 1;

while( buffer[st]!=0 ){st = _find_the_first_element( );if (buffer[st] == 0) err_code = 0;

ed = _find_the_last_element( st ); // err_code can be set inside

if (err_code == 0) break;else {

_copy_segment(piece, st, ed);_display_segment(piece);st = ed + 1;

}}return err_code;

}

Page 14: The Art of Design

Coarse-to-fine

Find the first element of a seg.st st

‘ ‘ ‘v ‘ ‘ a‘ ‘ r‘ ‘ _‘ ‘ 1‘ ‘ ‘ ‘ ‘ ‘ a‘ ‘ ‘ ‘ +‘ ‘ ‘ ‘3‘ ‘ .‘ ‘ 1‘ ‘ 4‘ ‘ ‘ 0

buffer[0]

buffer[st] == ‘ ‘ && buffer[st] != 0

st ++

Y

N

st

st

Page 15: The Art of Design

Coarse-to-fineFind the last element of a seg.

st ed

Scan to find ed

init ed = st

Is buffer[st] a letter?

Is buffer[st] a digit?

Is buffer[st] an operator?Scan to find ed

err_code = 0

Y N

YN

Y N

Scan to find ed

Is buffer[st] a ‘:’?

Is buffer[st] a ‘[‘?

Scan to find ed

Y

Scan to find ed

Y

ed and err_code

Page 16: The Art of Design

Go even finer

Scan to find ed

buffer[st] = ‘[‘

ed

init ed = st+1

Conditions for buffer[ed]

ed ++

err_code = 0

break

buffer[ed] != ‘]’

Page 17: The Art of Design

Put things together

From coarse-to-fine– always draw flowcharts– keep all your flowchart

Adding things little by little– don’t put together all at once

Validate the new components– whenever you refine a block, always

debug it and make sure the whole thing is correct

– keep the structure of the program clear