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 ...
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 ...
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 ...
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,...
4
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 ...
3
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.
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 ...
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 ...
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 +...
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 ...
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 ...
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 ...
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.
...
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 - ...
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 ...
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 ...
1
vote
Accepted
Pset3 Runoff tabulation issues
rank++
This means "tell me the value of rank, then add one to rank". So in the last line of elim_test, you're calling elim_test again with the exact same values over and over again.
To fix ...
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
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 .....
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 ...
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 ...
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!
1
vote
Accepted
Need help with recursion for unload in pset5
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 ...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
recursion × 56pset3 × 14
pset5 × 12
unload × 10
speller × 8
trie × 6
binary-search × 6
tideman × 5
segmentation-fault × 4
cs50x × 3
return × 3
c × 2
valgrind × 2
search × 2
memory-leak × 2
function × 2
help × 2
plurality × 2
segfault × 2
free × 2
c++ × 2
inheritance × 2
pset4 × 1
pset6 × 1
check50 × 1