#include <cmath>
#include <functional>
#include <iostream>
#include <iomanip>


double function0(int j, int i) {
    if(i == 0 || j == 1) return 1;
    if(i == 1 || j == 0) return j;
    if(i > 0) return j * function0(j, --i);
    return 1 / (function0(j, -i)); //changed this to -i
     //might be a division by zero, you should check for that
}

double function4(int j, int i) {
    bool invert = false;
    if(i<0) {
         i=-i; 
         invert=true;
    }
    double result=1;
    if(i == 0) result = 1;
    else if(j == 0) result = j;
    else if (j != 1) {
        while(i--)
            result *= j;
    }
    return (invert ? 1/result : result);            
}

double function5(int j, int i) {
    return std::pow(double(j), double(i));
}


void test(const char* name, std::function<double(int, int)> f) {
    for(int j=-3; j<=3; ++j) {
        std::cout << name << ' ';
        for(int i=-3; i<=3; ++i) {
            double t = f(i, j);
            std::cout <<std::setw(2)<<i<<','<<std::setw(2)<<j<<'='<<std::setw(6)<<std::setprecision(2)<<t<<' ';
        }
        std::cout << '\n';
    }
    std::cout << '\n';
}

int main() {
    std::cout << std::fixed;
    test("function0", function0);
    test("function4", function4);
    test("function5", function5);
}