20 votes
Accepted

Pset3 Binary Search problems

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 ...
user avatar
  • 63.1k
6 votes
Accepted

Pset5: Misunderstanding tries and method to assign letter to an array inside a struct

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 ...
user avatar
  • 63.1k
6 votes

Speller Unload Trie with Recursion

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 ...
user avatar
  • 2,625
4 votes
Accepted

pset3 - recursive binary search not compiling

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,...
user avatar
  • 63.1k
4 votes

Pset3 Binary Search problems

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 ...
user avatar
3 votes
Accepted

Need help with recursive binary search

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 ...
user avatar
  • 18.3k
3 votes
Accepted

I wrote this program as per the recursion video but its giving one error

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 ...
user avatar
  • 63.1k
3 votes
Accepted

PSET3 Tideman: Lock_Pairs Help

This is one of those times when I would recommend starting over. Try and break it down in terms of what you need your function to do, and build some pseudocode first. Some pointers: First, recursion ...
user avatar
  • 930
2 votes
Accepted

Slice a list for recursive function in pset3 search

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 ...
user avatar
  • 3,316
2 votes

how does sigma work?

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 +...
user avatar
  • 21
2 votes

pset 05 trie large dictionary unload issue

@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 ...
user avatar
  • 1,677
2 votes
Accepted

Recursion Destroy Singly linked List, memory leak?

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 ...
user avatar
  • 63.1k
2 votes
Accepted

Pset5 - Valgrind causes 9544106 errors, Uninitialised value was created by a heap allocation

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. ...
user avatar
  • 18.3k
2 votes
Accepted

Why does this recursive factorial function hit segmentation fault?

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 ...
user avatar
  • 63.1k
2 votes
Accepted

Week 6: DNA sequence aligning example: Are running times iterative approach and reqursive same?

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 ...
user avatar
  • 20.7k
2 votes
Accepted

Problem set 3 Plurality ISSUE cs50

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.
user avatar
  • 18.3k
2 votes
Accepted

Pset6 - how to recursively .append or .extend a list (cash.py)?

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 - ...
user avatar
  • 20.7k
2 votes
Accepted

pset3 plurality - trouble implementing a linear sort

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 ...
user avatar
  • 1,541
1 vote
Accepted

PSET5 Unload() recursive sub-function causes 112 bytes to leak - why?

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 ...
user avatar
  • 63.1k
1 vote
Accepted

Is there a limit to the number of calls which can be made in a call stack?

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 ...
user avatar
  • 20.7k
1 vote
Accepted

Is recursion a efficient solution?

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 ...
user avatar
1 vote

"Program received signal SIGSEGV, Segmentation fault" on using recursion with crack pset 2

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 ...
user avatar
  • 5,172
1 vote
Accepted

Lecture Week 6: Dynamic programming: rod example, is the recursion approach correct?

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 ...
user avatar
  • 20.7k
1 vote

I don't understand how the collatz(n) recursive function works

Based on the Collatz Conjecture, all values of n will eventually reach the value of 1 and fall into the pattern of 1, 4, 2, 1, 4... continuing forever. Therefore we want to have a base condition to ...
user avatar
  • 11
1 vote

factorial code;why does main function not return answer?

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 ...
user avatar
  • 63.1k
1 vote
Accepted

factorial code;why does main function not return answer?

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 ...
user avatar
1 vote

factorial code;why does main function not return answer?

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 .....
user avatar
  • 72
1 vote
Accepted

pset5 unload trie

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 ...
user avatar
  • 20.7k
1 vote

Recursive binary function returns error: control reaches end of non-void function

Your binarysearch function follows this logic: if (something) { if (something_else) { else { /* ... */ } /* **ERR:NOTHING_IS_RETURNED** */ // Here you must add a ...
user avatar
  • 1,223
1 vote

I wrote this program as per the recursion video but its giving one error

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!
user avatar
  • 1,223

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