0

For 2 numbers x and n entered by the user, my code needs to find Hn(x) defined recursively by the following formulas:

Or click here

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.

Stanislav Kralin
  • 11,070
  • 4
  • 35
  • 58
Techtirium
  • 13
  • 5
  • 1
    This `H(n)` is not how you call a function that takes two parameters and this `H1[n]` is also not how you call a function. Start by giving distinct names to variables and functions, your code is highly confusing – 463035818_is_not_an_ai Dec 05 '18 at 17:27
  • `int *H1 = new int[n + 1];` -- 1) Memory leak. 2) Confusing naming variables the same as function names. – PaulMcKenzie Dec 05 '18 at 17:27
  • https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list – 463035818_is_not_an_ai Dec 05 '18 at 17:28
  • @user463035818, Yes it is confusing, I apologize for that as I am completely new to functions. – Techtirium Dec 05 '18 at 17:29
  • rome wasnt build in one day. Start with a function that prints its parameter on the screen or something as simple as that – 463035818_is_not_an_ai Dec 05 '18 at 17:30
  • I already did that with fibonacci sequence , And there I used 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`.@ user463035818 – Techtirium Dec 05 '18 at 17:33
  • then you should already know that `H[x]` is not calling a function called `H`. There are just too many syntax errors in your code and its not clear what the question is ("please fix my code" isnt really one) – 463035818_is_not_an_ai Dec 05 '18 at 17:38
  • user463035818.click on the image , to see the conditions – Techtirium Dec 05 '18 at 17:42
  • yes your are getting the concept wrong. If you have (maths notation) `f(n,x)` then this is not the same as `f(n) * x` – 463035818_is_not_an_ai Dec 05 '18 at 17:47
  • Note that your conditions aren't consistent with the Tex in the image posted. Your conditions begin at `n = 1` while the Tex begins with `n = 0`. Please be accurate here. – TrebledJ Dec 05 '18 at 17:53
  • In the picture conditions start with `n = 0` – Techtirium Dec 05 '18 at 17:58
  • @Techtirium I removed your transcription of the formula, since it was more confusing than anything else. WIth the picture in the text, it is much more clearer. I also took over some information of your comments to clarify the question. I hope it's ok like that. – Christophe Dec 05 '18 at 18:24

2 Answers2

1

Yes, you need to translate your matematically indexed function into a function with 2 parameters.

The recursive version is almost ok, except for some shifts in the indexes:

int H(int n, int x)   // recursive version
{
    if (n <= 0)      
        return -1;
    else if (n == 1) 
        return 1;
    else if (n == 2) 
        return 2 * x;
    else 
        return 2 * x * H(n-1, x) - 2 * n * H(n - 2, x); // shift n+1, n n-1 to n, n-1 n-2 
}

Your iterative version needs rework, since you should write it as a loop, if possible without cashing the values that you no longer need. For example:

int Hi(int n, int x) //iterative version
{
    if (n <= 0)      
        return -1;
    else if (n == 1) 
        return 1;

    int am2 = 1;      // start for for n-2
    int am1 = 2*x;    // start for n-1
    if (n == 2) 
        return am1;

    int am; 
    for (int i=3; i<=n; i++) {
        am = 2*x*am1 -  2*i*am2;   // calculate Hn from Hn-1 and Hn-2
        //prepare next interation
        am2=am1; 
        am1=am;
    }
    return am; 
}

Online demo

Christophe
  • 68,716
  • 7
  • 72
  • 138
0

You wrote:

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;
}

You're not far from a working program. Drop that H1 function. Let's see:

int H(int n, int x)
{
    switch(n)
    {
    // H_0(x) = 1 
    case 0: return 1;
    // H_1(x) = 2x
    case 1: return 2 * x;
    // H_{n+1}(x) = 2x H_n(x) - 2n H_{n - 1}(x)
    default:
        return 2*x*H(n-1, x) - 2*(n-1)*H(n-2, x);
    }
}

The trick part is realizing than the n in H_{n+1}(x) = 2x H_n(x) - 2n H_{n - 1}(x) and in return 2*x*H(n-1, x) - 2*(n-1)*H(n-2, x);are not the same, they differ by one.

Now, you only need to handle user I/O and calling your H function with user input.

YSC
  • 38,212
  • 9
  • 96
  • 149