//#include "pch.h"
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
double df(double x, double y)            //function for defining dy/dx
{
	double a = (x * x) / (y - 1);                //Enter the Differential Equation to be solved
	return a;

}

int main()
{
	char ch, ch1;
	cout << "This is the Improved Eullers Method calculator." << endl;
	cout << "Enter the differential equation to be solved in line 8 of the code" << endl;
	cout << "have you entered the right equation? (Enter Y or N)" << endl;
	cin >> ch;
	ch1 = ch;
	while (ch1 == 'y' || ch1 == 'Y') {


		int n;
		double x0, y0, x, y, h, K1, K2, Z;            
		cout.precision(4);							//sets number of decimal places to be displayed.
		cout.setf(ios::fixed);						//displays floating point numbers in standard notation
		cout << "\nEnter the initial values of x and y respectively:\n";        
		cin >> x0 >> y0;
		cout << "\nFor what value of x do you want to find the value of y\n";
		cin >> x;
		cout << "\nEnter the step size h:\n";           
		cin >> h;
		cout << "x" << setw(16) << "y" << setw(19) << "K1" << setw(16) << "Z" << setw(19) << "K2" << setw(16) << "y(i+1)\n";
		cout << "---------------------------------------------------------------------------------------------------------\n";
		while (fabs(x - x0) > 0.000001)
		{
			//y = y0 + (h*df(x0, y0));            //calculate next y   
			K1 = df(x0, y0);
			Z = y0 + (h*df(x0, y0));
			K2 = df(x0 + h, Z);
			y = y0 + ((K1 + K2) / 2)*h;
			cout << x0 << setw(16) << y0 << setw(16) << K1 << setw(16) << Z << setw(16) << K2 << setw(16) << y << endl;
			y0 = y;                    //pass this new y as y0 in the next iteration.
			x0 = x0 + h;                //calculate new x.
		}
		cout << x0 << setw(16) << y << endl;
		cout << "----------------------------------------------------------------------" << endl;
		cout << "y(" << x0 << ") = " << y << endl;
		break;
	}
	goto EndLable;
EndLable:
	cout << "----------------------------------------------------------------------" << endl;
	cout << "Program will end so that you can enter the differential equation to be solved" << endl;

	system("Pause");
	return 0;
}