mardi 23 avril 2019

Using recursion inside a called constructor

Don't know where to begin on how to use recursion within a called constructor.

recursion.cpp

#include <iostream>
#include <string>
#include <cmath>
using namespace std;

#include "MinilabRecursion.h"

MinilabRecursion::puzzleRecurse(int n)
    {
        int recurse(n) // <--- Don't know what to do here
        {      //error: expected ',' or ';' before '{' token
            if( n == 0 )
            {
                return 1;
            }
            if( n >= 1 )
            {
                return recurse(n - 1) + 4 * n;
            }
        }
    }          //error: expected '}' at end of input

driver.cpp

#include <iostream>
#include <string>
using namespace std;

#include "MinilabRecursion.h"
{
        cout << "puzzleRecurse(1) returns: " << MinilabRecursion::puzzleRecurse(1) << endl;

        cout << "puzzleRecurse(7) returns: " << MinilabRecursion::puzzleRecurse(7) << endl;

        cin.get();
        return 0;
}

recursion.h

#ifndef MINILABRECURSION_H
#define MINILABRECURSION_H
#include <iostream>
#include <string>
using namespace std;

class MinilabRecursion
{
public:
    static int puzzleFormula(int n);
    static int puzzleLoop(int n);
    static int puzzleRecurse(int n);
};
#endif

The if statements within recursion.cpp should give me the desired outcome, but I do not know how to use recursion in this context.

Aucun commentaire:

Enregistrer un commentaire