For 2 numbers x
and n
entered by the user, my code needs to find Hn(x) defined recursively by the following formulas:
I am trying to implement a recursive version and and iterative version of that function. But I think I am getting the wrong concept of it, since my code doesn't compile due to errors on H(n) and H[n]:
#include "pch.h"
#include <iostream>
int H(int n, int x) //function for recursion
{
if (n < 0) return -1;
else if (n == 0) return 1;
else if (n == 1) return 2 * x;
return 2 * x * H(n) * x - 2 * n * H(n - 1) * x;
}
int H1(int n, int x) //function for Iterator
{
int *H1 = new int[n + 1];
H[0] * x = 1;
H[1] * x = 2 * x;
for (int i = 0; i <= n; i++)
{
H[i] * x = 2 * x * H[n] * x - 2 * n * H[n - 1] * x;
}
return H1(n) * x;
}
int main()
{
int n, x;
std::cout << "Enter the number n: ";
std::cin >> n;
std::cout << "Enter the number x: ";
std::cin >> x;
std::cout << "Rec = " << H[n] * x std::endl;
std::cout << "Iter = " << H1[n] * x std::endl;
}
It is confusing, I apologize for that as I am completely new to functions.
I already managed to do this with fibonacci sequence. And there I used only one parameter for function f(x)
that is f(int n){... }
, but here I am a bit confused with two parameters in function H(int n, int x)
, where n
is the index of H
while x
is an integer.