Programiranje II Beleške za predavanja

Size: px
Start display at page:

Download "Programiranje II Beleške za predavanja"

Transcription

1 Programiranje II Beleške za predavanja Smer Informatika Matematički fakultet, Beograd Filip Marić i Predrag Janičić 2011.

2 2

3 Sadržaj 1 Dinamička alokacija memorije Funkcije malloc i calloc Funkcija realloc Greške u radu sa dinamičkom memorijom Fragmentisanje memorije Implementacija primitivnog alokatora memorije Hip Veličina steka i hipa I Osnovi algoritmike 17 2 Rekurzija Rekurzivne matematičke funkcije Matematička indukcija i rekurzija Primeri rekurzivnih funkcija Faktorijel Stepenovanje Fibonačijev niz Kule Hanoja Uzajamna rekurzija Nedostaci rekurzije Eliminisanje rekurzije Složenost algoritama Osnove analize algoritama:,,o notacija; klase složenosti Red algoritma; analiza najgoreg slučaja; O notacija Izračunavanje složenosti algoritama NP kompletnost Ispitivanje ispravnosti programa Neformalno ispitivanje ispravnosti programa Formalno ispitivanje ispravnosti programa Formalna specifikacija

4 4 SADRŽAJ Razvoj programa u terminima Horovih trojki Primeri Horovih trojki Primer dokazivanja ispravnosti sekvencijalnog programa Dokazivanje ispravnosti programa koji sadrže petlje i metoda induktivne invarijante Primer dokazivanja korektnosti programa koji sadrži petlju 41 5 Fundamentalni algoritmi Pretraživanje Pronalaženje karaktera u stringu Odredivanje maksimuma Linearno pretraživanje Binarno pretraživanje Korišćenje sistemske implementacije binarne pretrage Odredivanje nula funkcije Sortiranje Selection sortiranje Insertion sortiranje Bubble sortiranje Merge sortiranje Quick sortiranje Korišćenje sistemske implementacije quick sort-a Jednostavni algebarsko-numerički algoritmi Izračunavanje vrednosti polinoma Karacubin algoritam Generisanje kombinatornih objekata Varijacije Permutacije Kombinacije Particionisanje Algoritmi zasnovani na bitovskim operatorima Fundamentalne strukture podataka Liste Stek (LIFO lista) Red (FIFO lista) Dvostruko povezane (dvostruko ulančane) liste Kružne (ciklične, cirkularne) liste Liste: primer Stabla Binarna stabla Uredena binarna stabla Izrazi u formi stabla Grafovi Heš tabele

5 SADRŽAJ 5 II Principi razvoja programa 85 7 Rešavanje problema uz pomoć računara 87 8 Strukturna dekompozicija i druga načela pisanja programa Pisanje čitljivih programa: vizualni elementi programa karaktera u liniji Broj naredbi po liniji Nazubljivanje/uvlačenje teksta Imenovanje promenljivih i funkcija Komentari Komentari ne treba da objačnjavaju kôd Komentari treba da su takvi da ih je moguće održavati Komentari treba da budu koncizni Korišćenje specifičnih komentara Pisanje programa: modularnost i podela na datoteke Deljenje programa u više datoteka Modularnost Kako koristiti konstante u programu Pisanje programa: dizajniranje programa Dizajn u vidu tokovnika Funkcijski-orijentisan dizajn Strukturna dekompozicija Strukturalni model sistema III Principi programskih jezika Programski jezici i paradigme Istorijat razvoja viših programskih jezika Programske paradigme Uvod u prevodenje programskih jezika Implementacija programskih jezika Kratka istorija razvoja kompilatora Moderni kompilatori Struktura kompilatora Leksička analiza Sintaksna analiza Primer Teorija formalnih jezika Načini opisa leksike i sintakse programskih jezika Načini opisa semantike programskih jezika

6 6 SADRŽAJ IV Socijalni aspekti informatike Istorijski i društveni kontekst računarstva kao naučne discipline; društveni značaj računara i Interneta; profesionalizam, etički kodeks; autorska prava; intelektualna svojina, softverska piraterija Istorijski i društveni kontekst računarstva Profesionalizam i etički kodeks Rizici i pouzdanost računarskih sistema Intelektualna svojina i autorska prava Privatnost i gradanske slobode Računarski kriminal Ekonomski aspekti računarstva

7 Glava 1 Dinamička alokacija memorije 1.1 Funkcije malloc i calloc The functions malloc and calloc obtain blocks of memory dynamically. They are declared in <stdlib.h>. void *malloc(size_t n) returns a pointer to n bytes of storage, or NULL if the request cannot be satisfied. The memory returned by malloc() is not initialized. It can contain any random garbage. void *calloc(size_t n, size_t size) returns a pointer to enough free space for an array of n objects of the specified size, or NULL if the request cannot be satisfied. The storage is initialized to zero. The pointer returned by malloc or calloc has the proper alignment for the object in question, but it must be cast into the appropriate type, as in int *ip; ip = (int *) calloc(n, sizeof(int)); free(p) frees the space pointed to by p, where p was originally obtained by a call to malloc or calloc. There are no restrictions on the order in which space is freed, but it is a ghastly error to free something not obtained by calling malloc or calloc. It is also an error to use something after it has been freed. Ne sme se koristiti nešto što je već osloboẹno, ne sme se dva puta oslobaạti ista memorija. Always check the return value of malloc() and calloc(). Never assume that memory allocation will succeed. If the allocation fails, malloc() returns NULL. If you use the value without checking, it is likely that your program will immediately die from a segmentation violation (or segfault), which is an attempt to use memory not in your address space. If you check the return value, you

8 8 1 Dinamička alokacija memorije can at least print a diagnostic message and terminate gracefully. Or you can attempt some other method of recovery. #include <stdio.h> #include <stdlib.h> int main() { int n, i, *a; printf("unesi broj clanova niza : "); scanf("%d", &n); /* Nije moguce deklarisati niz od n elemenata; no, moguce je sledece:*/ a = (int*)malloc(n*sizeof(int)); /* Mora se proveriti da li je alokacija memorije uspesno izvrsena */ if (a == NULL) { fprintf(stderr, "Greska prilikom alokacije memorije\n"); return 1; /* a moze da se koristi kao obican niz */ for (i = 0; i<n; i++) scanf("%d",&a[i]); /* Oslobadjanje alocirane memorije */ free(a); return 0; 1.2 Funkcija realloc Function realloc reallocates memory blocks. Za njeno korišćenje potrebno je uključiti zaglavlja <stdlib.h> i <malloc.h>. void *realloc( void *memblock, size_t size ); Parameter memblock is a pointer to previously allocated memory block. Parameter size is a new size in bytes. realloc returns a void pointer to the reallocated (and possibly moved) memory block. The return value is NULL if the size is zero and the buffer argument is not NULL, or if there is not enough available memory to expand the block to the given size. In the first case, the original block is freed. In the second, the original block is unchanged. The return value points to a storage space that is guaranteed to be suitably aligned for storage of any type of object. To get a pointer to a type other than void, use a type cast on the return value.

9 1.3 Greške u radu sa dinamičkom memorijom 9 /* REALLOC.C: This program allocates a block of memory for * buffer and then uses _msize to display the size of that * block. Next, it uses realloc to expand the amount of * memory used by buffer and then calls _msize again to * display the new amount of memory allocated to buffer. */ #include <stdio.h> #include <malloc.h> #include <stdlib.h> int main() { long *buffer1, *buffer2; size_t size; if((buffer1 = (long*)malloc(1000*sizeof(long))) == NULL) exit(1); size = 1000*sizeof(long); printf("size of block after malloc of 1000 longs: %u\n", size); /* Reallocate and show new size: */ if((buffer2 = realloc( buffer1, size + (1000*sizeof(long)))) == NULL) { free(buffer1); exit(1); size = size + (1000*sizeof(long)); printf("size of block after realloc of 1000 more longs: %u\n", size); free(buffer2); return 0; Output Size of block after malloc of 1000 longs: 4000 Size of block after realloc of 1000 more longs: Greške u radu sa dinamičkom memorijom Curenje memorije (memory leaking). Memory leaking is the situation that occurs when dynamically allocated memory is lost to the program.

10 10 1 Dinamička alokacija memorije char * p; p = (char *) malloc(1000);... p = (char *) malloc(5000); Initially, 1000 bytes are dynamically allocated and the the address of those bytes is stored in p. later 5000 bytes are dyanmically allocated and the address is stored in p. However, the original 1000 bytes have not been returned to the system using free. Memory leak actually depends on the nature of the program. Any dynamic memory that s not needed should be released. In particular, memory that is allocated inside loops or recursive or deeply nested function calls should be carefully managed and released. Failure to take care leads to memory leaks, whereby the process s memory can grow without bounds; eventually, the process dies from lack of memory. This situation can be particularly pernicious if memory is allocated per input record or as some other function of the input: The memory leak won t be noticed when run on small inputs but can suddenly become obvious (and embarrassing) when run on large ones. This error is even worse for systems that must run continuously, such as telephone switching systems. A memory leak that crashes such a system can lead to significant monetary or other damage. Even if the program never dies for lack of memory, constantly growing programs suffer in performance, because the operating system has to manage keeping in-use data in physical memory. In the worst case, this can lead to behavior known as thrashing, whereby the operating system is so busy moving the contents of the address space into and out of physical memory that no real work gets done. Većina debagere detektuje da u programu postoji curenje memorije, ali ne može da pomogne u lociranju odgovarajuće greške u kodu. Postoje specijalizovani programi (memory leaks profiler) koji olakšavaju otkrivanje curenje memorije. Druge česte greške. Once free(p) is called, the memory pointed to by p is off limits. It now belongs to the allocation subroutines, and they are free to manage it as they see fit. They can change the contents of the memory or even release it from the process s address space! There are thus several common errors to watch out for with free(): Accessing freed memory If unchanged, p continues to point at memory that no longer belongs to the application. This is called a dangling pointer. In many systems, you can get away with continuing to access this memory, at least until the next time more memory is allocated or freed. In many others though, such access won t work. In sum, accessing freed memory is a bad idea: It s not portable or reliable, and the GNU Coding Standards disallows it. For this reason, it s a good idea to immediately set the program s pointer variable to NULL. If you then accidentally attempt to access freed memory, your program will

