Recursive functions

I am trying write code which compute a recursive function, which is used in another function.

the recursive function is.
V[n_] = 2 Cos[(2*\[Pi]*k)/205]*V[n - 1] - V[n - 2] +
sin(1000*2*pi*1/8000 * n) // N

What i've tried until now is this. V[0] and V[-1] is 0.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25

#include "stdafx.h"
#include <iostream>
#include <windows.h>
#include <math.h>

using namespace std;

void Vk(int a)
{
	i
	double k = floor((double) 1000/8000*205);
	double vk = 2*cos((2*(22/7)* k)/205) * Vk(a-1)*Vk(a - 2)+sin((double) 1000*(2*(22/7))*1/8000*a);
	return vk;
	
}

int _tmain(int argc, _TCHAR* argv[])
{
	
		Vk(0) = 0;
		Vk(-1) = 0;

	return 0;
}


But it wont work.
Last edited on
Describe the "won't work" in more detail. I bet your compiler does point some reasons why it does not accept your syntax.

What does your function return? Hint: see line 9.

You must test within the function whether 'a' is 0 or -1, and act accordingly.
i doesn't appear to be set to anything.


you are also trying to return a value from a void function.

Maybe try switching it to

double Vk(double a)

you are going to want to put a .0 after each of the numbers also so the compiler won't treat them as integers.
Last edited on
You are also doing integer division. so probably get a 0 every time.
Last edited on
The function has to be called recursivly.. So if I in my main write vk(5) it should
cout vk(5).

Vk(0) = 0;
Vk(-1) = 0;

are my initial value declaration...
I would think the way i've declared them is wrong, but how else should i declare them?


Last edited on
Did you read garions post? You are trying to return a double when it the return type is set to void change it to a double. Then I mentioned earlier you are doing integer division so you are more than likely to get 0 when dividing by 8000+ too lazy to do the math by hand but just a . or .0 or .f or .0f at the end or cast them to double/float
Yes.. I typecasted all my variables Double. and added the. .0.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24

#include "stdafx.h"
#include <iostream>
#include <windows.h>
#include <math.h>

using namespace std;

double Vk(int a)
{
	
	double k = floor((double) 1000.0/8000.0*205.0);
	double vk = 2.0*cos((2.0*(22.0/7.0)* k)/205.0) * Vk(a-1)*Vk(a - 2.0)+sin((double) 1000.0*(2.0*(22.0/7.0))*1.0/8000.0*a);
	return vk;
	
}

int _tmain(int argc, _TCHAR* argv[])
{
	
		
	return 0;
}
Last edited on
A bridge too far ... you do call Vk, which takes an integer parameter, with a - 2.0 -- a double.

You don't call the function Vk anywhere.

Remove the
1
2
#include "stdafx.h"
#include <windows.h> 

Replace int _tmain(int argc, _TCHAR* argv[]) with a standard main syntax.
See https://publib.boulder.ibm.com/infocenter/comphelp/v8v101/topic/com.ibm.xlcpp8a.doc/language/ref/mainf.htm

#include <cmath> is a bit more "C++" than the #include <math.h>


What does the function Vk return if the parameter a == 0?
What does the function Vk return if the parameter a == -1?
As it is now the function don't know what it should return for a == 0, or -1.
I tried writing my initial declaration in my function, but I get an error during runtime, which say stackoverflow.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27

#include "stdafx.h"
#include <iostream>
#include <windows.h>
#include <cmath>

using namespace std;

double Vk(int a)
{
	Vk(0) == 0;
	Vk(-1) == 0;
	double k = floor((double) 1000.0/8000.0*205.0);
	double vk = 2.0*cos((2.0*(22.0/7.0)* k)/205.0) * Vk(a-1)*Vk(a - 2.0)+sin((double) 1000.0*(2.0*(22.0/7.0))*1.0/8000.0*a);
	return vk;
	
}

int _tmain(int argc, _TCHAR* argv[])
{
	
	Vk(5);
	system("pause");

	return 0;
}



Visual studio 2010 needs, STDafx.h to compile, so i can't remove that. windows.h was added so i could use Sleep for debugging.
Last edited on
Q. How is expression Vk(0) == 0; evaluated?

A. First the function Vk is called with parameter 0. The return value of the function call is compared against 0. The result of comparison is true or false.

The problem should be obvious: the called function has this same expression, which calls function, which calls function, which ...

That is an endless recursion.


Returning 0 from function is simple: return 0.0;

When to return 0? if ( a < 1 )
Ahh.. yeah it makes sense....
Last edited on
Topic archived. No new replies allowed.