My Prof gave me this to work with....
the following function accepts an integer greater than 0, as an argument and it returns the sum of the squares of the numbers 1 through N. For example: squares(3) must compute the value 1^2 + 2^2 + 3^2, that is, 14.
I have no clue what he's talking about...but this is what i have so far

int squares(int n){
square((n-1)+n*n);
}

Am I on the right track for this?

Please check my other thread too!
http://www.daniweb.com/forums/thread118102.html

Recommended Answers

All 2 Replies

Yes, that's right.
Now return the result of the recursive call and you're almost there.

That applies to your other threads as well.

#include <iostream>
using namespace std;

int squares(int x){
	if ( x == 0)
		return 0;
	return x*x+(squares(--x));
}

int main(int argc, char **argv) {
	cout << squares(3);
	return 0;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.