aboutsummaryrefslogtreecommitdiffstats
path: root/ee1301/wk6/hw6_directory/strap012_HW6B.cpp
blob: f2f0fd816e790afda370e7a46e422ecdc9c41fdc (plain) (blame)
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
#include <iostream>

int ff(int x);

int main() {
  int value = -1;
  do {
  std::cout << "Please enter a value of x: ";
    std::cin >> value;
  } while (value<0);
  std::cout << "Beginning calculation of ff(x)...\n";
  int ffx = ff(value);
  std::cout << "Calcuation complete, ff(x) = " << ffx << std::endl;
}

int ff(int x) {
  if (x > 1) {
    if (x%2 == 0) {
      std::cout << "Calling ff(" << x/2 << ")\n";
      return x*ff(x/2);
    } else {
      std::cout << "Calling ff(" << x-2 << ")\n";
      return x*ff(x-2);
    }
  } else {
    //std::cout << "Returning from ff(1) = 1\n";
    return 1;
  }
}