the 11 th lecture jiří Šebesta
DESCRIPTION
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 - PowerPoint PPT PresentationTRANSCRIPT
Computers and programming 1
The 11th lecture
Jiří Šebesta
TOPIC – programming with dynamic variables
1. Coupling with dynamic variables2. Dynamic database - example
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
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.
Coupling with dynamic variables (3/13)
• Linear list approach - modified
Two static pointers to the first and last structure (for simple implementation).
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. - globalt_comp *last; // ptr to the last comp. - globalint 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
Coupling with dynamic variables (5/13)
void add(char *cname, int cjump) // add comp. record{
t_comp *ccomp; // ptr to a compccomp = (t_comp*)malloc(sizeof(t_comp));strcpy(ccomp->name, cname); // record fillingccomp->jump = cjump; cnt++; // the number of records plus oneif(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
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)
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
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
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 71if(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
}
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
Coupling with dynamic variables (11/13)
• Stack approach
One static pointer to the top of stack (heap) – system LIFO (last in – first out).
Coupling with dynamic variables (12/13)
• Tree approach
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]
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.
Dynamic database - example (2/5)
• Linear sorted list
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. - globalt_comp *last; // ptr to the last comp. - globalint 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
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
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
TOPIC OF THE NEXT LECTURE
1. Advanced algorithms in C
THANK YOU FOR YOUR ATTENTION