Loading in 5 sec....

The 11 th lecture Jiří ŠebestaPowerPoint Presentation

The 11 th lecture Jiří Šebesta

- 98 Views
- Uploaded on
- Presentation posted in: General

The 11 th lecture Jiří Šebesta

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

Computers and programming1

The11th lecture

Jiří Šebesta

TOPIC – programming with dynamic variables

Coupling with dynamic variables

Dynamic database - example

- Approach using array of pointers
- Allocated static array of pointer to dynamic variable (structure) – max. number of structures is fixed in advance

Coupling with dynamic variables (1/13)

- Linear list approach
- The first pointer to the first structure is only allocated in memory. Each structure has pointer to the following structure as item – max. number of structures is restricted only by memory capacity.

Coupling with dynamic variables (2/13)

- Linear list approach - modified
- Two static pointers to the first and last structure (for simple implementation).

Coupling with dynamic variables (3/13)

- Linear list – insertion of unknown number of competitors (broad jumpers) in to the list including results (length of jump)

Coupling with dynamic variables (4/13)

- Structure and global variables

typedef struct comp// competitor record

{

charname[10];

intjump;

struct comp *next;

}t_comp;

t_comp *first;// ptr to the first comp. - global

t_comp *last;// ptr to the last comp. - global

intcnt=0;// the number of recorded comp. - global

comp is structure namespace

t_compis type namespace

- Function for adding of a competitor and his length of jump to the linear list

Coupling with dynamic variables (5/13)

voidadd(char*cname, int cjump)// add comp. record

{

t_comp *ccomp;// ptr to a comp

ccomp =(t_comp*)malloc(sizeof(t_comp));

strcpy(ccomp->name, cname);// record filling

ccomp->jump = cjump;

cnt++;// the number of records plus one

if(cnt==1)

{first = ccomp;// add the first record

last = ccomp;

}

else

{last->next=ccomp;// add next record

last=ccomp;

}

}

- List of competitors printing including length of jump according to insertion order (from the first to the last)

Coupling with dynamic variables (6/13)

voidshow(void)

{

t_comp *scomp;

int acnt=cnt;

scomp=first;

do

{

printf("%s: %d cm\n", scomp->name, scomp->jump);

scomp=scomp->next;

}

while(--acnt >0);

}

- main()function for application with commands

Coupling with dynamic variables (7/13)

int main(void)

{

char cmd, aname[10];

int ajump;

printf("\nA: Add, S: Show, Q: Quit\n");

scanf("%c", &cmd); fflush(stdin);

while(!(cmd =='Q'|| cmd =='q'))

{

if(cmd=='A'|| cmd=='a')

{

printf("\nName:");

scanf("%s", &aname); fflush(stdin);

printf("\nJump [cm]:");

scanf("%d",&ajump); fflush(stdin);

add(aname, ajump);

}

if(cmd=='S'|| cmd=='s')

show();

printf("\nA: Add, S: Show, Q: Quit");

scanf("%c",&cmd); fflush(stdin);

}

return0;

}

Coupling with dynamic variables (8/13)

Example: Ex71.c

- Modification of the previous example by algorithm for searching of the best competitor

Coupling with dynamic variables (9/13)

- Replenishment of the function add()

voidadd(char*cname, int cjump)// add comp. record

{

// the same as in the example 71

if(cnt==1)

{first = ccomp;// add the first record

last = ccomp;

}

else

{last->next=ccomp;// add next record

last=ccomp;

}

last->next= NULL;// last points to null address

}

void results(void)

{

t_comp *scomp,*gold;

int mjump = first->jump;

gold = first;

scomp = first->next;

do

{

if(scomp->jump > mjump)

{

gold = scomp; mjump = gold->jump;

}

scomp=scomp->next;

}

while(scomp != NULL);

printf("\nWin %s (%d cm)\n", gold->name, gold->jump);

}

Coupling with dynamic variables (10/13)

Example: Ex72.c

- Stack approach
- One static pointer to the top of stack (heap) – system LIFO (last in – first out).

Coupling with dynamic variables (11/13)

- Tree approach

Coupling with dynamic variables (12/13)

- Complicated coupling of structures – family tree

Coupling with dynamic variables (13/13)

strcpy(me->p_sists[0]->Name,”Jana”);

me->p_sists[0]-> p_mother = me-> p_mother;

//uncles:

me->p_mother-> p_brths[x]

me->p_father-> p_brths[x]

- Submission:

Dynamic database - example (1/5)

- Build-up a console application, which dynamically generate a database of competitors (broad jumpers). Each structure consists of competitor’s name, competitor’s country, length of the jump, and pointer to the other structure of competitor. Create a function for appending of the competitor including length of jump and function for printing of the competitors including their results.
- Modify the function for appending of competitor: a new competitor have to be add to the linear list according to his result (length of the jump) – list is always sorted regarding the length of the jump.

- Linear sorted list

Dynamic database - example (2/5)

- Linear list – insertion of unknown number of competitors (broad jumpers) to the list including result (length of the jump)

Dynamic database - example (3/5)

- Structure and global variables

typedef struct t_comp// competitor record

{

charname[10];

char country[10];

intjump;

t_comp *next;

};

t_comp *first;// ptr to the first comp. - global

t_comp *last;// ptr to the last comp. - global

int count=0;// the number of recorded comp. - global

- Modified function for appending of the competitor and his length of the jump to the linear list with automatic sorting regarding the result

Dynamic database - example (4/5)

void add(char*cname, char*ccountry, int cjump)

{

t_comp *ccomp, *prevcomp, *nextcomp;

...

- Programming in lecture

Records releasing from memory

Dynamic database - example (5/5)

Complete application will be issued in web pages in December, 5 as Ex73sol.c

Example: Ex73.c

TOPIC OF THE NEXT LECTURE

Advanced algorithms in C

THANK YOUFOR YOUR ATTENTION