Questions tagged [recursion]

The tag has no usage guidance.

Filter by
Sorted by
Tagged with
5
votes
1answer
964 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 ...
1
vote
2answers
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 ? ...
1
vote
1answer
163 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 ...
1
vote
1answer
560 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 ...
1
vote
0answers
209 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 ...
1
vote
0answers
729 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. ...
1
vote
0answers
2k views

Valgrind says “Invalid read of size 4” while freeing memory recursively in a trie

My PSET "mispelling" works fine and contains no memory leak but when I run the program with Valgrind it says Invalid read of size 4. My unload function calls a deleteTrie function which looks like ...
0
votes
1answer
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]) ...
0
votes
2answers
95 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);...
0
votes
3answers
42 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 ...
0
votes
1answer
278 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 ...
0
votes
1answer
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 &...
0
votes
1answer
2k 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 ...
0
votes
1answer
434 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 >...
0
votes
2answers
162 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 ...
0
votes
1answer
197 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 ...
0
votes
1answer
572 views

how does sigma work?

I am having trouble understanding how the recursion in following code works. So I do understand that main prints the user input. Then the function sigma will print + and a decrementation of m until ...
0
votes
1answer
572 views

pset 05 trie large dictionary unload issue

I am working through pset5 (Mispellings) using a trie and I am stumped. This is the error exactly from gdb (sample is a text file I made and works just fine with the small dictionary): Starting ...
0
votes
1answer
541 views

Slice a list for recursive function in pset3 search

I'm using a recursive function for pset3's binary search. How do you call the same function again giving half the list as an argument?
0
votes
1answer
109 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 ...
0
votes
2answers
108 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 ...
0
votes
1answer
96 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 ...
0
votes
1answer
24 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 ...
0
votes
1answer
9 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?
0
votes
1answer
70 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 ...
0
votes
2answers
378 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, ...
0
votes
1answer
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 ...
0
votes
1answer
822 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 ...
0
votes
2answers
679 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 ...
0
votes
1answer
167 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 ...
0
votes
2answers
938 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 ...
0
votes
1answer
317 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 ...
0
votes
1answer
13 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);...
0
votes
0answers
48 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 ...
0
votes
1answer
56 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 ...
0
votes
1answer
9 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 ...
0
votes
3answers
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) { ...
0
votes
1answer
35 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. ...
0
votes
1answer
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 ...
0
votes
1answer
386 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 ...
0
votes
1answer
98 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 ...
0
votes
0answers
112 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 ...
0
votes
1answer
197 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(...
0
votes
1answer
124 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 ...
0
votes
1answer
233 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 ...
0
votes
2answers
130 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 ...
0
votes
1answer
72 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 ...
0
votes
0answers
52 views

Touching Color Issues, Recursion and Pseudo-randoms in Scratch

http://scratch.mit.edu/projects/42012966 Hello All, So I was trying to build this little recursive scribbler that makes random little networks of lines, basically like an ant colony but have run into ...
0
votes
1answer
257 views

result of search function ignored by find.c

for negative results ,its O.K. 0 , 1 , 2 , 3 , 4 , 4 , 5 , 6 , 7 , 7 , 8 , 9 , 11 , 22 , 33 , 66 , 78 , 88 , 455 , 999 , lower =0 ,upper = 19 ,mid = 9 ,values[mid] = 7 lower =10 ,upper = 19 ,mid = ...
-1
votes
1answer
151 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 - ...