Download presentation
Presentation is loading. Please wait.
Published byOsborn Chambers Modified over 9 years ago
1
Computers and programming 1 The 11 th lecture Jiří Šebesta
2
TOPIC – programming with dynamic variables 1.Coupling with dynamic variables 2.Dynamic database - example
3
Coupling with dynamic variables (1/13) Approach using array of pointers Allocated static array of pointer to dynamic variable (structure) – max. number of structures is fixed in advance
4
Coupling with dynamic variables (2/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.
5
Coupling with dynamic variables (3/13) Linear list approach - modified Two static pointers to the first and last structure (for simple implementation).
6
Coupling with dynamic variables (4/13) typedef struct comp// competitor record { char name[10]; int jump; struct comp *next; }t_comp; t_comp *first;// ptr to the first comp. - global t_comp *last;// ptr to the last comp. - global int cnt = 0;// the number of recorded comp. - global Linear list – insertion of unknown number of competitors (broad jumpers) in to the list including results (length of jump) Structure and global variables comp is structure namespace t_comp is type namespace
7
Coupling with dynamic variables (5/13) void add(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; } Function for adding of a competitor and his length of jump to the linear list
8
Coupling with dynamic variables (6/13) void show(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); } List of competitors printing including length of jump according to insertion order (from the first to the last)
9
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); } main() function for application with commands
10
Coupling with dynamic variables (8/13) if(cmd=='S' || cmd=='s') show(); printf("\nA: Add, S: Show, Q: Quit"); scanf("%c", &cmd); fflush(stdin); } return 0; } Example: Ex71.c
11
Coupling with dynamic variables (9/13) Modification of the previous example by algorithm for searching of the best competitor Replenishment of the function add() void add(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 }
12
Coupling with dynamic variables (10/13) 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); } Example: Ex72.c
13
Coupling with dynamic variables (11/13) Stack approach One static pointer to the top of stack (heap) – system LIFO (last in – first out).
14
Coupling with dynamic variables (12/13) Tree approach
15
Coupling with dynamic variables (13/13) Complicated coupling of structures – family tree 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]
16
Dynamic database - example (1/5) Submission: 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.
17
Dynamic database - example (2/5) Linear sorted list
18
Dynamic database - example (3/5) typedef struct t_comp// competitor record { char name[10]; char country[10]; int jump; 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 Linear list – insertion of unknown number of competitors (broad jumpers) to the list including result (length of the jump) Structure and global variables
19
Dynamic database - example (4/5) void add(char *cname, char *ccountry, int cjump) { t_comp *ccomp, *prevcomp, *nextcomp;... Modified function for appending of the competitor and his length of the jump to the linear list with automatic sorting regarding the result Programming in lecture
20
Dynamic database - example (5/5) Example: Ex73.c Complete application will be issued in web pages in December, 5 as Ex73sol.c Records releasing from memory
21
TOPIC OF THE NEXT LECTURE 1.Advanced algorithms in C THANK YOU FOR YOUR ATTENTION
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.