20

This is an example of a recursive call. Even though it is calling itself correctly, it is missing the code necessary to recursively return the result. As it is written above, it will execute the recursive call to itself, but when it does find a number, it will return true on the first step back through the recursion. However, at the second step and ...


6

Your understanding is correct, but your example has a dangerous flaw if you were to implement it. Look back at your question as a whole. You have two important variables, root and newptr. root is the pointer that actually points to, well, the literal root of the trie. It's critical that you never reassign this pointer. If you do, you'll lose the root of the ...


6

Should I declare freenodes in the header file? It is not necessary to declare freenodes in the header file. However because freenodes is written after the function which calls it, it should be declared at the top of the .c file, like so: void freenodes(node* path); This is the same as the function signature used to define the function itself, with the ...


4

Common problem. The compiler looks at all the possible paths through the code and checks to see that there is going to be a guaranteed, unconditional return to satisfy the need for a return value. But, compilers are dumb. They don't recognize that you can create a fully conditional path that must hit a return. In your case, the fix is simple. The final else ...


4

There was a question about this question on facebook, so instead of replying there, I thought I'd try to give some examples here, to supplement Cliff's amazing answer. It might be that none of this makes sense, but it's how I wrap my mind around it. Feel free to correct as necessary. Let's work off the simplest case, an array of {1, 2, 3}. If we are ...


3

When you call a recursive function, you actually need to deal with the return value of that function. You are currently just calling BinarySearch(key, array, min, midpoint-1) for example and then throwing away whatever value it returns. Be sure to return BinarySearch(...) whenever you call it. Also, look again at your int midpoint value. If, for example, ...


3

The problem is that the compiler believes that the collatz() function can finish processing without hitting a return. While it may logically not be possible, compilers are dumb. They can't analyze code well enough to see that all logical possibilities may be covered. Instead, they verify that all paths through the code will end at a return. Because your ...


2

Make a function declared as bool binsearch(int array[], int start, int end, int ele), with start being the starting index and end being the last index(exclusive), and ele is the element to be search in the array. Initially call the function as binsearch(array, 0, len, ele), where len is the length of array. Now depending upon the value at mid element, the ...


2

Unroll sigma (5) and see what you get .. sigma (5) = 5 + sigma (4) = 5 + (4 + sigma (3)) = 5 + (4 + (3 + sigma (2))) = 5 + (4 + (3 + (2 + sigma (1)))) = 5 + (4 + (3 + (2 + (1 + sigma (0))))) = 5 + (4 + (3 + (2 + (1 + 0)))) = 5 + (4 + (3 + (2 + 1))) = 5 + (4 + (3 + 3)) = 5 + (4 + 6) = 5 + 10 = 15 ...


2

@SFri: I agree that it's hard to tell without seeing some code, but I'll tell you what i think can happen from the errors and the pseudocode. The reason to set a recently freed pointer to null is to clean up and be tidy, but it should not affect how your function works. If you get a double free error by commenting out the line that sets the freed pointer to ...


2

You have only implemented part of your goal. This code will walk down a linked list to the last element, but will only free memory when it finds that the next element is null, i.e., it is at the end of the list. To free memory as you go (beginning to end), you should create a temp pointer to hold the address of the next element, and then free the current ...


2

The issue lies in how you are initializing your nodes. for (int i=0; i<27; i++) { trav->next[x]=NULL; } What is x on that line? Pay special attention to your counting variable in the loop. To answer your question about calloc, calloc will give you initialized (to zero) memory, which means you don't have to do the initialization loop. I would ...


2

The fact() function is a recursive function that takes a pointer to an int as input. However, the internal recursive call, return *a * fact(a - 1); is not passing a valid pointer to an int. It's clear that you want to pass the next smaller integer, but you are actually passing the contents stored at the pointer minus 1, instead of a pointer address. This ...


2

