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 ...
Sentox's user avatar
  • 950
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 ...
Sam Gerber's user avatar
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 ...
Cliff B's user avatar
  • 68.4k
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.
curiouskiwi's user avatar
  • 18.6k
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 ...
curiouskiwi's user avatar
  • 18.6k
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 ...
Cliff B's user avatar
  • 68.4k
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. ...
curiouskiwi's user avatar
  • 18.6k
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 ...
Ashen Gunaratne's user avatar
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 ...
Blauelf's user avatar
  • 20.9k
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 - ...
Blauelf's user avatar
  • 20.9k
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 ...
Cliff B's user avatar
  • 68.4k
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 ...
Tritum's user avatar
  • 1,571
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 ...
Crypt0xyc's user avatar
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 ...
jodok's user avatar
  • 11
1 vote
Accepted

When does a recursive function actually repeat?

Congratulations. You have interpreted recursive functions exactly correct.
Cliff B's user avatar
  • 68.4k
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 ...
DinoCoderSaurus's user avatar
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 ...
Duncan McAlister's user avatar
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 ...
Llenk's user avatar
  • 744
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 ...
Cliff B's user avatar
  • 68.4k
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 ...
Blauelf's user avatar
  • 20.9k
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!
kluvin's user avatar
  • 1,223
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 ...
Cliff B's user avatar
  • 68.4k
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 ...
MARS's user avatar
  • 5,169
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 ...
Blauelf's user avatar
  • 20.9k
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 ...
AndJF's 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 ...
Cliff B's user avatar
  • 68.4k
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 ...
DinoCoderSaurus's 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 .....
DD86's 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 ...
Blauelf's user avatar
  • 20.9k
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 ...
kluvin's user avatar
  • 1,223

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