Carleton University Department of Systems and Computer Engineering SYSC 2006 - Foundations of Imperative Programming - Winter 2012 Extra Programming Exercises These exercises review almost every concept covered during the term, except for recursive functions. During Labs 5 and 6, you prototyped some functions for a list collection. Later in the term, we looked at two approaches to implementing the list's data structure using a C struct . In the first implementation, the list's capacity was fixed at compile-time, In the second implementation, the list's capacity was specified at run-time. Exercise 1 • Open project intlist_v3 . The function prototypes in intlist_v3.h and function definitions in intlist_v3.c were presented in one of the lectures. In addition, the project contains a program (see test_list.c ) that tests these functions. • Read the header comments for intlist_get and intlist_set , and finish the implementations of these functions. Modify test_list.c to test these functions. • In intlist_v3.h , uncomment the function prototypes for intlist_index , intlist_count and intlist_contains . In intlist_v3.c , write the implementations of these functions.You implemented these functions for Lab 5, and you should be able to reuse much of this code. (The biggest change is that, instead of passing an array and its length (size) as function arguments, you are now passing a pointer to an IntList structure.) Modify test_list.c to test these functions.You should be able to reuse much of the test code you wrote for Lab 5. Run the tests, and make any necessary corrections. • In intlist_v3.h , uncomment the function prototypes for intlist_delete and intlist_remove . In intlist_v3.c , write the implementations of these functions. You implemented these functions for Lab 6, and you should be able to reuse much of this code. Note that the function return types have changed from Lab 6. Previously, the functions returned the size of the modified list, or -1 to indicate that they weren't successful. The return types are now _Bool . The functions should return true if they were successful; otherwise they should return false . Modify test_list.c to test these functions.You should be able to reuse much of the test code you wrote for Lab 6. Run the tests, and make any necessary corrections. 1
Exercise 2 • Assume a function contains the following statement: IntList *list = intlist_construct(); Draw a memory diagram showing the function's stack frame and the heap after this statement is executed. • Assume the function contains the following statements: _Bool appended; for (int i = 0; i < 5; i++) { appended = intlist_append(list, 2 * i); Draw a memory diagram showing the function's stack frame and the heap after this statement is executed. • Assume the function contains the following statement: intlist_removeall(list); Draw a memory diagram showing the function's stack frame and the heap after this statement is executed. • Assume the function contains the following statement: intlist_destroy(list); Draw a memory diagram showing the function's stack frame and the heap after this statement is executed. Exercise 3 • Open project intlist_v4 . The functions in files intlist_v4.h and intlist_v4.c were presented in one of the lectures. • Open intlist_v4.h in the Pelles C editor.In the declaration of struct intlist , member elems is now declared as a "pointer to int ", and a third member (capacity) has been added to the structure. • Open intlist_v4.c . Notice that intlist_construct , intlist_append and intlist_destroy were modified to reflect the changes to the intlist structure. 2
• Copy your intlist_get and intlist_set functions from intlist_v3.c to intlist_v4.c . If necessary, modify these functions to use the revised intlist structure. • In intlist_v4.h , uncomment the function prototypes for intlist_index , intlist_count and intlist_contains . Copy your intlist_index , intlist_count and intlist_contains functions from intlist_v3.c to intlist_v4.c . If necessary, modify these functions to use the revised intlist structure. • In intlist_v4.h , uncomment the function prototypes for intlist_delete and intlist_remove . Copy your intlist_delete and intlist_remove functions from intlist_v3.c to intlist_v4.c . If necessary, modify these functions to use the revised intlist structure. • Copy the test code you wrote for Exercise 1 to test_list.c . The only changes you should have to make is in the calls to intlist_construct (you'll need to specify the list's capacity). Run the tests, and make any necessary corrections. Exercise 4 Repeat Exercise 2, using the list implementation from the intllist_v4 project. Assume that the new list is created this way: IntList *list = intlist_construct(10); 3
Recommend
More recommend