// factRecursion.cc

#include <iostream>
using namespace std;

#include "StopWatch.h" //  " " mean look for .h file in current directory


int factRecursion(int x);
int factIteration(int x);

int main(void)
{
  int x;
  cout << "Enter x:" << flush;
  cin >> x;

  if (x < 0)
    {
      cerr << "Sorry, x should be non-negative" << endl;
      exit(1);
    }

  StopWatch s1, s2;

  s1.reset();
  s1.start();
  cout << "x factorial (via recursion) is: " << factRecursion(x) << endl;
  s1.stop();
  
  cout << "It took us " << s1.readValue() << "microseconds to do that" << endl;

  s2.reset();
  s2.start();
  cout << "x factorial (via iteration) is: " << factIteration(x) << endl;
  s2.stop();
    
  cout << "It took us " << s2.readValue() << "microseconds to do that" << endl;

  return 0;

}

int factRecursion(int x)
{
  if (x == 0) // if x "is equal to" zero
    return 1;
  else
    return x * factRecursion(x - 1); // should be factRecursion
}


int factIteration(int x)
{
  int product = 1;
  // how can we make the following loop 
  // slightly more efficient?

  for(int i=1; i<= x; i++)
    product *= i; // product "times equals i"

  // product *= i is equiv to product = product * i;
  // product = product * i  read as "product is assigned product times i";

  return (product);
}











