# Questions tagged [recursion]

The tag has no usage guidance.

54 questions
Filter by
Sorted by
Tagged with
34 views

### When does a recursive function actually repeat?

I was just wondering when exactly a recursive function repeats itself. Below is code from inheritance.c from CS50's Lab 5: person *create_family(int generations) { // TODO: Allocate memory for new ...
202 views

### Pset5 Unload - Recursion with Tries - Fails Valgrind with apostrophes

I'm getting a strange problem in my unload() code, or more specifically the freeNode() recursive function that unload() calls. I think I have the recursive part of the function nailed; once free(...
110 views

### pset5 unload base case

I haven't written any code yet, although I've written pseudocode for this problem, and I keep coming back to the issue of base case. I built a trie and now want to recursively unload it. I think I've ...
10 views

### PSET 3 Plurality Print_winner function prints smallest number of votes too many times (sorting algorithm?)

I am doing Problem Set 3 Plurality. I am currently on the print_winner function. My program compiles but I'm having issues because it does not print the winner and any ties it prints whoever got the ...
19 views

### How h increases while executing for loop. In lecture3 recursion example

#include <cs50.h> #include <stdio.h> void draw(int h); int main(void) { // Get height of pyramid int height = get_int("Height: "); // Draw pyramid draw(height);...
2k views

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

If you watch the short on recursion in week2 2018, Doug explains how calculate the steps for the collatz conjecture. I don't understand how the solution he proposes can work: int collatz(int n) { ...
31 views

### pset5 - speller - recursion

// Implements a dictionary's functionality #include <stdio.h> #include <stdbool.h> #include <stdlib.h> #include <string.h> #include <strings.h> #include "dictionary....
225 views

### PSET3 Tideman: Lock_Pairs Help

I've been stuck on this function of the PSET for a couple of days now. I have met every requirement in check50 except for the one that says, ":( lock_pairs skips final pair if it creates a cycle; ...
63 views

### lock_pairs skips final pair if it creates cycle

I have been working on the tideman problem from Problem Set 3. After multiple attempts, I have been able to pass all the required checks except one: "lock_pairs skips final pair if it creates ...
155 views

### pset3 Tideman: checking for cycles

I made it so that if a losing candidate in any pair wins in another, a function checks whether there's the losing candidate in that pair wins in any other, and so forth. this here is the unlocks the ...
132 views

### Pset3 Runoff tabulation issues

I am so close to a successful runoff program, but I've been having issues with the tabulation section. I decided that this was a good place to implement a recursive function, but it is a new concept ...
243 views

### Pset3 Tideman: lock_pairs skips middle pair if it creates a cycle

I'm having issues figuring out how to solve this problem without using recursion (I'm not sure how to solve things recursively). Its a program that determines the winner of a Tideman election and I'm ...
110 views

### pset3 plurality - trouble implementing a linear sort

I have some code that completely passes check50, but prints results way too many times. For instance, here's a typical output: ~/pset3/plurality/ \$ ./plurality Alice Bob Carol Number of voters: 5 ...
25 views

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

I've already fixed this issue playing around with my code, but I cannot seem to understand why this (former) version of my recursive function leaks 112 bytes (2 nodes). Can anyone please help me ...
948 views

### Tideman lock_pairs recursion problem

I'm currently working on Pset3 Tideman and am stuck on the recursion part of lock_pairs function. I tried implementing an additional function to check for a cycle, and obviously, it's not working. ...
69 views

### Pset5 Unload Function help! (recursion)

Is my approach correct, of trying to get to the end of the linked list and free through it to the top? Does my recursion make sense? // Unloads dictionary from memory, returning true if successful ...
1k views

### Problem set 3 Plurality ISSUE cs50