11 1.4 Fragmentisanje memorije 11 immediately fail with a segmentation fault (before you ve released it to the world, we hope). Freeing the same pointer twice This causes undefined behavior. Once the memory has been handed back to the allocation routines, they may merge the freed block with other free storage under management. Freeing something that s already been freed is likely to lead to confusion or crashes at best, and so-called double frees have been known to lead to security problems. Passing a pointer not obtained from malloc(), calloc(), or realloc() This seems obvious, but it s important nonetheless. Even passing in a pointer to somewhere in the middle of dynamically allocated memory is bad: free(p+10); /* Release all but first 10 elements. */ This call won t work, and it s likely to lead to disastrous consequences, such as a crash. (This is because many malloc() implementations keep bookkeeping information in front of the returned data. When free() goes to use that information, it will find invalid data there. Other implementations have the bookkeeping information at the end of the allocated chunk; the same issues apply.) Buffer overruns and underruns Accessing memory outside an allocated chunk also leads to undefined behavior, again because this is likely to be bookkeeping information or possibly memory that s not even in the address space. Writing into such memory is much worse, since it s likely to destroy the bookkeeping data. 1.4 Fragmentisanje memorije Often, applications that are free from memory leaks but frequently allocate and delocate dynamic memory show gradual performance degradation if they are kept running for long periods. Finally, they crash. Why is this? Recurrent allocation and deallocation of dynamic memory causes heap fragmentation, especially if the application allocates small memory chunks. A fragmented heap might have many free blocks, but these blocks are small and non-contiguous. To demonstrate this, look at the following scheme that represents the system s heap. Zeros indicate free memory blocks and ones indicate memory blocks that are in use: The above heap is highly fragmented. Allocating a memory block that contains five units (i.e., five zeros) will fail, although the systems has 12 free units in total. This is because the free memory isn t contiguous. On the other hand, the following heap has less free memory but it s not fragmented: malloc() is a nightmare for embedded systems. As with stacks, figuring the heap size is tough at best, a problem massively exacerbated by multitasking. malloc() leads to heap fragmentation though it may contain vast amounts

12 12 1 Dinamička alokacija memorije of free memory, the heap may be so broken into small, unusable chunks that malloc() fails. In simpler systems it is probably wise to avoid malloc() altogether. When there is enough RAM allocating all variables and structures statically yields the fastest and most deterministic behavior, though at the cost of using more memory. When dynamic allocation is unavoidable, by all means remember that malloc() has a return value! Fact is, it may fail, which will cause our program to crash horribly. If we are smart enough proactive enough to test every malloc() then an allocation error will still cause the program to crash horribly, but at least we can set a debug trap, greatly simplifying the task of finding the problem. The programmer should concentrate on optimizing busy heap allocation sites. What can you do to avoid heap fragmentation? First, use dynamic memory as little as possible. Secondly, try to allocate and de-allocate large chunks rather than small ones. For example, instead of allocating a single object, allocate an array of objects at once. As a last resort, use a custom memory pool. 1.5 Implementacija primitivnog alokatora memorije Let us illustrate memory allocation by writing a rudimentary storage allocator. There are two routines. The first, alloc(n), returns a pointer to n consecutive character positions, which can be used by the caller of alloc for storing characters. The second, afree(p), releases the storage thus acquired so it can be re-used later. The routines are rudimentary because the calls to afree must be made in the opposite order to the calls made on alloc. That is, the storage managed by alloc and afree is a stack, or last-in, first-out. The standard library provides analogous functions called malloc and free that have no such restrictions. The easiest implementation is to have alloc hand out pieces of a large character array that we will call allocbuf. This array is private to alloc and afree. Since they deal in pointers, not array indices, no other routine need know the name of the array, which can be declared static in the source file containing alloc and afree, and thus be invisible outside it. In practical implementations, the array may well not even have a name; it might instead be obtained by calling malloc or by asking the operating system for a pointer to some unnamed block of storage. The other information needed is how much of allocbuf has been used. We use a pointer, called allocp, that points to the next free element. When alloc is asked for n characters, it checks to see if there is enough room left in allocbuf. If so, alloc returns the current value of allocp (i.e., the beginning of the free block), then increments it by n to point to the next free area. If there is no room, alloc returns zero. afree(p) merely sets allocp to p if p is inside allocbuf.

13 1.6 Hip 13 #define ALLOCSIZE /* size of available space */ static char allocbuf[allocsize]; /* storage for alloc */ static char *allocp = allocbuf; /* next free position */ char *alloc(int n) /* return pointer to n characters */ { if (allocbuf + ALLOCSIZE - allocp >= n) { /* it fits */ allocp += n; return allocp - n; /* old p */ else /* not enough room */ return 0; void afree(char *p) /* free storage pointed to by p */ { if (p >= allocbuf && p < allocbuf + ALLOCSIZE) allocp = p; In general a pointer can be initialized just as any other variable can, though normally the only meaningful values are zero or an expression involving the address of previously defined data of appropriate type. The declaration static char *allocp = allocbuf; defines allocp to be a character pointer and initializes it to point to the beginning of allocbuf, which is the next free position when the program starts. This could also have been written static char *allocp = &allocbuf[0]; since the array name is the address of the zeroth element. The test if (allocbuf + ALLOCSIZE - allocp >= n) { /* it fits */ checks if there s enough room to satisfy a request for n characters. If there is, the new value of allocp would be at most one beyond the end of allocbuf. If the request can be satisfied, alloc returns a pointer to the beginning of a block of characters (notice the declaration of the function itself). If not, alloc must return some signal that there is no space left. C guarantees that zero is never a valid address for data, so a return value of zero can be used to signal an abnormal event, in this case no space. 1.6 Hip Svaki program ima prostor raspoložive memorije koju može da koristi za vreme izvršavanja. Ovaj prostor raspoložive memorije naziva se slobodna memorija ili hip. Alokacija memorije u fazi izvršavanja naziva se dinamička alokacija memorije. Ona se postiže funkcijama kao što je malloc. Objekti koji su alocirani u slobodnom memorijskom prostoru nisu imenovani. malloc ne vraća

14 14 1 Dinamička alokacija memorije stvarni alocirani objekat, već njegovu adresu. Preko te adrese se indirektno manipuliše objektom. Kada završimo sa korišćenjem objekta, moramo da eksplicitno vratimo memoriju tog objekta u slobodnu memoriju. To se postiže funkcijom free. free se ne poziva nad adresama objektata koji nisu alocirani dinamički. The heap segment provides more stable storage of data for a program; memory allocated in the heap remains in existence for the duration of a program. The memory allocated in the heap area, if initialized to zero at program start, remains zero until the program makes use of it. Thus, the heap area need not contain garbage. The heap is where dynamic memory (obtained by malloc() and friends) comes from. As memory is allocated on the heap, the process s address space grows, as you can see by watching a running program with the ps command. Although it is possible to give memory back to the system and shrink a process s address space, this is almost never done and this leads to memory fragmentation. It is typical for the heap to grow upward. This means that successive items that are added to the heap are added at addresses that are numerically greater than previous items. It is also typical for the heap to start immediately after the data segment. Main characteristics of the heap: freelist - list of free space on allocation - memory manager finds space and marks it as used changing freelist; on deallocation - memory manager marks space as free changing freelist; memory fragmentation - memory fragments into small blocks over lifetime of program; Veličina steka i hipa Some operating systems may have the size of the heap and stack defined at run time. In UNIX operating system, the heap and the stack grow towards each other. The stack grows in the up direction and the heap grows in the down direction as indicated in the above diagram. When the stack and the heap collide the program will crash. This is an extremely important concept for students to understand. It is also important to understand what is going on in the run-time stack. Traditionally in a process address space you would see stack growing upside down and heap growing upwards. An application wise difference is all the memory allocated dynamically gets allocated on heap viz. malloc free, calloc etc. The stack of an process contains stack frames( containing the return address linkage for a function and the data required in a stack which has the qualifier auto ).

15 1.6 Hip 15 Although it s theoretically possible for the stack and heap to grow into each other, the operating system prevents that event, and any program that tries to make it happen is asking for trouble. This is particularly true on modern systems, on which process address spaces are large and the gap between the top of the stack and the end of the heap is a big one. The different memory areas can have different hardware memory protection assigned to them. The details, of course, are hardware and operating-system specific and likely to change over time. Of note is that both Standard C and C++ allow const items to be placed in read-only memory. Heap u Win32 sistemima: In its simplest form, the default heap spans a range of addresses. Some ranges are reserved, while others are committed and have pages of memory associated with them. In this case the addresses are contiguous, and they all originated from the same base allocation address. In some cases the default heap needs to allocate more memory than is available in its current reserved address space. For these cases the heap can either fail the call that requests the memory, or reserve an additional address range elsewhere in the process. The default heap manager opts for the second choice. When the default heap needs more memory than is currently available, it reserves another 1MB address range in the process. It also initially commits as much memory as it needs from this reserved address range to satisfy the allocation request. The default heap manager is then responsible for managing this new memory region as well as the original heap space. If necessary, it will repeat this throughout the application until the process runs out of memory and address space. So the default heap is not really a static heap at all. In fact, it may seem like a waste of energy to bother with managing the size of the initial heap since the heap always behaves in the same way after initialization. Managing the size of the default heap offers one advantage, though. It takes time to locate and reserve a new range of addresses in a process; you can save time by simply reserving a large enough address range initially. If you expect your application to require more heap space than the 1MB default, reserve more address space initially to avoid allocating another region of addresses in your process later. Remember, each application has 2 gigabytes (GB) of address space to work with, and requires very little physical memory to support it. Na starim MS DOS sistemima, postojala su različita vrlo stroga ograničenja u vezi sa veličinama memorijskih segmenata.

16 16 1 Dinamička alokacija memorije

17 Deo I Osnovi algoritmike

18

19 Glava 2 Rekurzija In mathematics and computer science, recursion specifies (or constructs) a class of objects or methods (or an object from a certain class) by defining a few very simple base cases or methods (often just one), and then defining rules to break down complex cases into simpler cases. For example, the following is a recursive definition of person s ancestors: One s parents are one s ancestors (base case); The parents of any ancestor are also ancestors of the person under consideration (recursion step). It is convenient to think that a recursive definition defines objects in terms of previously defined objects of the class to define. Primer 2.1 (Recursive humour). A common joke is the following definition of recursion. Recursion See Recursion. This is a parody on references in dictionaries, which in some careless cases may lead to circular definitions. Every joke has an element of wisdom, and also an element of misunderstanding. This is also an example of an erroneous recursive definition of an object, the error being the absence of the termination condition (or lack of the initial state, if to look at it from an opposite point of view). Newcomers to recursion are often bewildered by its apparent circularity, until they learn to appreciate that a termination condition is key. 2.1 Rekurzivne matematičke funkcije A function may be partly defined in terms of itself. A familiar example is the Fibonacci sequence: F (n) = F (n 1) + F (n 2). For such a definition to be useful, it must lead to values which are non-recursively defined, in this case F (0) = 0 and F (1) = 1.