Recursive with memoization and non-recursive do the same number of computations. The recursive version however is more complex, memoization requires some check whether the value already has been calculated (the loops guarantee that), the additional function calls add some overhead, memory and time wise. The most important thing in recursion is a) defining ...


2

If you fix the missing semicolon that @DinoCoderSaurus mentioned, this works fine. I suspect you hadn't compiled it after writing your function, so the default return false was still being used.


2

You could change # Recursive case 1 - Current denomination in list is too large to be used to make change if (amount < first): make_change(amount, rest) # Recursive case 2 - Use current denomination to make change if (amount >= first): outlist.append(first) make_change((amount - first), denoms) to # ...


1

The problem with using recursion here, is that it executes x times not only the part with the sorting but also the printing part. If you want to go with recursion you could split into two separate functions: the recursive one, for the sorting part, and other with the printing part. If you want to keep it in one function, you could simply change the value of ...


1

Actually, it probably leaks a lot more than that, depending on the size of the linked list. The first version is an either/or. It will either free a node or move to the next node. In practice, that means that it will walk the linked list to the last node and free it, and then return to the original calling code. The second version is different. It will ...


1

The number of calls is usually limited by the size of the stack, but the details differ between programming languages, operating systems, and how they handle memory. Operating systems usually require the stack to be in a statically sized contiguous memory block (because that makes it easier to handle), those are precious, so you are limited to what you got. ...


1

At the machine code level a loop is just a conditional jump and a test whereas a naively implemented recursion involves jumps, stack frame pushes, stack frame pops, and returns. Howbeit, most recursive functions can be rewritten to avoid stack frame push/pop by exerting tail recursion which yield an equivalent to an iterative loop at the machine code level. ...


1

If I had to bet on something it would be for a stack overflow, although I recognize that I am not a great expert in terms of the stack, it is bad programming practice to use large amounts of memory in the stack, one solution could be to use the dynamic allocation of memory, although I do not see very clear how to use it with recursion


1

Recursion isn't done right. For example, your function meant to return an int might end without returning anything. Essence of recursion is that you define a base case (like for example rod of length 0 is worth nothing), and assume a sub-problem closer to that base case (shorter rod) can already be solved. Like you get a rod of length 10, and assume the ...


1

Your fact() function looks like it is correctly written. The problem lies in how you are calling it in/from main: return fact(n); This line means "Take the value returned by fact(n), return that value to the operating system, and terminate main()." If you want to use the value returned by fact(n) and continue to execute the main program, you have to ...


1

Hint for formatting code: after you paste it into question, select it and click {} icon in the format bar. (you can do the same when you edit the question). The return value from main is not automatically displayed anywhere. You can see the result by typing echo $? immediately after the program finishes. echo $? displays the return value of the most ...


1

The value is actually returned to the main function ...but it is not printed in the terminal ..so ur second option prints it in the screen, so you'll know the returned valu:).vote if you understood ..happy coding


1

You free the node even if it has children. You should first destroy any non-NULL children, and then free the current node. No while loop required, just a single loop over all the children, calling destroy on the non-NULL ones.


1

Your binarysearch function follows this logic: if (something) { if (something_else) { else { /* ... */ } /* **ERR:NOTHING_IS_RETURNED** */ // Here you must add a return false statement } As you can see you're not returning anything in case the first if condition fails.


1

Your int main function is not returning a value, which is required if you specify a return type. Either change it to void main() or well, add a return type. Hope that helps!


1

Did you make dictionary or make speller? The latter is correct, the former will produce the error you saw, along with about 20 relocation errors. This almost certainly has nothing to do with your code and all to do with how you tried to build the executable. Part of this lesson is understanding how to build a project using multiple source code files and a ...


1

Your first mergesort() call results in an infinite loop. beg < end will always evaluate to true as the value is not modified. Recursively calling mergesort() creates copies of those integers. Any changes inside those functions have no effect on them. Consider removing the loop. Add a check such that the function does nothing if there's only one element to ...


Only top voted, non community-wiki answers of a minimum length are eligible