1 / 21

# The 11 th lecture Jiří Šebesta - PowerPoint PPT Presentation

Computers and programming 1. The 11 th lecture Jiří Šebesta. TOPIC – programming with dynamic variables. Coupling with dynamic variables Dynamic database - example. Approach using array of pointers

I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.

## PowerPoint Slideshow about ' The 11 th lecture Jiří Šebesta' - lucie

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 - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript

The11th lecture

Jiří Šebesta

### TOPIC – programming with dynamic variables

Coupling with dynamic variables

Dynamic database - example

### 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 (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

### Coupling with dynamic variables (5/13)

{

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=ccomp;

}

}

### 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() according to insertion orderfunction 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);

}

if according to insertion order(cmd=='S'|| cmd=='s')

show();

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

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

}

return0;

}

Example: Ex71.c

### Coupling with dynamic variables (9/13)

• Replenishment of the function add()

{

// the same as in the example 71

if(cnt==1)

{ first = ccomp;// add the first record

last = ccomp;

}

else

last=ccomp;

}

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

}

void searching of the best competitor 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 searching of the best competitor

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

### 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]

### 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.

### Dynamic database - example (2/5)

• Linear list searching of the best competitor– 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

### Dynamic database - example (4/5)

{

t_comp *ccomp, *prevcomp, *nextcomp;

...

• Programming in lecture