20 20 2 Rekurzija Another example is factorial. Factorial of a number, say n, is equal to the product of all integers from 1 to n. Factorial of n is denoted by n! = n or by 1! = 1 and n! = n (n 1)!, for n > 1. Eg: 10! = Matematička indukcija i rekurzija Now recall proofs by induction. Show that the theorem is true for the smallest case. This can usually be done by inspection. basis Show that if the theorem is true for the basis cases, it can be extended to include the next case. If the theorem is true for the case k = n 1, show that it is true for the case k=n. Inductive hypothesis assumes that the theorem is true for the case k = n 1. Similarity with recursive programming: The base case of a recursive method is the case that can be solved without a recursive call. For the general (non-base) case, k = n, we assume that the recursive method works for k = n 1. Recursion is the programming equivalent of induction. 2.3 Primeri rekurzivnih funkcija Recursion is an algorithmic technique where a function, in order to accomplish a task, calls itself with some part of the task. Programs using recursion can be made to do all sorts of things, right from calculating the factorial of a number, to playing complex games against human intelligence. In normal procedural languages, one can go about defining functions and procedures, and calling these from the parent functions. Some languages also provide the ability of a function to call itself. This is called recursion Faktorijel The simplest program to calculate factorial of a number is a loop with a product variable. Instead, it is possible to give a recursive definition for Factorial as follows: (1) If n=1, then Factorial of n = 1 (2) Otherwise, Factorial of n = product of n and Factorial of (n 1)

