# Questions tagged [recursion]

The tag has no usage guidance.

59 questions
Filter by
Sorted by
Tagged with
3 views

### Any tips or ideas on what I could do to better understand recursion?

I’m still trying to wrap my head around recursion and how and when to use it. Any tips or ideas on what I could do to better understand it?
13 views

### Merge Sort segmentation fault

I am trying to learn how to implement Merge Sort. With the code below, I keep getting a Segmentation Fault. Does anyone know why? Thanks! // 6/30/23: Segmentation fault (core dumped)?!?! #include <...
24 views

### CS50AI Tictactoe - Recursion Error

I'm trying to work on the tictactoe assignment from CS50AI. I end up getting an error: "RecursionError: maximum recursion depth exceeded while calling a Python object". I suspect that its an ...
1 vote
34 views

### Recursion question - sum of integers

I'm new to coding, working my way through week 3 of cs50X. I'm trying to teach myself how to write recursive functions, as they spent a lot of time explaining the theory behind recursion in the ...
1 vote
237 views

### CS50 Pset3 - Tideman - I passed all the checks except for "lock_pairs skips final pair if it creates cycle". What am I doing wrong!

I am unable to pass the above-mentioned check. I can't see the issue. I would greatly appreciate if someone can share some insights. Code is below: // Lock pairs into the candidate graph in order, ...
• 11
15 views

### This code should give the value 1 throuhg "count ++" but rather it is giving 0. But it is working fine when base case contains "return 2"

#include <stdio.h> int countDigits(int n){ static int count = 0; if(n==0) return count++; // return 2; return count + countDigits(n/10); } int main(void){ int ...
304 views

### how does recursion reach parent[1] in inheritance?

A similar question was asked previously about "when" the recursion happens in inheritance and I'm struggling to wrap my head around it. How does the program ever reach parent[1] if create ...
• 11
1 vote
299 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 ...
56 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....
1 vote
1k 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; ...
45 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);...
455 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 ...
280 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 ...
1 vote
390 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 ...
• 11
1 vote
178 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 ...
33 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 ...
1 vote
1k 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. ...
• 113
117 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 &...
• 23
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 ...
8k 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 ...
66 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?
1 vote
111 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 ...
453 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 >...
• 107
484 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, ...
152 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 ...
• 107
266 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 ...
6k 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) { ...
83 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. ...
119 views

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 ...
475 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 ...
109 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 ...
77 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 ...
• 19
117 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 ...
• 21
843 views

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 ...
209 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(...
• 27
172 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 ...
1k 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 ...
• 17
308 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 ...
108 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);...
171 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 ...
• 147
244 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 ...
135 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 ...
99 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 ...
324 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 ...
1 vote
825 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 ...
• 45
197 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 - ...
347 views