|
How do we link the library in eclipse? |
You need to update the linker settings as following: 1 - Go to: Project->propreties->C/C++ Build ->Setting->Libraries 2 - add the library name "mtm" 3 - add the search path to your current project 4 - click OK
|
How do we link the library in CLion? |
Go to your CMakeLists.txt file, There should be a line "add_executable(PROJECT_NAME ${SOURCE_FILES})" You should add a line after that line that says: "target_link_libraries(PROJECT_NAME ${CMAKE_SOURCE_DIR}/libmtm.a)" (The libmtm.a file should be in the root directory of your project) Please Notice "PROJECT_NAME" is actually whats written in the add_executable line in that place. There is no need for adding the "-L. -lmtm" flags
|
How do we change the compiliation flags in CLion? |
Add a line to the end of the CMakeLists.txt file that says: set(CMAKE_C_FLAGS "-std=c99 -Wall -Werror -pedantic-errors -DNDEBUG")
|
List implementation: What should be the state of the inner iterator after each function: |
1. After creation of a new list (with listCreate function) the list is empty so obviously the iterator should point to NULL. 2. When copying a list (with listCopy) the iterator of the new list should point to the same clone node that the original iterator is pointing to in the original list. 3. After creating a new list with listFilter function, we get a new list that contains elements, therefore we expect the iterator to point to the first element in the filtered list. The state of the original list is not defined after this operation. 4. listGetFirst() - sets the iterator to point to the first element in the list. 5. listGetNext()- sets the iterator to point to the next element from the the one currently pointed. 6. listInsertLast, listInsertFirst, listInsertAfterCurrent, listInsertBeforeCurrent - The iterator state should not be changed. 7. listRemoveCurrent() - the iterator should point to null at the end. 8. listSort() - the iterator will still point to the same node in order: if the list before sorting was: 1, 5, 2, 6. and the iterator pointed to "5". after the sorting: 1, 2, 5, 6. the iterator should still points to the second element in the list and therefore will point to "2".
|
What order should the list sort? |
What order should the list sort? It depends on the comparison function, but here is a code example: if The comperFunction behavior is : return a value greater than 0 if the first element is greater, 0 if they're equal and a number smaller than 0 if the second element is greater.
(Notice: this is opposite then the behavior described in the list.h file )
static ListElement copyString(ListElement str){ assert(str); char* copy = malloc(strlen(str)+1); return copy != NULL ? strcpy(copy, str) : NULL; }
static void freeString(ListElement str){ free(str); }
static int compareString(ListElement str1, ListElement str2){ return strcmp(str1, str2); }
boo testListSort{ char* a[5] = {"aaa","NI","hello mister fish","I", "bbb"}; List list1 = listCreate(copyString,freeString); for (int i=0;i <5; ++i){ listInsertFirst(list1,a[i]); } listSort(list1,compareString); ASSERT_TEST(strcmp(listGetFirst(list1),"I") == 0); ASSERT_TEST(strcmp(listGetNext(list1),"NI") == 0); ASSERT_TEST(strcmp(listGetNext(list1),"aaa") == 0); ASSERT_TEST(strcmp(listGetNext(list1),"bbb") == 0); ASSERT_TEST(strcmp(listGetNext(list1),"hello mister fish") == 0); listDestroy(list1); }
|
when iterating over the list using listGetFirst and listGetNext, do we return a copy to the element or the element itself? |
You should return the element itself.
|
Do we need to use qsort function in order to sort the list? |
Don't use the build in qsort function. The function declaration is:
void qsort(void *base, size_t nitems, size_t size, int (*compar)(const void *, const void*))
this function expects to get as an argument a pointer to a compare function of the type : int cmp(const void* a, const void* b);
while the list is using a pointer to a compare function of the type: int cmp(void* a, void* b);
therefore it will be difficult to pass the compare function that the list holds to the qsort function. (there might be some solutions for that but they are counted as bad coding)
So in order to implement listSort you will have to choose any sorting algorithm that you learned in the introduction to CS course, and implement it. (tutorial 13 page 29 might be useful)
|
Do we need to implement the listSort with or without a key element? |
even though it is better sorting with a key in case that extra data is needed, You MUST NOT implement it with a key, that means that you implement the function from the interface:
ListResult listSort(List list, CompareListElements compareElement);
|
What will happen if a NULL element is given to the listInsert functions? |
We assume that the Element copyFunction will fail in a case that it gets NULL as argument, and from there The insert function should handle the issue.
|
in List Sort function, in a case that for two elements a,b compare(a,b) = 0 ,
what should be the order between the elements? |
if the compare function decides that the two elements are equal (return 0) then we don't care what will be the order between the two elements : Consider a case we have a list of strings, and the compare function key is which string is longer, and lets say the two only elements in the list are "aaa" "bbb" , both have length 3, and compareLength("aaa", "bbb") =0. then this is how a typical test will look like:
listSort(listStrings, compareLength); ASSERT(compareLength("aaa",listGetFirst(listStrings)) == 0); //dont care if "aaa" or "bbb" ASSERT(compareLength("bbb",listGetNext(listStrings)) == 0); //dont care if "aaa" or "bbb" ASSERT(compareLength("Hello World",listGetNext(listStrings)) == 0); ASSERT(listGetNext(listStrings) == NULL);
|
What should be the state of the iterator after listSort? |
consider the following list of intergers:
1 , 6, 3, 7, 4
and lets say the iterator points to the second element : 6.
after sorting the list will be: 1, 3, 4, 6, 7
the iterator will still point to the second node, therefore it hill bow point to : 3
|
What happens if we send key = NULL to the listFilter function? |
You can assume that NULL will not be sent as key to the listFilter function, and therefore you don't need to check this case.
|
The provided set/list crashes on destruction, what should we do? |
The freeFunction you provide it should *not* assert that the parameter is non-null. Just check it with an if statement instead.
|
|