21 2.3 Primeri rekurzivnih funkcija 21 The following code fragment depicts recursion at work. int factorial(int n) { if (n==1) return 1; else return n*factorial(n-1); The important thing to remember when creating a recursive function is to give an end-condition. We don t want the function to keep calling itself forever. Somehow, it should know when to stop. There are many ways of doing this. One of the simplest is by means of an if condition statement, as above. In the above example, the recursion stops when n reaches 1. In each instance of the function, the value of n keeps decreasing. So it ultimately reaches 1 and ends. Of course, the above function will run infinitely if the initial value of n is less than 1. So the function is not perfect. The n == 1 condition should be changed to n <= 1. Imagination of recursion is a bit tricky. Think of clones. Say you have a machine to make clones of yourself, and (for lack of a better pass-time) decide to find the factorial of a number, say 10, using your clones Stepenovanje Izracunavanje x k : float stepen_sporo(float x, int k) { int i; float m = 1.0f; for(int i=0; i<k; i++) m *= x; return m; /* k >= 0 */ float stepen_sporo(float x, int k) { if (k == 0) return 1; else return x * stepen_sporo(x, k - 1);

22 22 2 Rekurzija /* k >= 0 */ float stepen_brzo(float x, int k) { if (k==0) return 1; else if (k % 2 == 0) return stepen_brzo(x * x, k / 2); else return x * stepen_brzo(x, k - 1); Fibonačijev niz Consider the Fibonacci number sequence, defined as the sequence: 0, 1, 1, 2, 3, 5, 8, 13,... where n is a non-negative integer. A method to generate Fibonacci numbers can be implemented recursively. int fib(int n) { if(n <= 1) return n; else return fib(n-1) + fib(n-2); Kule Hanoja Definition: Given three posts (towers) and n disks of decreasing sizes, move the disks from one post to another one at a time without putting a larger disk on a smaller one. The minimum is 2n-1 moves. The ancient legend was invented by De Parville in A solution using recursion is: to move n disks from post A to post B (1) recursively move the top n-1 disks from post A to C, (2) move the n-th disk from A to B, and (3) recursively move the n-1 disks from C to B. A solution using iteration is: on odd-numbered moves, move the smallest disk clockwise. On even-numbered moves, make the single other move which is possible. void tower(int n, char start, char finish, char spare) { if (n > 0) { tower(n-1,start,spare,finish); printf("moving a disk from peg %c to %c",start,finish); tower(n-1,spare,finish,start); Calling the above method with n=4, i.e., tower(4, A, C, B ) yields the following result:

23 2.4 Uzajamna rekurzija 23 Moving a disk from peg A to peg B Moving a disk from peg A to peg C Moving a disk from peg B to peg C Moving a disk from peg A to peg B Moving a disk from peg C to peg A Moving a disk from peg C to peg B Moving a disk from peg A to peg B Moving a disk from peg A to peg C Moving a disk from peg B to peg C Moving a disk from peg B to peg A Moving a disk from peg C to peg A Moving a disk from peg B to peg C Moving a disk from peg A to peg B Moving a disk from peg A to peg C Moving a disk from peg B to peg C 2.4 Uzajamna rekurzija So far we have only considered recursive methods that call themselves. Another type of recursion involves methods that cyclically call each other. This is known as cyclical or mutual recursion. In the following example, methods A and B are mutually recursive. void A(int n) { if (n <= 0) return; n--; B(n); void B(int n) { if (n <= 0) return; n--; A(n); 2.5 Nedostaci rekurzije Redundantna izračunavanja Consider now the execution of the function for Fibonacci sequence for n=5. Note the redundant calculations: 3 calls to fib(0), 5 calls to fib(1), and so on. Each recursive call does more and more redundant work, resulting in exponential growth. Cena poziva All local variables and formal parameters for the procedure are stored on top of a stack. When a recursive call is made, new copies are pushed. This can be very time and space consuming. In such cases, when possible, recursion should be replaced by iterative solution. Na primer, gore navedena funkcija fib može se zameniti iterativnom funkcijom koja dinamički alocira prostor (korišćenjem funkcije realloc) za elemente Fibonačijevog niza ili iterativnom funkcijom koja ne pamti sve elemente niza do indeksa n već samo dva prethodna:

24 24 2 Rekurzija int fib(int n) { int f1,f2,tmp,i; if (n<=1) return n; f1 = 0; f2 = 1; for(i=3;i<=n;i++) { tmp = f2; f2 = f1+f2; f1 = tmp; return f2; 2.6 Eliminisanje rekurzije Write an iterative, non-recursive version of the following routine: void r(int x) { if (p(x)) a(x); else { b(x); r(f(x)); where p, a, b, and f are arbitrary functions. Rešenje: void r(int x) { while(!p(x)) { b(x); x = f(x); a(x);

25 2.6 Eliminisanje rekurzije 25 Pitanja i zadaci za vežbu Pitanje Napisati rekurzivnu funkciju koja za dato n iscrtava trougao dimenzije n: (a) (b) (c) (d) Napisati rekurzivnu funkciju koja prikazuje sve cifre datog celog broja i to: (a) s leva na desno; (b) s desna na levo. 3. Napisati rekurzivnu funkciju koja odreduje binarni (oktalni, heksadekadni) zapis datog celog broja. 4. Napisati rekurzivnu funkciju koja izračunava zbir cifara datog celog broja. 5. Napisati rekurzivnu funkciju koja izračunava broj cifara datog celog broja. 6. Napisati rekruzivnu funkciju koja izračunava broj parnih cifara datog celog broja. 7. Napisati rekurzivnu funkciju koja izračunava najveu cifru datog broja. 8. Napisati rekurzivnu funkciju koja uklanja sva pojavljivanja date cifre iz datog broja. 9. Napisati rekurzivnu funkciju koja kreira niz cifara datog celog broja. 10. Napisati rekurzivnu funkciju koja obrće cifre datog celog broja. 11. Napisati rekurzivnu funkciju koja obrće niz brojeva. 12. Napisati rekurzivnu funkciju koja ispituje da li su elementi nekog niza brojeva poredani palindromski (isto od napred i od pozadi). 13. Napisati rekruzivnu funkciju koja obrće nisku. 14. Napisati rekurzivnu funkciju koja izbacuje sve parne cifre datog celog broja. 15. Napisati rekurzivnu funkciju koja iz datog broja izbacuje svaku drugu cifru. 16. Napisati rekurzivnu funkciju koja posle svake neparne cifre datog broja dodaje Napisati rekurzivnu funkciju za odredivanje NZD dva broja (Euklidov algoritam).

26 26 2 Rekurzija

27 Glava 3 Složenost algoritama Razmatra se vremenska složenost algoritma; prostorna (memorijska) složenost algoritma. Primer 3.1. Izračunavanje vrednosti faktorijela prirodnog broja zahteva n poredenja i n 1 množenje; to je ukupno 2n 1 koraka, te je vremenska složenost algoritma linearna. Klasu algoritama koji imaju linarnu složenost označavamo sa O(n). Često se algoritmi ne izvršavaju isto za sve ulaze istih veličina, pa je potrebno naći način za opisivanje i poredenje efikasnosti različitih algoritama. Analiza najgoreg slučaja zasniva procenu složenosti algoritma na najgorem slučaju (na slučaju za koji se algoritam najduže izvršava). Ta procena može da bude varljiva, ali ne postoji bolji opšti način za poredenje efikasnosti algoritama. Čak i kada bi uvek bilo moguće izračunati prosečno vreme izvršavanja algoritma, i takva procena bi često mogla da bude varljiva. Ako algoritam u najgorem slučaju zahteva an 2 +bn+c koraka, onda kažemo da je on kvadratne složenosti i da pripada klasi O(n 2 ). Aditivne i multiplikativne konstante ne utiču na klasu kojoj funkcija pripada. Primer 3.2. Važi: n 2 = O(n 2 ) 3 n = O(n 2 ) 3.1 Osnove analize algoritama:,,o notacija; klase složenosti Svojstva programa:

28 28 3 Složenost algoritama zaustavljanje 1 korektnost kompleksnost (složenost) vremenska prostorna (memorijska) Primer 3.3. Izračunavanje vrednosti faktorijela prirodnog broja: n poredjenja i n 1 množenje; linearna složenost. Primer 3.4. Odredjivanje najmanjeg elementa u nizu od n prirodnih brojeva: linearna složenost. Primer 3.5. Sortiranje niza od n prirodnih brojeva odredjivanjem najmanjeg u tekućem nizu: n(n 1)/2 vremenskih jedinica (kvadratna složenost). 3.2 Red algoritma; analiza najgoreg slučaja; O notacija Definicija 3.1. Ako postoje pozitivna konstanta c i prirodan broj N takvi da za funkcije f i g nad prirodnim brojevima važi f(n) c g(n) za sve vrednosti n veće od N onda pišemo i čitamo f je veliko o od g. f = O(g) NOTA BENE: O nije funkcija; O označava klasu funkcija. Aditivne i multiplikativne konstante ne utiču na klasu kojoj funkcija pripada. Primer 3.6. Važi: n 2 = O(n 2 ) n = O(n 2 ) 10 n = O(n 2 ) 10 n 2 + 8n + 10 = O(n 2 ) n 2 = O(n 3 ) 2 n = O(2 n ) 1 Tjuring je tridesetih godina dvadesetog veka dokazao da ne postoji opšti postupak kojim se za proizvoljan program može utvrditi da li se zaustavlja (to je tzv. halting problem). Dakle, postoje funkcije koje nisu izračunljive.

29 3.3 Izračunavanje složenosti algoritama 29 2 n + 10 = O(2 n ) 10 2 n + 10 = O(2 n ) 2 n + n 2 = O(2 n ) 3 n + 2 n = O(3 n ) 2 n + 2 n n = O(2 n n) Definicija 3.2. Ako je T (n) vreme izvršavanja algoritam A (čiji ulaz karakteriše prirodan broj n), ako važi T = O(g) i ako u najgorem slučaju vrednost T (n) dostiže vrednost c g(n) (gde je c pozitivna konstanta), onda kažemo da je algoritam A složenosti (ili reda) g i da pripada klasi O(g). Često se algoritmi ne izvršavaju isto za sve ulaze istih veličina, pa je potrebno naći način za opisivanje i poredjenje efikasnosti različitih algoritama. Analiza najgoreg slučaja zasniva procenu složenosti algoritma na najgorem slučaju (na slučaju za koji se algoritam najduže izvršava). Ta procena može da bude varljiva, ali ne postoji bolji opšti način za poredjenje efikasnosti algoritama. Čak i kada bi uvek bilo moguće izračunati prosečno vreme izvršavanja algoritma, i takva procena bi često mogla da bude varljiva. Analiziranje najboljeg slučaja, naravno, ima još manje smisla. Primer 3.7. Algoritam za izračunavanje vrednosti faktorijela prirodnog broja je reda n, tj. pripada klasi O(n), tj. on je linearne složenosti. Primer 3.8. Algoritam za sortiranje niza od n prirodnih brojeva odredjivanjem najmanjeg u tekućem nizu je reda n 2, tj. pripada klasi O(n 2 ), tj. on je kvadratne složenosti jer važi n(n 1) n2 za sve vrednosti n 1. i u najgorem slučaju (za ovaj algoritam svi se slučajevi mogu smatrati najgorim) dostiže vrednost koja kvadratno zavisi od n. 3.3 Izračunavanje složenosti algoritama Primer 3.9. Neka je T (n) vreme izvršavanja algoritma A za izračunavanje faktorijela broja n. Važi T (1) = 1 (tj. za n = 1 algoritam utroši samo jednu vremensku jedinicu) i T (n) = T (n 1) + 2 (nakon izračunavanja vrednosti (n 1)! algoritam treba da izvrši još jedno poredjenje i još jedno množenje; smatramo da i poredjenje i množenje troše po jednu vremensku jedinicu; možemo da smatramo i da poredjenje i množenje zajedno

30 30 3 Složenost algoritama troše jednu vremensku jedinicu ili c vremenskih jedinica to ne utiče na rezultat koji govori o redu algoritma A). Dakle, T (n) = T (n 1) + 2 = T (n 2) =... = T (1) = {{ n 1 = 1 + 2(n 1) = 2n + 1 = O(n), pa je algoritam A reda n (tj. pripada klasi O(n), tj. on je linearne složenosti). Pitanje 2. Ako za vreme izvršavanja T (n) algoritma A (gde n odredjuje ulaznu vrednost za algoritam) važi T (n) = T (n 1) + n/2 i T (1) = 1, odrediti složenost algoritma A. Rešenje: T (n) = T (n 1) + n n 1 = T (n 2) + + n = T (1) n 1 + n 2 2 = = T (1) ( n) = ( ) n(n + 1) 1 = n2 + n + 2, pa je algoritam A kvadratne složenosti. Pitanje 3. Ako za vreme izvršavanja T (n) algoritma A (gde n odredjuje ulaznu vrednost za algoritam) važi T (n + 2) = 8T (n + 1) 15T (n) (za n 1) i T (1) = 1, T (2) = 4, odrediti složenost algoritma A. Rešenje: Karakteristična jednačina za navedenu homogenu rekurentntu vezu drugog reda je t 2 = 8t 15 i njeni koreni su t 1 = 3 i t 2 = 5. Opšti član niza T (n) može biti izražen u obliku tj. Iz T (1) = 1, T (2) = 4 dobija se sistem T (n) = α t n 1 + β t n 2, T (n) = α 3 n + β 5 n, α 3 + β 5 = 1 α 9 + β 25 = 4 čije je rešenje (α, β) = (1/6, 1/10). Dakle, važi pa je algoritam A reda O(5 n ). T (n) = 1 6 3n n,

31 3.3 Izračunavanje složenosti algoritama 31 (Zaista, za npr. c = 1 važi T (n) = 1 6 3n n 1 6 5n n pa je T (n) = O(5 n ).) ( ) 5 n 1 5 n = c 5 n, 10 Pitanje 4. Ako za vreme izvršavanja T (n) algoritma A (gde n odredjuje ulaznu vrednost za algoritam) važi T (n + 2) = 4T (n + 1) 4T (n) (za n 1) i T (1) = 6, T (2) = 20, odrediti složenost algoritma A. Rešenje: vezu je Karakteristična jednačina za navedenu homogenu rekurentntu t 2 = 4t 4 i njen dvostruki koren je t 1 = 2. Opšti član niza T (n) može biti izražen u obliku tj. Iz T (1) = 6, T (2) = 20 dobija se sistem T (n) = α t n 1 + β n t n 2. T (n) = α 2 n + β n 2 n. α 2 + β 2 = 6 α 4 + β 8 = 20 čije je rešenje (α, β) = (1, 2), pa je T (n) = 2 n + 2 n 2 n, odakle sledi da je T (n) = O(n 2 n ). Pitanje 5. Odrediti složenost algoritma za rešavanje problema kule Hanoja. Rešenje: Važi T (1) = 1 i T (n) = 2T (n 1) + 1 (i T (2) = 2T (1) + 1 = 3). Iz T (n) 2T (n 1) = 1 = T (n 1) 2T (n 2) (za n > 2) sledi T (n) = 3T (n 1) 2T (n 2). Karakteristična jednačina ove veze je t 2 = 3t 2 i njeni koreni su 2 i 1. Iz sistema α 2 + β 1 = 1 sledi α = 1 i β = 1, pa je što znači da je algoritam reda O(2 n ). α 4 + β 1 = 3 T (n) = 1 2 n + ( 1) 1 n = 2 n 1, Pitanje 6. Algoritam A izvršava se za vrednost n (n > 1) primenom istog algoritma za vrednost n 1, pri čemu se za svodjenje problema koristi algoritam B za vrednost n 1. Algoritam B izvršava se za vrednost n (n > 1) trostrukom primenom istog algoritma za vrednost n 1, pri čemu se za svodjenje problema koristi algoritam A za vrednost n 1. Algoritmi A i B se za n = 1 izvršavaju jednu vremensku jedinicu. Izračunati vreme izvršavanja algoritma A za ulaznu vrednost n.

32 32 3 Složenost algoritama Rešenje: Neka je A(n) vreme izvršavanja algoritma A za ulaznu vrednost n i neka je B(n) vreme izvršavanja algoritma B za ulaznu vrednost n. Na osnovu uslova zadatka važi: A(1) = B(1) = 1 (3.1) Iz jednakosti (2) imamo: A(n) = A(n 1) + B(n 1) (3.2) B(n) = 3B(n 1) + A(n 1) (n > 1) (3.3) pa, uvrštavanjem u jednakost (3), za n > 1 važi: B(n) = A(n + 1) A(n) (3.4) B(n 1) = A(n) A(n 1) (3.5) A(n + 1) 4A(n) + 2A(n 1) = 0 (3.6) Pomoću ove rekurentne veze i početnih uslova: A(1) = 1 i A(2) = A(1)+B(1) = 2, možemo odrediti vrednost A(n). Karakteristična jednačina relacije (6) je: x 2 4x + 2 = 0 Njeni koreni su x 1 = i x 2 = 2 2, pa je opšte rešenje rekurentne jednačine (6) oblika: A(n) = c 1 (2 + 2) n + c 2 (2 2) n za neke c 1, c 2 R. Konstante c 1 i c 2 odredjujemo pomoću početnih uslova, rešavajući sledeći sistem linearnih jednačina po c 1 i c 2 : 1 = A(1) = c 1 (2 + 2) + c 2 (2 2) 2 = A(2) = c 1 (2 + 2) 2 + c 2 (2 2) 2 Dobija se da je c 1 = 1 4 (2 2), a c 2 = 1 4 (2 + 2), pa je konačno: A(n) = 1 2 ((2 + 2) n 1 + (2 2) n 1 ) Pitanje 7. Problem P ima parametar n (n je prirodan broj) i rešava se primenom algoritama A i B. Algoritam A rešava problem za vrednost n (n > 1). primenom algoritma B za vrednost n 1, pri čemu se za svodjenje problema troši n vremenskih jedinica. Algoritam B rešava problem za vrednost n (n > 1). primenom algoritma A za vrednost n 1, pri čemu se za svodjenje problema troši n vremenskih jedinica. Problem za n = 1, algoritam A rešava trivijalno za jednu vremensku jedinicu, a algoritam B za dve vremenske jedinice. Izračunati vreme izvršavanja algoritma A za ulaznu vrednost n.

33 3.3 Izračunavanje složenosti algoritama 33 Rešenje: Označimo sa a n vreme izvršavanja algoritma A, a sa b n vreme izvršavanja algoritma B za ulaznu vrednost n. Na osnovu uslova zadatka je: a 1 = 1 b 1 = 2 a n = b n 1 + n (n > 1) b n = a n 1 + n (n > 1) Kako je b n 1 = a n 2 + n 1, zaključujemo da je a n = a n 2 + n 1 + n Primenom matematičke indukcije dokažimo da za neparne vrednosti n važi a n = n(n+1) 2. Tvrdjenje važi za n = 1 jer je a 1 = 1 = Pretpostavimo da je tvrdjenje tačno za neki neparan broj n i dokažimo da važi i za sledeći neparan broj - n + 2: a n+2 = b n+1 + n + 2 = a n + n n + 2 a n+2 = n(n+1) 2 + n n + 2 a n+2 = n(n+1)+2(n+1)+2(n+2) 2 a n+2 = (n+1)(n+2)+2(n+2) 2 a n+2 = (n+2)(n+1+2) 2 a n+2 = (n+2)(n+3) 2 Dakle, na osnovu principa matematičke indukcije tvrdjenje važi za sve neparne brojeve. Dokažimo da za parne vrednosti n važi a n = n(n+1) Za n = 2 tvrdjenje je tačno: a 2 = b = = Pretpostavimo da je tvrdjenje tačno za neki paran broj n i dokažimo da je tačno i za n + 2, tj. za sledeći paran broj: a n+2 = b n+1 + n + 2 = a n + n n + 2 a n+2 = n(n+1) n n + 2 a n+2 = n(n+1)+2(n+1)+2(n+2) a n+2 = (n+1)(n+2)+2(n+2) a n+2 = (n+2)(n+1+2) a n+2 = (n+2)(n+3) Dakle, tvrdjenje je tačno za sve parne brojeve. Konačno imamo da je: a n = { n(n+1) Teorema 3.1. Rešenje rekurentne relacije za n parno n(n+1) 2 za n neparno T (n) = at (n/b) + cn k,

34 34 3 Složenost algoritama gde su a i b celobrojne konstante (a 1, b 1) i c i k pozitivne konstante je O(n log b a ), ako je a > b k T (n) = O(n k log n), ako je a = b k O(n k ), ako je a < b k 3.4 NP kompletnost Primer Šef protokola na jednom dvoru treba da organizuje bal za predstavnike ambasada. Kralj traži da na bal bude pozvan Peru ili da ne bude pozvan Katar (Qatar). Kraljica zahteva da budu pozvani Katar ili Rumunija (ili i Katar i Rumunija). Princ zahteva da ne bude pozvana Rumunija ili da ne bude pozvan Peru (ili da ne budu pozvani ni Rumunija ni Peru). Da li je moguće organizovati bal i zadovoljiti zahteve svih članova kraljevske porodice? Ako su p, q i r bulovske (logičke) promenljive (koje mogu imati vrednosti true ili false, tj. ili ), navedeni problem može biti formulisan na sledeći način: da li je zadovoljiv logički iskaz (p q) (q r) ( r p). Zadovoljivost navedenog iskaza može biti odredjena tako što bi bile ispitane sve moguće interpretacije sve moguće dodele varijablama p, q i r. Ako je u nekoj interpretaciji vrednost datog logičkog iskaza true, onda je dati iskaz zadovoljiv. Za izabranu, fiksiranu interpretaciju može se u konstantnom vremenu utvrditi da li je istinitosna vrednost true. Za tri promenljive ima 2 n interpretacija, pa je red ovog algoritma za ispitivanje zadovoljivosti logičkih iskaza reda O(2 n ) (algoritam je eksponencijalne složenosti). Pitanje je da li postoji algoritam koji navedeni problem rešava u polinomijalnom vremenu. Definicija 3.3. Za algoritam sa ulaznom vrednošću n kažemo da je polinomijalne složenosti ako je njegovo vreme izvršavanja O(P (n)) gde je P (n) polinom po n. Klasu polinomijalnih algoritama označavamo sa P. Definicija 3.4 (Pojednostavljena definicija klase NP). Ako neki problem može da se predstavi u vidu najviše eksponencijalno mnogo instanci i ako za bilo koju instancu može da bude rešen u polinomijalnom vremenu, onda kažemo da problem pripada klasi NP. Definicija 3.5 (Formalna definicija klase NP). Ako je U skup svih mogućih ulaznih vrednosti za neki problem odlučivanja (za koji je potrebno dati odgovor da ili ne) i L U skup svih ulaznih vrednosti za koje je odgovor na problem potvrdan, onda L zovemo jezik koji odgovara problemu. Kažemo da nedetrministički algoritam prepoznaje jezik L ako važi: za zadate ulazne vrednosti x, moguće je pretvoriti svaki nd-izbor koji se koristi tokom izvršavanja algoritma u stvarni izbor takav da će algoritam prihvatiti x ako i samo ako je x L. Klasu svih problema za koje postoje nedeterministički algoritmi čije je vreme izvršavanja je polinomijalne složenosti zovemo N P klasa.

35 3.4 NP kompletnost 35 Očigledno je da važi P NP, ali se još uvek ne zna 2 da li važi P = NP : P = NP? Ako bi se pokazalo da neki problem iz klase NP nema polinomijalno rešenje, onda bi to značilo da ne važi P = NP. Ako neki problem iz klase NP ima polinomijalno rešenje, onda to još ne znači da važi P = NP. Za probleme iz posebne potklase klase NP (to je klasa NP -kompletnih problema) važi da ako neki od njih ima polinomijalno rešenje, onda važi P = NP. Primer Problem ispitivanja zadovoljivosti logičkih iskaza (SAT ) pripada klasi NP, ali se ne zna da li pripada klasi P. Definicija 3.6. Za problem X kažemo da je N P -težak problem ako je svaki N P problem polinomijalno svodljiv na X. Definicija 3.7. Za problem X kažemo da je NP -kompletan problem ako pripada klasi NP i ako je NP -težak. Teorema 3.2. Ako bilo koji NP -težak problem pripada klasi P, onda važi P = NP. Teorema 3.3. Problem X je N P -kompletan ako X pripada klasi NP Y je polinomijalno svodljiv na X, gde je Y neki N P -kompletan problem. Dakle, ako znamo da je neki problem NP -kompletan, onda na osnovu prethodne teoreme možemo da pokažemo da su i svi N P -problemi na koje ga je moguće svesti takodje NP -kompletni. Dugo se tragalo za pogodnim NP - kompletnim problemom za koji bi moglo da se pronadje polinomijalno rešenje, što bi značilo da važi P = NP. Zato je bilo važno otkriti što više NP -kompletnih problema. Postavljalo se pitanje da li uopšte postoji ijedan N P -kompletan problem (korišćenjem prethodne teoreme može da se utvrdi da je neki problem NP - kompletan samo ako se za neki drugi problem veće zna da je NP -kompletan). Cook je godine dokazao (neposredno, koristeći formalizam Tjuringove mašine) da je SAT problem N P -kompletan. U godinama koje su sledile za mnoge probleme je utvrdjeno da su takodje N P -kompletni (najčešće svodjenjem SAT problema na njih), ali ni za jedan od njih nije pokazano da pripada klasi P, pa se još uvek ne zna da li važi P = NP (mada se veruje da ne važi). P SP ACE je klasa problema koji mogu biti rešeni korišćenjem memorijskog prostora koji je polinomijalna funkcija ulaza. Važi NP P SP ACE, ali se ne zna da li važi NP = P SP ACE (rasprostranjeno je uverenje da ne važi). Tokom poslednje decenije, pored napora da se dokaže da ne važi P = NP, radi se i na ispitivanju raspodela najtežih problema u pojedinim klasama NP - kompletnih problema. Dosta se radi i na primeni novih pristupa (npr. na primeni genetskih algoritama) u efikasnijem rešavanju nekih problema u pojedinim klasama N P -kompletnih problema. 2 Clay Institute for Mathematical Sciences is offering a one million dollar prize for a complete polynomial-time SAT solver or a proof that such an algorithm does not exist (the P vs NP problem).

Programiranje II Beleške za predavanja

Programiranje II Beleške za predavanja Programiranje II Beleške za predavanja Smer Informatika Matematički fakultet, Beograd Filip Marić i Predrag Janičić 2011. 2 Sadržaj I Osnovi algoritmike 7 1 Rekurzija 9 1.1 Primeri rekurzivnih funkcija.....................

More information

Programiranje III razred

Programiranje III razred Tehnička škola 9. maj Bačka Palanka Programiranje III razred Naredbe ciklusa for petlja Naredbe ciklusa Veoma često se ukazuje potreba za ponavljanjem nekih naredbi više puta tj. za ponavljanjem nekog

More information

b) program deljiv3; uses wincrt; var i:integer; begin i:=3; while i<100 do begin write(i:5); i:=i+3; end; end.

b) program deljiv3; uses wincrt; var i:integer; begin i:=3; while i<100 do begin write(i:5); i:=i+3; end; end. NAREDBA CIKLUSA SA PREDUSLOVOM WHILE 1.Odrediti vrednosti s i p nakon izvrsenja sledecih naredbi za dato a=43, a=34, a=105 program p1; var a,s,p:integer; write('unesite a:');readln(a); p:=a; s:=0; while

More information

Osnove programskog jezika C# Čas 5. Delegati, događaji i interfejsi

Osnove programskog jezika C# Čas 5. Delegati, događaji i interfejsi Osnove programskog jezika C# Čas 5. Delegati, događaji i interfejsi DELEGATI Bezbedni pokazivači na funkcije Jer garantuju vrednost deklarisanog tipa. Prevodilac prijavljuje grešku ako pokušate da povežete

More information

Uvod u programiranje - vežbe. Kontrola toka izvršavanja programa

Uvod u programiranje - vežbe. Kontrola toka izvršavanja programa Uvod u programiranje - vežbe Kontrola toka izvršavanja programa Naredbe za kontrolu toka if, if-else, switch uslovni operator (?:) for, while, do-while break, continue, return if if (uslov) naredba; if

More information

... ; ako je a n parno. ; ako je a n neparno

... ; ako je a n parno. ; ako je a n neparno Zadaci vezani za ciklus sa preduslovom (WHILE) Zad. Napisati program za izračunavanje n_tog stepena broja a. Zad2. Napisati program za izračunavanje sume S kvadrata parnih i kubova neparnih prirodnih brojeva

More information

pojedinačnom elementu niza se pristupa imeniza[indeks] indeks od 0 do n-1

pojedinačnom elementu niza se pristupa imeniza[indeks] indeks od 0 do n-1 NIZOVI Niz deklarišemo navođenjemtipa elemenata za kojim sledi par srednjih zagrada[] i naziv niza. Ako je niz višedimenzionalni između zagrada[] se navode zarezi, čiji je broj za jedan manji od dimenzija

More information

PREDMET. Osnove Java Programiranja. Čas JAVADOC

PREDMET. Osnove Java Programiranja. Čas JAVADOC PREDMET Osnove Java Programiranja JAVADOC Copyright 2010 UNIVERZITET METROPOLITAN, Beograd. Sva prava zadržana. Bez prethodne pismene dozvole od strane Univerziteta METROPOLITAN zabranjena je reprodukcija,

More information

Osnove programskog jezika C# Čas 4. Nasledjivanje 2. deo

Osnove programskog jezika C# Čas 4. Nasledjivanje 2. deo Osnove programskog jezika C# Čas 4. Nasledjivanje 2. deo Nasledjivanje klasa Modifikator new class A { public virtual void F() { Console.WriteLine("I am A"); } } class B : A { public override void F()

More information

COSC345 Software Engineering. The Heap And Dynamic Memory Allocation

COSC345 Software Engineering. The Heap And Dynamic Memory Allocation COSC345 Software Engineering The Heap And Dynamic Memory Allocation Outline Revision The programmer s view of memory Simple array-based memory allocation C memory allocation routines Virtual memory Swapping

More information

Rekurzivne metode. Posmatrajmo rekurzivan metod kojim u objektu listbox1 klase ListBox upisujemo sve prirodne brojeve od 1 do datog n.

Rekurzivne metode. Posmatrajmo rekurzivan metod kojim u objektu listbox1 klase ListBox upisujemo sve prirodne brojeve od 1 do datog n. Rekurzivne metode Rekurzivan metod je onaj metod koji u nekoj svojoj instrukciji sadrži poziv samog sebe. Svakako prilikom kreiranja rekurzivnog metoda moramo voditi računa da ne dodje do beskonačne rekurzije

More information

Understanding Pointers

Understanding Pointers Division of Mathematics and Computer Science Maryville College Pointers and Addresses Memory is organized into a big array. Every data item occupies one or more cells. A pointer stores an address. A pointer

More information

Algoritmi i strukture podataka

Algoritmi i strukture podataka Algoritmi i strukture podataka vežbe 7 Mirko Stojadinović 20. decembar 2015 1 1 Kviksort Složenost ovog algoritma je u najgorem sluǎju O(n 2 ) (kada se za pivot bira uvek najmanji element što je slučaj

More information

x y = z Zadaci - procedure

x y = z Zadaci - procedure Zadaci - procedure Zad1. Data je kvadratna meta u koordinatnom sistemu sa koordinatama A(0,0), B(1,0), C(1,1), D(0,1). Sastaviti proceduru Gadjanje koja će odrediti broj poena na sledeći način: ako je

More information

COSC Software Engineering. Lectures 14 and 15: The Heap and Dynamic Memory Allocation

COSC Software Engineering. Lectures 14 and 15: The Heap and Dynamic Memory Allocation COSC345 2013 Software Engineering Lectures 14 and 15: The Heap and Dynamic Memory Allocation Outline Revision The programmer s view of memory Simple array-based memory allocation C memory allocation routines

More information

Uputa: Zabranjeno je koristiti bilo kakva pomagala. Rje²enja pi²ete desno od zadatka. Predajete samo ovaj list.

Uputa: Zabranjeno je koristiti bilo kakva pomagala. Rje²enja pi²ete desno od zadatka. Predajete samo ovaj list. Ime i prezime: Asistent: Predava : Programiranje (C) 1. kolokvij 14. 4. 2003. 1. 2. 3. 4. 5. 6. 7. Uputa: Zabranjeno je koristiti bilo kakva pomagala. Rje²enja pi²ete desno od zadatka. Predajete samo ovaj

More information

Računarske osnove Interneta (SI3ROI, IR4ROI)

Računarske osnove Interneta (SI3ROI, IR4ROI) Računarske osnove terneta (SI3ROI, IR4ROI) Vežbe MPLS Predavač: 08.11.2011. Dražen Drašković, drazen.draskovic@etf.rs Autori: Dražen Drašković Naučili ste na predavanjima MPLS (Multi-Protocol Label Switching)

More information

Lectures 13 & 14. memory management

Lectures 13 & 14. memory management Lectures 13 & 14 Linked lists and memory management Courtesy of Prof. Garcia (UCB) CS61C L05 Introduction to C (pt 3) (1) Review Pointers and arrays are virtually same C knows how to increment pointers

More information

UNIVERZITET U BEOGRADU ELEKTROTEHNIČKI FAKULTET

UNIVERZITET U BEOGRADU ELEKTROTEHNIČKI FAKULTET UNIVERZITET U BEOGRADU ELEKTROTEHNIČKI FAKULTET Katedra za elektroniku Računarska elektronika Grupa br. 11 Projekat br. 8 Studenti: Stefan Vukašinović 466/2013 Jelena Urošević 99/2013 Tekst projekta :

More information

Svi Java tipovi imaju ekvivalentan tip u jeziku Scala Većina Scala koda se direktno preslikava u odgovarajući Java konstrukt

Svi Java tipovi imaju ekvivalentan tip u jeziku Scala Većina Scala koda se direktno preslikava u odgovarajući Java konstrukt Funkcionalno programiranje Interoperabilnost jezika Scala i Java Prevođenje u Java bajt kod Svi Java tipovi imaju ekvivalentan tip u jeziku Scala Većina Scala koda se direktno preslikava u odgovarajući

More information

Mašinska vizija. Dr Nenad Jovičić tnt.etf.rs/~mv

Mašinska vizija. Dr Nenad Jovičić tnt.etf.rs/~mv Mašinska vizija Dr Nenad Jovičić 2017. tnt.etf.rs/~mv Linearne 2D geometrijske transformacije 2D geometrijske transformacije Pretpostavka: Objekti u 2D prostoru se sastoje iz tačaka i linija. Svaka tačka

More information

Veliki računski zadaci mogu se razbiti u manje delove i time se omogućava ljudima da iskoriste ono što su neki drugi već uradili, umesto da počinju

Veliki računski zadaci mogu se razbiti u manje delove i time se omogućava ljudima da iskoriste ono što su neki drugi već uradili, umesto da počinju Staša Vujičić Čas 9 Veliki računski zadaci mogu se razbiti u manje delove i time se omogućava ljudima da iskoriste ono što su neki drugi već uradili, umesto da počinju sve od početka. Odgovarajuće funkcije

More information

CS 11 C track: lecture 5

CS 11 C track: lecture 5 CS 11 C track: lecture 5 Last week: pointers This week: Pointer arithmetic Arrays and pointers Dynamic memory allocation The stack and the heap Pointers (from last week) Address: location where data stored

More information

KLASIFIKACIJA JELENA JOVANOVIĆ. Web:

KLASIFIKACIJA JELENA JOVANOVIĆ.   Web: KLASIFIKACIJA JELENA JOVANOVIĆ Email: jeljov@gmail.com Web: http://jelenajovanovic.net PREGLED PREDAVANJA Šta je klasifikacija? Binarna i više-klasna klasifikacija Algoritmi klasifikacije Mere uspešnosti

More information

CS61C : Machine Structures

CS61C : Machine Structures inst.eecs.berkeley.edu/~cs61c/su06 CS61C : Machine Structures Lecture #6: Memory Management CS 61C L06 Memory Management (1) 2006-07-05 Andy Carle Memory Management (1/2) Variable declaration allocates

More information

For. 1) program ispis; {ispisuje brojeve od 1 do 5 jedan ispod drugog} uses wincrt; var s,i:integer; begin for i:=1 to 5do writeln(i); end.

For. 1) program ispis; {ispisuje brojeve od 1 do 5 jedan ispod drugog} uses wincrt; var s,i:integer; begin for i:=1 to 5do writeln(i); end. For 1) program ispis; {ispisuje brojeve od 1 do 5 jedan ispod drugog} for i:=1 to 5do writeln(i); 2) program ispis; {ispisuje brojeve od 5 do 1 jedan ispod drugog} for i:=5 downto 1 do writeln(i); 3) program

More information

Programs in memory. The layout of memory is roughly:

Programs in memory. The layout of memory is roughly: Memory 1 Programs in memory 2 The layout of memory is roughly: Virtual memory means that memory is allocated in pages or segments, accessed as if adjacent - the platform looks after this, so your program

More information

Učitati cio broj n i štampati njegovu recipročnu vrijednost. Ako je učitan broj 0, štampati 1/0.

Učitati cio broj n i štampati njegovu recipročnu vrijednost. Ako je učitan broj 0, štampati 1/0. Kontrolne naredbe Primjeri: Opšti oblik razgranate strukture (if sa ) if (uslov) Naredba 1 ili blok naredbi1 Naredba 2 ili blok naredbi2 Učitati broj x i štampati vrijednost double x, z; Scanner in=new

More information

Lecture 8 Dynamic Memory Allocation

Lecture 8 Dynamic Memory Allocation Lecture 8 Dynamic Memory Allocation CS240 1 Memory Computer programs manipulate an abstraction of the computer s memory subsystem Memory: on the hardware side 3 @ http://computer.howstuffworks.com/computer-memory.htm/printable

More information

Limitations of the stack

Limitations of the stack The heap hic 1 Limitations of the stack int *table_of(int num, int len) { int table[len+1]; for (int i=0; i

More information

1 Dynamic Memory continued: Memory Leaks

1 Dynamic Memory continued: Memory Leaks CS104: Data Structures and Object-Oriented Design (Fall 2013) September 3, 2013: Dynamic Memory, continued; A Refresher on Recursion Scribes: CS 104 Teaching Team Lecture Summary In this lecture, we continue

More information

CSS CSS. selector { property: value; } 3/20/2018. CSS: Cascading Style Sheets

CSS CSS. selector { property: value; } 3/20/2018. CSS: Cascading Style Sheets CSS CSS CSS: Cascading Style Sheets - Opisuje izgled (appearance) i raspored (layout) stranice - Sastoji se od CSS pravila, koji defini[u skup stilova selector { property: value; 1 Font face: font-family

More information

Programming. Pointers, Multi-dimensional Arrays and Memory Management

Programming. Pointers, Multi-dimensional Arrays and Memory Management Programming Pointers, Multi-dimensional Arrays and Memory Management Summary } Computer Memory } Pointers } Declaration, assignment, arithmetic and operators } Casting and printing pointers } Relationship

More information

CS 31: Intro to Systems Pointers and Memory. Kevin Webb Swarthmore College October 2, 2018

CS 31: Intro to Systems Pointers and Memory. Kevin Webb Swarthmore College October 2, 2018 CS 31: Intro to Systems Pointers and Memory Kevin Webb Swarthmore College October 2, 2018 Overview How to reference the location of a variable in memory Where variables are placed in memory How to make

More information

2. Linijska algoritamska struktura

2. Linijska algoritamska struktura Univerzitet u Nišu Građevinsko-arhitektonski fakultet Informatika 2 2. Linijska algoritamska struktura Milica Ćirić Blokovi za prikaz algoritma Algoritam se vizuelno može prikazati pomoću blok dijagrama,

More information

Heap Management. Heap Allocation

Heap Management. Heap Allocation Heap Management Heap Allocation A very flexible storage allocation mechanism is heap allocation. Any number of data objects can be allocated and freed in a memory pool, called a heap. Heap allocation is

More information

CS61C : Machine Structures

CS61C : Machine Structures inst.eecs.berkeley.edu/~cs61c CS61C : Machine Structures Lecture #4 C Memory Management 2007-06-28 Scott Beamer, Instructor iphone Comes out Tomorrow CS61C L4 C Memory Management (1) www.apple.com/iphone

More information

VHDLPrimeri Poglavlje5.doc

VHDLPrimeri Poglavlje5.doc 5. VHDL opis kola koja obavljaju osnovne aritmetičke funkcije Sabirači Jednobitni potpuni sabirač definisan je tablicom istinitosti iz Tabele 5.1. Tabela 5.1. cin a b sum cout 0 0 0 0 0 0 0 1 1 0 0 1 0

More information

In Java we have the keyword null, which is the value of an uninitialized reference type

In Java we have the keyword null, which is the value of an uninitialized reference type + More on Pointers + Null pointers In Java we have the keyword null, which is the value of an uninitialized reference type In C we sometimes use NULL, but its just a macro for the integer 0 Pointers are

More information

Memory Allocation. Static Allocation. Dynamic Allocation. Dynamic Storage Allocation. CS 414: Operating Systems Spring 2008

Memory Allocation. Static Allocation. Dynamic Allocation. Dynamic Storage Allocation. CS 414: Operating Systems Spring 2008 Dynamic Storage Allocation CS 44: Operating Systems Spring 2 Memory Allocation Static Allocation (fixed in size) Sometimes we create data structures that are fixed and don t need to grow or shrink. Dynamic

More information

CS 536 Introduction to Programming Languages and Compilers Charles N. Fischer Lecture 11

CS 536 Introduction to Programming Languages and Compilers Charles N. Fischer Lecture 11 CS 536 Introduction to Programming Languages and Compilers Charles N. Fischer Lecture 11 CS 536 Spring 2015 1 Handling Overloaded Declarations Two approaches are popular: 1. Create a single symbol table

More information

o Code, executable, and process o Main memory vs. virtual memory

o Code, executable, and process o Main memory vs. virtual memory Goals for Today s Lecture Memory Allocation Prof. David August COS 217 Behind the scenes of running a program o Code, executable, and process o Main memory vs. virtual memory Memory layout for UNIX processes,

More information

Programske paradigme Funkcionalna paradigma

Programske paradigme Funkcionalna paradigma Programske paradigme Funkcionalna paradigma 1. čas: Uvod u funkcionalno programiranje. Programski jezik Haskel. Upoznavanje sa razvojnim okruženjem. Tipovi podataka. Funkcionalno programiranje Stil u programiranju

More information

Data Structure Series

Data Structure Series Data Structure Series This series is actually something I started back when I was part of the Sweet.Oblivion staff, but then some things happened and I was no longer able to complete it. So now, after

More information

VRIJEDNOSTI ATRIBUTA

VRIJEDNOSTI ATRIBUTA VRIJEDNOSTI ATRIBUTA Svaki atribut (bilo da je primarni ključ, vanjski ključ ili običan atribut) može i ne mora imati ograničenja na svojim vrijednostima. Neka od ograničenja nad atributima: Null / Not

More information

Dynamic Memory Allocation (and Multi-Dimensional Arrays)

Dynamic Memory Allocation (and Multi-Dimensional Arrays) Dynamic Memory Allocation (and Multi-Dimensional Arrays) Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2 nd edition, by Kernighan

More information

Arrays and Memory Management

Arrays and Memory Management Arrays and Memory Management 1 Pointing to Different Size Objects Modern machines are byte-addressable Hardware s memory composed of 8-bit storage cells, each has a unique address A C pointer is just abstracted

More information

Last week. Data on the stack is allocated automatically when we do a function call, and removed when we return

Last week. Data on the stack is allocated automatically when we do a function call, and removed when we return Last week Data can be allocated on the stack or on the heap (aka dynamic memory) Data on the stack is allocated automatically when we do a function call, and removed when we return f() {... int table[len];...

More information

Static Program Analysis Part 1 the TIP language

Static Program Analysis Part 1 the TIP language Static Program Analysis Part 1 the TIP language http://cs.au.dk/~amoeller/spa/ Anders Møller & Michael I. Schwartzbach Computer Science, Aarhus University Questions about programs Does the program terminate

More information

namespace spojneice { public partial class Form1 : Form { public Form1() { InitializeComponent(); }

namespace spojneice { public partial class Form1 : Form { public Form1() { InitializeComponent(); } Spojnice using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.IO;

More information

Lecture Notes on Memory Layout

Lecture Notes on Memory Layout Lecture Notes on Memory Layout 15-122: Principles of Imperative Computation Frank Pfenning André Platzer Lecture 11 1 Introduction In order to understand how programs work, we can consider the functions,

More information

for i:=2 to n do if glasovi[i]>max then begin max:=glasovi[i]; k:=i {*promenljiva k ce cuvati indeks takmicara sa najvise glasova *} end;

for i:=2 to n do if glasovi[i]>max then begin max:=glasovi[i]; k:=i {*promenljiva k ce cuvati indeks takmicara sa najvise glasova *} end; {*Na Evroviziji je ucestvovalo n izvodjaca. Koji od njih je osvojio najvise glasova publike?*} program Evrovizija; glasovi:array[1..50] of integer; max,k:integer; writeln('unosi se broj izvodjaca:'); writeln('unose

More information

Dynamically Allocated Memory in C

Dynamically Allocated Memory in C Dynamically Allocated Memory in C All throughout the C course (COP 3223), all examples of variable declarations were statically allocated memory. The word static means not changing while the word dynamic

More information

C Structures & Dynamic Memory Management

C Structures & Dynamic Memory Management C Structures & Dynamic Memory Management Goals of this Lecture Help you learn about: Structures and unions Dynamic memory management Note: Will be covered in precepts as well We look at them in more detail

More information

Memory Allocation in C C Programming and Software Tools. N.C. State Department of Computer Science

Memory Allocation in C C Programming and Software Tools. N.C. State Department of Computer Science Memory Allocation in C C Programming and Software Tools N.C. State Department of Computer Science The Easy Way Java (JVM) automatically allocates and reclaims memory for you, e.g... Removed object is implicitly

More information

modifier returnvaluetype methodname(list of parameters) { // Method body; }

modifier returnvaluetype methodname(list of parameters) { // Method body; } Početna grupa, 28.11.2015. Metodi 1. Metodi opšti oblik metoda: modifier returnvaluetype methodname(list of parameters) // Method body; 2. Ime metoda: početno slovo je malo, a zatim slijede slova, cifre

More information

Binarne hrpe. Strukture podataka i algoritmi VJEŽBE 26. siječnja / 133

Binarne hrpe. Strukture podataka i algoritmi VJEŽBE 26. siječnja / 133 Binarne hrpe Potpuno binarno stablo binarno stablo u kojem svaki čvor koji nije list ima točno 2 nasljednika. Binarna hrpa potpuno binarno stablo u kojem svaki čvor koji nije list ima veću ključnu vrijednost

More information

Reminder of midterm 1. Next Thursday, Feb. 14, in class Look at Piazza announcement for rules and preparations

Reminder of midterm 1. Next Thursday, Feb. 14, in class Look at Piazza announcement for rules and preparations Reminder of midterm 1 Next Thursday, Feb. 14, in class Look at Piazza announcement for rules and preparations A New Example to see effect of E++ (better than the one in previous lecture) Purpose of showing

More information

Programiranje 1. Školska 2006/2007 godina. Matematički fakultet, Beograd

Programiranje 1. Školska 2006/2007 godina. Matematički fakultet, Beograd Programiranje 1 Beleške sa vežbi Školska 2006/2007 godina Matematički fakultet, Beograd Jelena Tomašević December 12, 2006 2 Sadržaj 1 Programski jezik C 5 1.1 Nizovi osnovni pojmovi................................

More information

Uputstvo za korišćenje logrotate funkcije

Uputstvo za korišćenje logrotate funkcije Copyright AMRES Sadržaj Uvod 3 Podešavanja logrotate konfiguracionog fajla 4 Strana 2 od 5 Uvod Ukoliko je aktivirano logovanje za RADIUS proces, može se desiti da posle određenog vremena server bude preopterećen

More information

CS 61C: Great Ideas in Computer Architecture C Memory Management, Usage Models

CS 61C: Great Ideas in Computer Architecture C Memory Management, Usage Models CS 61C: Great Ideas in Computer Architecture C Memory Management, Usage Models Instructors: Nicholas Weaver & Vladimir Stojanovic http://inst.eecs.berkeley.edu/~cs61c/sp16 1 Pointer Ninjitsu: Pointers

More information

Uvod u relacione baze podataka

Uvod u relacione baze podataka Uvod u relacione baze podataka Ana Spasić 5. čas 1 Podupiti, operatori exists i in 1. Izdvojiti imena i prezimena studenata koji su položili predmet čiji je identifikator 2001. Rešenje korišćenjem spajanja

More information

Deallocation Mechanisms. User-controlled Deallocation. Automatic Garbage Collection

Deallocation Mechanisms. User-controlled Deallocation. Automatic Garbage Collection Deallocation Mechanisms User-controlled Deallocation Allocating heap space is fairly easy. But how do we deallocate heap memory no longer in use? Sometimes we may never need to deallocate! If heaps objects

More information

Vežbe - XII nedelja PHP Doc

Vežbe - XII nedelja PHP Doc Vežbe - XII nedelja PHP Doc Dražen Drašković, asistent Elektrotehnički fakultet Univerziteta u Beogradu Verzija alata JavaDoc za programski jezik PHP Standard za komentarisanje PHP koda Omogućava generisanje

More information

15 Sharing Main Memory Segmentation and Paging

15 Sharing Main Memory Segmentation and Paging Operating Systems 58 15 Sharing Main Memory Segmentation and Paging Readings for this topic: Anderson/Dahlin Chapter 8 9; Siberschatz/Galvin Chapter 8 9 Simple uniprogramming with a single segment per

More information

Programiranje Programski jezik C. Sadržaj. Datoteke. prof.dr.sc. Ivo Ipšić 2009/2010

Programiranje Programski jezik C. Sadržaj. Datoteke. prof.dr.sc. Ivo Ipšić 2009/2010 Programiranje Programski jezik C prof.dr.sc. Ivo Ipšić 2009/2010 Sadržaj Ulazno-izlazne funkcije Datoteke Formatirane datoteke Funkcije za rad s datotekama Primjeri Datoteke komunikacija između programa

More information

VEŽBA 5 do while petlja, switch case

VEŽBA 5 do while petlja, switch case VEŽBA do while petlja, switch case Petlja sa ulaznim uslovom do while U slučaju do while petlje obavezno izvršavanje bar jedne iteracije se postiže tako što je upravljački izraz petlje na samom dnu petlje.

More information

My malloc: mylloc and mhysa. Johan Montelius HT2016

My malloc: mylloc and mhysa. Johan Montelius HT2016 1 Introduction My malloc: mylloc and mhysa Johan Montelius HT2016 So this is an experiment where we will implement our own malloc. We will not implement the world s fastest allocator, but it will work

More information

CS 310 Advanced Data Structures and Algorithms

CS 310 Advanced Data Structures and Algorithms CS 310 Advanced Data Structures and Algorithms Recursion June 27, 2017 Tong Wang UMass Boston CS 310 June 27, 2017 1 / 20 Recursion Recursion means defining something, such as a function, in terms of itself

More information

Algorithms in Systems Engineering IE172. Midterm Review. Dr. Ted Ralphs

Algorithms in Systems Engineering IE172. Midterm Review. Dr. Ted Ralphs Algorithms in Systems Engineering IE172 Midterm Review Dr. Ted Ralphs IE172 Midterm Review 1 Textbook Sections Covered on Midterm Chapters 1-5 IE172 Review: Algorithms and Programming 2 Introduction to

More information

CS 241 Honors Memory

CS 241 Honors Memory CS 241 Honors Memory Ben Kurtovic Atul Sandur Bhuvan Venkatesh Brian Zhou Kevin Hong University of Illinois Urbana Champaign February 20, 2018 CS 241 Course Staff (UIUC) Memory February 20, 2018 1 / 35

More information

Dynamic Allocation in C

Dynamic Allocation in C Dynamic Allocation in C C Pointers and Arrays 1 The previous examples involved only targets that were declared as local variables. For serious development, we must also be able to create variables dynamically,

More information

G Programming Languages - Fall 2012

G Programming Languages - Fall 2012 G22.2110-003 Programming Languages - Fall 2012 Lecture 2 Thomas Wies New York University Review Last week Programming Languages Overview Syntax and Semantics Grammars and Regular Expressions High-level

More information

Vidljivost TipPovratneVrednosti ImeFunkcije (NizParametara) { TeloFunkcije }

Vidljivost TipPovratneVrednosti ImeFunkcije (NizParametara) { TeloFunkcije } 1. FUNKCIJE I STRUKTRUE PROGRAMA Složeni problemi lakše se rašavaju ako se podele na manje celine koje mogu nezavisno da se rešavaju. Rešenje celokupnog složenog problema dobija se kombinovanjem rešenja

More information

Univerzitet u Nišu Građevinsko-arhitektonski fakultet. 4. Ciklična algoritamska struktura 5. Jednodimenzionalno polje.

Univerzitet u Nišu Građevinsko-arhitektonski fakultet. 4. Ciklična algoritamska struktura 5. Jednodimenzionalno polje. Univerzitet u Nišu Građevinsko-arhitektonski fakultet Informatika 2 4. Ciklična algoritamska struktura 5. Jednodimenzionalno polje Milica Ćirić Ciklična algoritamska struktura Ciklična struktura (petlja)

More information

CS61C : Machine Structures

CS61C : Machine Structures inst.eecs.berkeley.edu/~cs61c CS61C : Machine Structures Lecture 5 C Memory Management 2010-06-28!!! Instructor Paul Pearce! Symmetric multiprocessor! MIPS support for Android MIPS Technologies (founded

More information

I PISMENI ZADATAK III6 I GRUPA IME I PREZIME

I PISMENI ZADATAK III6 I GRUPA IME I PREZIME I PISMENI ZADATAK III6 I GRUPA IME I PREZIME 1.1.Pronaci najveći i najmanji element unete matrice dimenzija n x m i mesto na kome se nalaze. Korististi 2.1. Na osnovu unete matrice A (nxn) celih brojeva

More information

Discussion on Writing of Recursive Algorithm

Discussion on Writing of Recursive Algorithm , pp.127-134 http://dx.doi.org/10.14257/ijhit.2013.6.6.11 Discussion on Writing of Recursive Algorithm Song Jinping Computer Department, Jining Teachers College, Wulanchabu, China jnsongjinping@126.com

More information

Numerical Computation

Numerical Computation GNU Octave Numerical Computation vrlo često u tehnici retko stvarni problemi imaju closed-form solution čak i kad imaju, pitanje upotrebljivosti mnogo detalja numerički pristup u početku tretirano kao

More information

Lecture Notes 4 More C++ and recursion CSS 501 Data Structures and Object-Oriented Programming Professor Clark F. Olson

Lecture Notes 4 More C++ and recursion CSS 501 Data Structures and Object-Oriented Programming Professor Clark F. Olson Lecture Notes 4 More C++ and recursion CSS 501 Data Structures and Object-Oriented Programming Professor Clark F. Olson Reading for this lecture: Carrano, Chapter 2 Copy constructor, destructor, operator=

More information

Recursion & Performance. Recursion. Recursion. Recursion. Where Recursion Shines. Breaking a Problem Down

Recursion & Performance. Recursion. Recursion. Recursion. Where Recursion Shines. Breaking a Problem Down Recursion & Performance Recursion Part 7 The best way to learn recursion is to, first, learn recursion! Recursion Recursion Recursion occurs when a function directly or indirectly calls itself This results

More information

Q1: /8 Q2: /30 Q3: /30 Q4: /32. Total: /100

Q1: /8 Q2: /30 Q3: /30 Q4: /32. Total: /100 ECE 2035(A) Programming for Hardware/Software Systems Fall 2013 Exam Three November 20 th 2013 Name: Q1: /8 Q2: /30 Q3: /30 Q4: /32 Total: /100 1/10 For functional call related questions, let s assume

More information

Dynamic memory allocation

Dynamic memory allocation Dynamic memory allocation outline Memory allocation functions Array allocation Matrix allocation Examples Memory allocation functions (#include ) malloc() Allocates a specified number of bytes

More information

Actually, C provides another type of variable which allows us to do just that. These are called dynamic variables.

Actually, C provides another type of variable which allows us to do just that. These are called dynamic variables. When a program is run, memory space is immediately reserved for the variables defined in the program. This memory space is kept by the variables until the program terminates. These variables are called

More information

Short Notes of CS201

Short Notes of CS201 #includes: Short Notes of CS201 The #include directive instructs the preprocessor to read and include a file into a source code file. The file name is typically enclosed with < and > if the file is a system

More information

Dynamic Memory Allocation

Dynamic Memory Allocation Dynamic Memory Allocation CS61, Lecture 10 Prof. Stephen Chong October 4, 2011 Announcements 1/2 Assignment 4: Malloc Will be released today May work in groups of one or two Please go to website and enter

More information

Review! Lecture 5 C Memory Management !

Review! Lecture 5 C Memory Management ! CS61C L05 C Memory Management (1)! inst.eecs.berkeley.edu/~cs61c CS61C : Machine Structures Lecture 5 C Memory Management 2010-06-28!!! Instructor Paul Pearce! Symmetric multiprocessor! MIPS support for

More information

PROGRAMIRANJE. Amir Hajdar

PROGRAMIRANJE. Amir Hajdar PROGRAMIRANJE Amir Hajdar Teme 2 Klase i objekti u Javi Primjer kroz klasu Krug Atributi i metode Inicijalizacija objekata (konstruktori) Polymorphism Statičke varijable i metode This Klase i objekti u

More information

CS201 - Introduction to Programming Glossary By

CS201 - Introduction to Programming Glossary By CS201 - Introduction to Programming Glossary By #include : The #include directive instructs the preprocessor to read and include a file into a source code file. The file name is typically enclosed with

More information

16 Sharing Main Memory Segmentation and Paging

16 Sharing Main Memory Segmentation and Paging Operating Systems 64 16 Sharing Main Memory Segmentation and Paging Readings for this topic: Anderson/Dahlin Chapter 8 9; Siberschatz/Galvin Chapter 8 9 Simple uniprogramming with a single segment per

More information

Run-Time Environments

Run-Time Environments CS308 Run-Time Environments Li Jiang Department of Computer Science and Engineering Shanghai Jiao Tong University Current Progress Source Language Lexical Analyzer Syntax Analyzer Semantic Analyzer Intermediate

More information

Agenda. Peer Instruction Question 1. Peer Instruction Answer 1. Peer Instruction Question 2 6/22/2011

Agenda. Peer Instruction Question 1. Peer Instruction Answer 1. Peer Instruction Question 2 6/22/2011 CS 61C: Great Ideas in Computer Architecture (Machine Structures) Introduction to C (Part II) Instructors: Randy H. Katz David A. Patterson http://inst.eecs.berkeley.edu/~cs61c/sp11 Spring 2011 -- Lecture

More information

CSCI 171 Chapter Outlines

CSCI 171 Chapter Outlines Contents CSCI 171 Chapter 1 Overview... 2 CSCI 171 Chapter 2 Programming Components... 3 CSCI 171 Chapter 3 (Sections 1 4) Selection Structures... 5 CSCI 171 Chapter 3 (Sections 5 & 6) Iteration Structures

More information

PITANJA ZA II KOLOKVIJUM KLASE I OBJEKTI

PITANJA ZA II KOLOKVIJUM KLASE I OBJEKTI PITANJA ZA II KOLOKVIJUM KLASE I OBJEKTI 1. Enkapsulacija je podataka. skrivanje apstrakcija nasledivanje 2. Unutar deklaracije klase navode se: definicije funkcija clanica prototipovi (deklaracije) funkcija

More information

Memory Management. CSC215 Lecture

Memory Management. CSC215 Lecture Memory Management CSC215 Lecture Outline Static vs Dynamic Allocation Dynamic allocation functions malloc, realloc, calloc, free Implementation Common errors Static Allocation Allocation of memory at compile-time

More information

CSC 1600 Memory Layout for Unix Processes"

CSC 1600 Memory Layout for Unix Processes CSC 16 Memory Layout for Unix Processes" 1 Lecture Goals" Behind the scenes of running a program" Code, executable, and process" Memory layout for UNIX processes, and relationship to C" : code and constant

More information

CS61C : Machine Structures

CS61C : Machine Structures inst.eecs.berkeley.edu/~cs61c CS61C : Machine Structures Lecture 4 Introduction to C (pt 2) 2014-09-08!!!Senior Lecturer SOE Dan Garcia!!!www.cs.berkeley.edu/~ddgarcia! C most popular! TIOBE programming

More information

Memory Management. CS449 Fall 2017

Memory Management. CS449 Fall 2017 Memory Management CS449 Fall 2017 Life9mes Life9me: 9me from which a par9cular memory loca9on is allocated un9l it is deallocated Three types of life9mes Automa9c (within a scope) Sta9c (dura9on of program)

More information

Reduction & Recursion Overview

Reduction & Recursion Overview Reduction & Recursion Overview Reduction definition Reduction techniques Recursion definition Recursive thinking (Many) recursion examples Indirect recursion Runtime stack Factorial isnumericstring add

More information

CSE 143. Complexity Analysis. Program Efficiency. Constant Time Statements. Big Oh notation. Analyzing Loops. Constant Time Statements (2) CSE 143 1

CSE 143. Complexity Analysis. Program Efficiency. Constant Time Statements. Big Oh notation. Analyzing Loops. Constant Time Statements (2) CSE 143 1 CSE 1 Complexity Analysis Program Efficiency [Sections 12.1-12., 12., 12.9] Count number of instructions executed by program on inputs of a given size Express run time as a function of the input size Assume

More information