/*
 * Practica 2.
 *
 * Practicas Metodologia de la Programacion.
 * Curso 2008 - 2009
 *
 * Vicente Lujan Mansilla
 * Lorena Margareto Bisquert
 */

#include<iostream>
#include<assert.h>
#include<math.h>
#include<fstream>
#include<time.h>

using namespace std;

double fact(unsigned n);
float suma(unsigned n);
float f1(int i, int j);

void factAux(unsigned n);
void sumaAux(unsigned n);
void f1Aux(unsigned n);

void calculoCoste(unsigned ini, unsigned fin, int inc, void (*t)(unsigned), unsigned repeticiones, ostream & file);

int main()
{
	
	unsigned ini, fin, rep;
	int inc;
	ofstream f1, f2, f3;

	f1.open("ar.dat");
	f2.open("br.dat");
	f3.open("cr.dat");

	/*cout<<"Introduce el valor inicial\n";
	cin>>ini;
	cout<<"Introduce el valor final\n";
	cin>>fin;
	cout<<"Introduce el incremento\n";
	cin>>inc;
	cout<<"Introduce las repeticiones\n";
	cin>>rep;*/

	calculoCoste(1, 2000, 5, factAux, 50, f1);
	calculoCoste(1, 1000, 10, sumaAux, 1000, f2);
	calculoCoste(1, 25, 1, f1Aux, 25, f3);


	f1.close();
	f2.close();
	f3.close();

	return 0;
	
}

double fact(unsigned n)
{
    double f;
    if(n == 0)
           f = 1;
    else
           f = fact(n-1) * n;
    return f;
}

float suma(unsigned n)
{
    float s = 0;
    for(unsigned i = 0; i <= n; i++)
           for(unsigned j = 0; j <= i; j++)
                  s = s + j * i;
    return s;
}

float f1(int i, int j)
{
    float f,a,b;
    assert(i+j >= 0);
    if(i+j == 0)
           f = 0;
    else
    {
           a = f1(i-1, j);
           b = f1(i, j-1);
           f = (a + i + b + j)/2;
    }
    return f;
}

void factAux(unsigned n)
{
    fact(n);
    return;
}


void sumaAux(unsigned n)
{
    suma(n);
    return;
}


void f1Aux(unsigned n)
{
    f1(n, 0);
    return;
}

void calculoCoste(unsigned ini, unsigned fin, int inc, void (*t)(unsigned), unsigned repeticiones, ostream & file)
{	
	clock_t ini_time, fin_time;
	float time;
	ini_time = clock();

	for(unsigned i=ini; i<= fin; i+=inc)
	{
		for(unsigned j=1; j <= repeticiones; j++)
   			t(i);

		fin_time = clock();
		time =fin_time - ini_time;
		cout << "Coste en segundos: " << time / (CLOCKS_PER_SEC * repeticiones) << endl;

		file << i << "\t";
		file << time << "\n";
	}
}











