Andreas Savva Data Structures Chapter 1 Programming Principles Data Structures Type of data Structure of data Data Type  Consists of two parts: Set of values  Set of operations that.

Download Report

Transcript Andreas Savva Data Structures Chapter 1 Programming Principles Data Structures Type of data Structure of data Data Type  Consists of two parts: Set of values  Set of operations that.

Data Structures

Chapter 1 Programming Principles

Andreas Savva

Data Structures Type of data Structure of data

2

Data Type

Consists of two parts:

 Set of values  Set of operations that act on the values  Example : Integer data type   Values - whole numbers in some defined range Operations - add, subtract, multiply, divide, etc. 3

Data Structure

Consists of two parts:

  Collection of elements each of which is either a data type or another data structure.

A set of associations or relationships ( structure ) involving the collection of elements.

4

Examples – Data Structure

 Array  Sequence of elements (data types)  Position association among the elements

pos

 4 0 8 1 12 2 20 3  Record  Collection of data into a single structure  No association Surname: Eastwood Name: Nationality: Clint USA Telephone: Age: +15678900 75 32 4 47 5 60 6 5

Classification - Data Structures

  Hierarchical (Tree) structure   Unique predecessor Many successors  Graph structure   Many predecessors Many successors  Linear Structure Set structure   Unique predecessor Unique successor   No predecessor No successor 6

Examples

 Linear Structure   Stack Queue  Hierarchical (Tree) structure   Family Tree Computer Directories  Graph structure   Computer Network London Underground  Set structure  You Students in this class 7

Why do we need data structures?

Example – Towns Data Structure     You need to visit all houses in two towns once only.

In each town you start and finish at house number 1.

All roads are the same length.

Same number of houses in each town.

8

Town 1 Town 2 1 8 7 2 6 3 4 5 1 8 2 3 4 7 6 5 In town 1 you walk twice as far as in town 2.

Why?

Because the structures are different.

9

Choosing a data structure

The process involves:

 analyzing

the problem,

 determining

basic operations needed,

 selecting

the data structure.

10

Choosing the right data structure

The

right

data structure will make your operations

simple

and

efficient

.

The

wrong

data structure will make your operations

complex

and

inefficient

.

11

Abstract Data Types (ADT)

A module

(

object

)  

containing:

the data structure, the associated operations (subprograms).

Details of the module are user

(

encapsulation ).

hidden from the

This is called data abstraction.

Modules are stored as Unit in Pascal, Class in C++ and Java 12

Abstract Data Types (ADT)

 

Data cannot be accessed directly.

Data can only be accessed indirectly via the subprograms (

methods

) associated with the data.

Thus, the module contains: The data structure .

Subprograms to access structure.

and/or modify the data 13

C++ Classes, Objects, and Methods     A class collects data and the methods used to access or change the data.

Such a collection of data and methods is called an object belonging to the given class.

Every class consists of either variables (called functions (called members methods or that represent data members ) or member functions ). The member functions of a class are normally used to access or alter the data members.

Clients (user programs) can declare and manipulate objects of that class.

14

Information Hiding

   A client does not need to know how the data are actually stored or how the methods are programmed. This important programming strategy is called Data members and methods available to a client are called Private public .

information hiding .

variables and functions may be used in the implementation of the class, but are not available to a client.

Methods Functions of a class are public in a class are private 15

Comparison

 Abstract Data Types   High level description – logical picture of the data, and operations that manipulate the data.

Implementation of program is structure.

independent to the data  Implementation of program is concerned with what can do.

it  NOT Abstract Data Types   Concrete description – collection of data types & operations that store and retrieve individual elements.

Implementation of program is structure.

dependant to the data  Implementation of program is concerned with how task is done.

a 16

Example – Abstract Data Type

 Main activities for operating a car are:  steering, accelerating, braking    The design of a car can be viewed as an ADT with operations:  steer, accelerate, brake Two cars may implement these operations in different ways.

Most drivers can operate any car since the ADT presents a uniform method of operation.

17

Programming

Assumption:

 You can design and write programs.

This Course:

  Uses C++ as a tool.

Will not teach you how to program.

18

Programming Principles

   We must learn to observe important principles of program design.

We usually ignore them when we write small programs.

BUT ignoring them when writing large projects could be disastrous.

19

Problems of large programs

   More difficult to maintain them than to write them.

In time there will be new requests on the program, and if it is not well-designed or if the data is not well structured it will be impossible to restructure it and the program will become unusable.  It will cost less to write another program from scratch than maintaining the existing one.

The approach “ First make your program work and then make it pretty ” may be effective for small programs but not for large ones.

20

Program Design

 Divide the problem into smaller problems until they become of manageable size.

Project Data Structure Common and compatible Sub-problem  Each part of a program must be well organized, clearly written or else its structure will have been forgotten at some time later or will not be understood by other programmers.

You must form good programming habits from the beginning.

21

 Choice of Data Structures The most important aspect in algorithm design is the way in which the data of the program is stored:   How they are arranged in relation to each other.

Which data are kept in memory.

  Which are calculated when needed.

Which are kept in files, and how this files are arranged.

   Program Testing The difficulty of debugging a program increases much faster than its size. A program twice the size of another will likely to not take twice as long to debug, but most probably, four times as long.

