language: C++11 (gcc-4.7.2)
date: 246 days 0 hours ago
link:
visibility: private
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#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);
}
  • upload with new input
  • result: Success     time: 0s    memory: 2888 kB     returned value: 0

    function0 -3,-3= -0.04 -2,-3= -0.12 -1,-3= -1.00  0,-3=  0.00  1,-3=  1.00  2,-3=  0.12  3,-3=  0.04 
    function0 -3,-2=  0.11 -2,-2=  0.25 -1,-2=  1.00  0,-2=  0.00  1,-2=  1.00  2,-2=  0.25  3,-2=  0.11 
    function0 -3,-1= -0.33 -2,-1= -0.50 -1,-1= -1.00  0,-1=  0.00  1,-1=  1.00  2,-1=  0.50  3,-1=  0.33 
    function0 -3, 0=  1.00 -2, 0=  1.00 -1, 0=  1.00  0, 0=  1.00  1, 0=  1.00  2, 0=  1.00  3, 0=  1.00 
    function0 -3, 1= -3.00 -2, 1= -2.00 -1, 1= -1.00  0, 1=  0.00  1, 1=  1.00  2, 1=  2.00  3, 1=  3.00 
    function0 -3, 2=  9.00 -2, 2=  4.00 -1, 2=  1.00  0, 2=  0.00  1, 2=  1.00  2, 2=  4.00  3, 2=  9.00 
    function0 -3, 3=-27.00 -2, 3= -8.00 -1, 3= -1.00  0, 3=  0.00  1, 3=  1.00  2, 3=  8.00  3, 3= 27.00 
    
    function4 -3,-3= -0.04 -2,-3= -0.12 -1,-3= -1.00  0,-3=   inf  1,-3=  1.00  2,-3=  0.12  3,-3=  0.04 
    function4 -3,-2=  0.11 -2,-2=  0.25 -1,-2=  1.00  0,-2=   inf  1,-2=  1.00  2,-2=  0.25  3,-2=  0.11 
    function4 -3,-1= -0.33 -2,-1= -0.50 -1,-1= -1.00  0,-1=   inf  1,-1=  1.00  2,-1=  0.50  3,-1=  0.33 
    function4 -3, 0=  1.00 -2, 0=  1.00 -1, 0=  1.00  0, 0=  1.00  1, 0=  1.00  2, 0=  1.00  3, 0=  1.00 
    function4 -3, 1= -3.00 -2, 1= -2.00 -1, 1= -1.00  0, 1=  0.00  1, 1=  1.00  2, 1=  2.00  3, 1=  3.00 
    function4 -3, 2=  9.00 -2, 2=  4.00 -1, 2=  1.00  0, 2=  0.00  1, 2=  1.00  2, 2=  4.00  3, 2=  9.00 
    function4 -3, 3=-27.00 -2, 3= -8.00 -1, 3= -1.00  0, 3=  0.00  1, 3=  1.00  2, 3=  8.00  3, 3= 27.00 
    
    function5 -3,-3= -0.04 -2,-3= -0.12 -1,-3= -1.00  0,-3=   inf  1,-3=  1.00  2,-3=  0.12  3,-3=  0.04 
    function5 -3,-2=  0.11 -2,-2=  0.25 -1,-2=  1.00  0,-2=   inf  1,-2=  1.00  2,-2=  0.25  3,-2=  0.11 
    function5 -3,-1= -0.33 -2,-1= -0.50 -1,-1= -1.00  0,-1=   inf  1,-1=  1.00  2,-1=  0.50  3,-1=  0.33 
    function5 -3, 0=  1.00 -2, 0=  1.00 -1, 0=  1.00  0, 0=  1.00  1, 0=  1.00  2, 0=  1.00  3, 0=  1.00 
    function5 -3, 1= -3.00 -2, 1= -2.00 -1, 1= -1.00  0, 1=  0.00  1, 1=  1.00  2, 1=  2.00  3, 1=  3.00 
    function5 -3, 2=  9.00 -2, 2=  4.00 -1, 2=  1.00  0, 2=  0.00  1, 2=  1.00  2, 2=  4.00  3, 2=  9.00 
    function5 -3, 3=-27.00 -2, 3= -8.00 -1, 3= -1.00  0, 3=  0.00  1, 3=  1.00  2, 3=  8.00  3, 3= 27.00