fork download
  1. #include <cmath>
  2. #include <functional>
  3. #include <iostream>
  4. #include <iomanip>
  5.  
  6.  
  7. double function0(int j, int i) {
  8. if(i == 0 || j == 1) return 1;
  9. if(i == 1 || j == 0) return j;
  10. if(i > 0) return j * function0(j, --i);
  11. return 1 / (function0(j, -i)); //changed this to -i
  12. //might be a division by zero, you should check for that
  13. }
  14.  
  15. double function4(int j, int i) {
  16. bool invert = false;
  17. if(i<0) {
  18. i=-i;
  19. invert=true;
  20. }
  21. double result=1;
  22. if(i == 0) result = 1;
  23. else if(j == 0) result = j;
  24. else if (j != 1) {
  25. while(i--)
  26. result *= j;
  27. }
  28. return (invert ? 1/result : result);
  29. }
  30.  
  31. double function5(int j, int i) {
  32. return std::pow(double(j), double(i));
  33. }
  34.  
  35.  
  36. void test(const char* name, std::function<double(int, int)> f) {
  37. for(int j=-3; j<=3; ++j) {
  38. std::cout << name << ' ';
  39. for(int i=-3; i<=3; ++i) {
  40. double t = f(i, j);
  41. std::cout <<std::setw(2)<<i<<','<<std::setw(2)<<j<<'='<<std::setw(6)<<std::setprecision(2)<<t<<' ';
  42. }
  43. std::cout << '\n';
  44. }
  45. std::cout << '\n';
  46. }
  47.  
  48. int main() {
  49. std::cout << std::fixed;
  50. test("function0", function0);
  51. test("function4", function4);
  52. test("function5", function5);
  53. }
Success #stdin #stdout 0s 2888KB
stdin
Standard input is empty
stdout
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