I've been trying to solve the new problem set 3 plurality step by step, and here where I got stuck, but somehow I'm sure about my answer bool vote(string name) { bool v = false for (int i = 0 ; i &...
10 views

### Analysing Expected runtime

I came across a question to implement an unbiased random function which outputs 0 or 1 using a biased output function which outputs 0 or 1 with probability P for outputting 1. And to predict how the ...
3k views

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

I'm experimenting with recursive solutions to cash. I've got a number of working solutions but eventually decided I wanted to extend the functionality of the program (just to learn). I tried to write ...
81 views

### Is recursion a efficient solution?

We have learnt that in recursion a function calls itself n number of times before a condition is met, and each call gets a place in memory including each variable in that function uses. With enough ...
11 views

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

Is there a limit to the number of calls on a call stack? If so then what is the maximum number? Or is memory the limiting factor to use recursion?
422 views

### “Program received signal SIGSEGV, Segmentation fault” on using recursion with crack pset 2

I am trying to get 3 characters combinations using recursion but after some calls i get Segmentation fault #include <cs50.h> #include <stdio.h> void three_Characters(char c, char c2, ...
443 views

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

once again I'm asking help ( not to provide me with code example or pseudo code ), but for clarification on the problem. It seems that this double loop ( iterative approach ) for (int y2 = y; y2 >...
145 views

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

so then I was watching the lecture and I thought at the time it would be nice to try implement the example (well, partially at least) used in the lecture. I have two questions: Is my approach to ...
177 views

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

Im unsure if there is any error in my recursion in unloading/unload to free all memory. (But i doubt so because help50 tells me all my memory were freed- 367,084 allocs, 367,084 frees) I checked ...
57 views

### Why do i need a return value when my program will recurse until it does in fact return a value?

To fix this problem all i need to do is return something in the else part of the if statement, but it seems so pointless and messy because the program is designed to recurse until a value is returned. ...
401 views

### SEGMENTATION FAULT : Week 4- shorts-Recursion - Collatz conjecture

I can't understand why my program is facing a segmentation fault. I decided to make my own program which counts the number of steps used in Collatz Conjecture(as discussed in week 4). The picture ...
99 views

### Pset 3 Recursive Binary Search problems - Always returns true

I have tried almost everything and edited my code a hundred times in search of an answer. It seems that whenever I run the program (find.c) the result always resolves to true. Not sure where the ...
56 views

### factorial code;why does main function not return answer?

In the following code, my last statement in main function is return fact(n) does not work. But if I substitute it with printf ("Factorial of %d = %d\n", n, fact(n)); it works My question is if main ...
113 views

### How can I write a recursive function if that function's parameter is void? - pset5 - unload

Per the specification, we are not allowed to alter the declarations of load, check, size, or unload in dictionary.c or dictionary.h. Yet in Zamyla's walkthrough for unload, if one implements with a ...
825 views

### pset5 unload trie

I am really struggling with the unload function in pset5 :( I have implemented a trie to load the dictionary, and my load and check functions work fine. But somehow, the recursive structure needed ...
134 views

### I implemented Collatz recursion which is giving error “Control reached at non void function”

It has proper return value kindly guide me how can i tell compiler I have return values. here is the code #include <stdio.h> include <cs50.h> int coll (int a,int c); int ...
738 views

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

Working on pset3 and tried to write a separate binary search function in helpers.c so I can just call it later in the main search function. Here's my code for the binary search: bool binarysearch(int ...
218 views

### Why does this recursive factorial function hit segmentation fault?

This is the recursive function I'm using to calculate the factorial of an int. It compiles fine but when I run it, the program hits segmentation fault. The function receives an int taken from the ...
99 views

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

here is the code and error is"control may reach end of non-void function [-Werror,-Wreturn-type]" #include <stdio.h> #include <cs50.h> #include <stdlib.h> int collatz( int n, int c);...
168 views

### Need help with recursion for unload in pset5

Need help with pset5's unload fuction. I'm trying to use recursion. When I compile, I'm getting: clang: error: linker command failed with exit code 1 (use -v to see invocation) Here's my code. I'm ...
234 views

### pset5 unload recursion causes memory leaks

In pset5, I have made load (using a trie), check and size run successfully, even my whole codes passed check50. I believe so because when I do spell check a text using the big library, I get the same ...
3k views

### Speller Unload Trie with Recursion

node* root; bool unload(void) { freenodes(root); return true; } freenodes function(node* path) { loop over all nodes { if node is not null (path -> children[i]) ...
4k views

### Pset3 Binary Search problems

I have tested the code presented in the picture and find this function always returns false no matter what number you search.There must be a problem in it but I can not see it!! Can anyone help me ? ...
132 views

### What does free all pointers in children mean? (Unload function in Pset 5)

In the walkthrough video, it is told that we should free all pointers in children (trie). Does that mean that all pointers should be set to null? My program passes CS50 and valgrind doesn't indicate ...
74 views

### What's wrong with my recursive binary search function? [duplicate]

I've spent hours on this, have tried multiple configurations based on code I can find online, but am getting no where. Any input would be appreciated. I'm trying to make a recursive, binary search ...
285 views

### Need help with recursive binary search

Here is my helpers.c file, I have been scouring the message boards and nothing is making sense. I use the pset3 input of ./generate 1000 50 | ./find 127. I get a Didn't find needle in haystack ...
1k views

### Merge Sort running into infinite loop using recursion

When i try to run my code using using recursion i get stuck in an infinite loop. this is my first time implementing divide and conquer using recursion so i am a bit rusty on the concepts. Please have ...
624 views

### Recursion Destroy Singly linked List, memory leak?

I'm working through some personal test code to make sure I understand how to work withs singly linked lists. I'm basically going through the Week 5 Section video on singly linked lists and putting ...
154 views

### Because of calling recursive function don't do printf?

Because of calling recursive function don't do printf (bubble sort)? (actually only first time) #include <stdio.h> int* bubblesort(int n1, int arr[]) { for (int i = 0; i < n1 - ...
324 views

### unload function failed

I don't know why my unload function doesn't work.i wrote a recursive function to use in the unload function in dictionary.c and wrote the prototype in dictionary.h . i used a trie structure to be the ...
164 views

### pset3 - recursive binary search not compiling

I wrote a recursive function for binary search in pset" and I keep getting the same error from clang: "control may reach end on non-void function" it is driving me crazy... some help would be very ...
967 views

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

Before posting, I've searched a lot here and there on the Internet to find the answers to my questions myself and I did code something that eventually works. My issue is that I've re-rewrote some ...