#include <stdio.h>
#include <math.h>

void step(double x1[], double x2[], double m1, double m2, double ks1, double ks2, double ks3,
	  double kd1, double kd2, double kd3, double h, double f)  {

	
	double dx1[2];
	double dx2[2]; /* The two arrays are for x1, v1, x2, v2 */

	dx1[0] = x1[1];
	dx1[1] = ((-kd2-kd1)/m1)*x1[1] + (kd2/m2)*x2[1] + (ks2/m1)*x2[0] + ((-ks2-ks1)/m1)*x1[0];

	dx2[0] = x2[1];
	dx2[1] = (kd2/m2)*x1[1] + ((-kd2-kd3)/m2)*x2[1] + ((-ks2-ks3)/m2)*x2[0] + (ks2/m2)*x1[0] + f/m2;

	x1[0] = x1[0] +h*dx1[0];
	x1[1] = x1[1] +h*dx1[1];
	x2[0] = x2[0] +h*dx2[0];
	x2[1] = x2[1] +h*dx2[1];

} /*end step*/


void main() {

	double x1[2]; 				//State variable matix for x1 and x2
	double x2[2]; 				//State variable matix for v1 and v2
	double h;     				//Increment Size	
	double ks1, ks2, ks3, kd1, kd2, kd3;  	//Spring and Damping Coefficients
	double m1, m2;  			//Mass Values 
	double Force;				//Force Value
	double start, end;			//Values of time limits
	double t;				//variable for time
	
	FILE *fp;
	
    if((fp= fopen("C:\\temp\\out.txt", "w")) != NULL) {	

	printf("Enter the starting time: ");
	fflush(stdin); scanf("%lf", &start);

	printf("Enter the ending time: ");
	fflush(stdin); scanf("%lf", &end);

	printf("Enter the time increment: ");
	fflush(stdin); scanf("%lf", &h);

	printf("Enter the value for kd1: ");
	fflush(stdin); scanf("%lf", &kd1);

	printf("Enter the value for kd2: ");
	fflush(stdin); scanf("%lf", &kd2);

	printf("Enter the value for kd3: ");
	fflush(stdin); scanf("%lf", &kd3);
	
	printf("Enter the value for ks1: ");
	fflush(stdin); scanf("%lf", &ks1);

	printf("Enter the value for ks2: ");
	fflush(stdin); scanf("%lf", &ks2);

	printf("Enter the value for ks3: ");
	fflush(stdin); scanf("%lf", &ks3);

	printf("Enter the mass of the first cart: ");
	fflush(stdin); scanf("%lf", &m1);

	printf("Enter the mass of the second cart: ");
	fflush(stdin); scanf("%lf", &m2);
		
	printf("Enter the value of the input force: ");
	fflush(stdin); scanf("%lf", &Force);
	
	
	x1[0] = 0;
	x2[0] = 0;


	fprintf(fp, "  t     x1     v1    x2    v2    \n");
	fprintf(fp, "--------------------------------\n\n");

		
		for(t = start; t <= end; t += h)  {

			step(x1, x2, m1, m2, ks1, ks2, ks3, kd1, kd2, kd3, h, Force);
			
			fprintf(fp, "%4.4lf %8.4lf %8.4lf %8.4lf %8.4lf \n", t, x1[0], x1[1], x2[0], x2[1]);

		} //end for
    } //end if 
    
    fclose(fp);


} // end main 