Many large programs (such as operating systems) are put into use still containing errors that the programmers have not spotted.

Sometimes projects that have consumed years of effort must be discarded because it is impossible to discover why they will not work.

22

Program Correctness  If we do not wish such a fate to our projects, then we must use methods that:    Reduce the number of errors, making it easier to spot those that remain.

Enable us to verify in advance that our algorithms are correct.

Provide us with ways to test our programs so that we can be reasonably confident that they will not misbehave.

Maintenance  Since new demands will arise in the future it is important that a large project is written to make it as easy as possible to be understood and modified.

23

Case Study

 The Game of Life   Introduced by the British mathematician J.H. Conway in 1970.

It is a simulation, not a game with players.

24

  Rules for the Game of Life It takes place on an unbounded rectangular grid in which each cell can either be occupied by an organism or not. Occupied cells are called alive ; unoccupied cells are called dead changes from generation to generation according to the number of neighboring cells that are alive, as follows: . Which cells are alive      The neighbors of a given cell are the eight cells that touch it vertically, horizontally, or diagonally.

If a cell is alive but either has no neighboring cells alive or only one alive, then in the next generation the cells dies of loneliness.

If a cell is alive and has four or more neighboring cells also alive, then in the next generation the cell dies of overcrowding.

A living cell with either two or three living neighbors remains alive in the next generation.

If a cell is dead, then it will become alive if it has exactly three neighboring cells that are alive. All other dead cells remain dead in the next generation.

All births and deaths take place at exactly the same time.

25

Example 1 Configuration Living neighbors

* *

0 0 0 0 0 0 1 1 1 0 0 2 1

*

2 0 0 2 1

*

2 0 0 1 1 1 0 By rule 2 both the living cells will die in the next generation and rule 5 shows that no cells will become alive, so that configuration dies out.

26 0 0 0 0 0

Example 2 0 0 0 0 0 0 0 1 2 2 1 0 0 2 3

*

3

*

2 0 0 2 3

*

3

*

2 0 0 1 2 2 1 0 0 0 0 0 0 0 Each of the living cells has a neighbor count of three, and hence remain alive, but the dead cells all have neighbor countsof two or less, and none of them becomes alive.

Thus all new configurations will be identical to this one.

27

Working Example

28

Another Example * * * * * * The two configuration continue to alternate from generation to generation.

29

The Algorithm

   Set up an initial Life configuration.

Print the Life configuration.

While the user wants to see further configurations:   Update the configuration by applying the rules of the Life game.

Print the current configuration.

30

The Class Life

enum status class

Life {

public:

{dead,alive};

void void

initialize(); print()

const void

update();

private:

; };

int

neighbor_count(

int

,

int

)

const

;

status

grid[maxrow][maxcol]; 31

The Main Program

int main() { Life configuration; } { welcome(); configuration.initialize(); configuration.print(); cout << "Continue viewing new generations?" << endl; while (user_says_yes()) configuration.update(); configuration.print(); cout << "Continue viewing new generations?" << endl; } return 0; 32

Initialize

void Life::initialize() { int row,col; for (row=0;rowfor (col=0;colwhile ((row!=-1) || (col!=-1)) { cin >> row >> col; if (row==-1 && col==-1) cout << "Done" << endl; else if ((row<0) || (row>=maxrow)) cout << "Row out of range" << endl; else if ((col<0) || (col>=maxcol)) cout << "Column out of range" << endl; else grid[row][col] = alive; } 33

}

Print

void Life::print() const { 34

}

Count Neighbors

int Life::neighbor_count (int row, int col) const { 35

}

Update

void Life::update () { 36

User Reply

bool user_says_yes() { char c; bool initial_response = true;

do

{ if (initial_response) cout << "(y/n)?"; else cout << "Respond with either y or n: "; initial_response = false; do { cin >> c; } while (c=='\n' || c==' ' || c=='\t'); } while (c!='y' && c!='Y' && c!='n' && c!='N'); } return (c=='y' || c=='Y'); 37

Programming Guidelines

      Choose meaningful names.

Use common prefixes or suffixes to associate names of the same general category, i.e.

input_file out_file total_file Be careful with the use of the letters “I” and “O”, i.e.

I = 1; x = 1; x = I; x = O; x = 0; Avoid global variables when possible.

Avoid side-effects (changing the values of global variables).

Keep the functions short – less than a page.

38

Documentation Guidelines

 The reading time for programs is much more than the writing time. “ Make your program readable ”.

       Place a prologue at the beginning of your program.

When each variable, constant, or type is declared explain what it is and how it is used. Better still, make this information evident from the name.

Introduce each significant part of your program (paragraph or function) and indicate where it ends if it is not obvious.

Avoid comments that parrot what the code does, i.e.

count++ // Increase counter by 1 The code itself should explain documentation should explain how why the program works. The it works and what it does.

Modify the comments along with the code.

Add pre-conditions and post-conditions to your functions.

39

Programming Questions

        Does the program solve the problem that is requested?

Does the program work correctly under all conditions?

Does the program have a good user interface?

Is the program logically and clearly written?

Is the program well documented?

Does the program make efficient use of time Does the program run on the right hardware?

and space ?

Can the program handle the maximum size of input?

40