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 ...
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 ...
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
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
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
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
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
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 ...
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
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
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
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 ...
2
votes
CS50 Pset3 - Tideman - I passed all the checks except for "lock_pairs skips final pair if it creates cycle". What am I doing wrong!
The problem likely originates in your second for loop, where you return check_cycle(j, w) if the condition is true (meaning that there is an edge from the loser to another candidate), and you proceed ...
1
vote
how does recursion reach parent[1] in inheritance?
I found my answer, once the recursive function calls for "parents [0]" reach the 3rd generation, it then "returns," and finishes the rest of the code under parents [0], which then ...
1
vote
Accepted
When does a recursive function actually repeat?
Congratulations. You have interpreted recursive functions exactly correct.
1
vote
pset5 - speller - recursion
This if ((int) k == EOF) is a problem in load. EOF will never be "read" into k. EOF is the return value of fscanf. From man fscanf
Return Value
These functions return the number of input ...
1
vote
pset3 Tideman: checking for cycles
I found it unnecessary to check multiple branches to the same node the way I executed my lock cycles function. I was very surprised by this. Whether this is related to the fact the graph does not have ...
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
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
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 ...
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
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 ...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
recursion × 59pset3 × 14
pset5 × 12
unload × 10
speller × 8
trie × 6
binary-search × 6
segmentation-fault × 5
tideman × 5
cs50x × 4
return × 3
c × 2
valgrind × 2
search × 2
memory-leak × 2
function × 2
help × 2
plurality × 2
segfault × 2
free × 2
c++ × 2
mergesort × 2
inheritance × 2
pset4 × 1
pset6 × 1