recursion isn't working why? (6 lines)

Jul 25, 2015 at 1:23pm
simplest question. WHY does this code crash?
('hello' is the vector: [1, 2, 3] with size 3.)

1
2
3
4
5
6
7
int sumSquares(int p) {
	if(p == 0) {
		return hello.at(p)*hello.at(p);
	} else {
		return hello.at(p)*hello.at(p) + sumSquares(hello.at(p-1));
	}
};


I am calling this function like this:

 
sumSquares(hello.size()-1);

Last edited on Jul 25, 2015 at 1:24pm
Jul 25, 2015 at 1:44pm
Edit:
The 'hello', is apparently a global variable. Why?

One simple debug method is to print the value of p at the begin of the sumSquares.
Last edited on Jul 25, 2015 at 1:51pm
Jul 25, 2015 at 1:49pm
1
2
3
4
5
6
7
int sumSquares(int p) {
	if(p == 0) {
		return hello.at(p)*hello.at(p);
	} else {
		return hello.at(p)*hello.at(p) + sumSquares(hello.at(p-1));
	}
};


1
2
3
else {
    return hello.at(p)*hello.at(p) + sumSquares(hello.at(p-1));
}

sumSquares(hello.at(p-1));

hello.at(p-1)
at first p is 2
then you call sumSquares(hello.at(2-1))
which is equal to sumSquares(hello.at(1))
and therefore equal to sumSquares(2)
so p is 2 again and you have infinite recursion.

I think you want to call this instead:
sumSquares(p-1);
Last edited on Jul 25, 2015 at 1:50pm
Jul 25, 2015 at 1:58pm
I was gonna reply with "no, you are wrong" but then I kept re-reading your post. It took me 15 min to get it

Holy f***. You are right.
Thank you SO MUCH. I would have never spotted that. I kept printing values of p at the first call and everything seemed fine in every way but yes, there is a major logic flaws there.


I basically call sumSquares with a SIZE and then call it again with CONTENT which makes no sense whatsoever. Thanks.
Jul 25, 2015 at 2:24pm
'hello' is global because I generate the array in another function
Jul 25, 2015 at 2:40pm
..and now I am stuck because I have to generate as many vectors as input by the user. Basically I need two layers of recursion which is crazy
Jul 25, 2015 at 4:55pm
'hello' is global because I generate the array in another function
It is usually considered bad practice to have global variables.
You could return that array to the main function and then pass it to sumSquared.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <vector>

std::vector<int> create_vec()
{
    return std::vector<int>({1,2,3});
}
int sumSquares(const std::vector<int>& vec, int p) {
    if(p == 0) {
        return vec.at(p)*vec.at(p);
    } else {
        return vec.at(p)*vec.at(p) + sumSquares(vec, p-1);
    }
};

int main()
{
    std::vector<int> vec = create_vec();
    std::cout << sumSquares(vec, vec.size() - 1);

    return 0;
}


..and now I am stuck because I have to generate as many vectors as input by the user. Basically I need two layers of recursion which is crazy
Which probably means that you have a bad design.
look at my example above, I hope it helps you to solve that task :)
And I think you're playing around with recursion because you want to learn it but I think this example would be better not implemented using recursion but a loop.

Last edited on Jul 25, 2015 at 5:01pm
Topic archived. No new replies allowed.