#define _CRT_SECURE_NO_WARNINGS
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include "header.h"

double integrate_rec_l(double(*f)(double),double prev_S, long int N, double a, double b) {
	double S;
	long double H = (long double)(b - a) / N;

	S = 0;
	for (int i = 2;i <= N;i += 2) {
		S += f(a + (i-1)*H);
	}


	return H * S + (0.5) * prev_S;
	
}
void runge_write(int power, double(*f)(double),double a,double b) {
	double S, S2 = 0;
	long int N = 1;
	int counter = 0;
	char path_res[100];


	S2 = (b - a) * f(0);

	sprintf(path_res, "C:/Users/egorl/source/repos/num_methods_3/lab10/res_%i.txt", power);
	FILE* f_res;
	f_res = fopen(path_res, "w+");
	
	do {

		counter++;

		S = S2;
		N *= 2;

		S2 = integrate_rec_l(f, S, N, a, b);

		fprintf(f_res, "%.20lf %.20lf %d %ld\n", S2, (b - a) / N, counter, N);
		printf("N = %ld, S = %lf, counter = %d,eps = %e\n", N, S2, counter, pow(10, -power));

	} while (fabs(S2 - S) > pow(10, -power));
}