#include "nr.h"
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include "nrutil.h"
#include <iostream>
#include <fstream>
#include <iomanip>
#include <time.h>
#include <string.h>
#include <ctype.h>
#include <malloc.h>

int main(int argc, char **argv)
{
	int i,j;
	float *data1, *data2;
	int dataIndex1=0, dataIndex2=0;
	char line[1000];
	FILE *input;
//	for(i=0;i<argc;i++) printf("%s\n",argv[i]);
	if(argc<3) { printf("Usage: t_test datafile1, datafile2\n"); exit(1); }
	input = fopen(argv[1],"r");
	while(fgets(line,1000,input)!=NULL) {
		if(strcmp(line,"")!=0 && (line[0]!='\n')) ++dataIndex1;
	}
	//assign data1 vector
	if(dataIndex1==1) dataIndex1=2;
	data1=vector(1,dataIndex1);
	dataIndex1=0;
	rewind(input);
	while(fgets(line,1000,input)!=NULL) {
		if(strcmp(line,"")!=0 && (line[0]!='\n')) {
			data1[++dataIndex1]=atof(line);
		}
	}
	if(dataIndex1==1) { dataIndex1=2; data1[2]=data1[1]; }
	fclose(input);

	input = fopen(argv[2],"r");
	while(fgets(line,1000,input)!=NULL) {
		if(strcmp(line,"")!=0 && (line[0]!='\n')) ++dataIndex2;
	}
	//assign data2 vector
	if(dataIndex2==1) dataIndex2=2;
	data2=vector(1,dataIndex2);
	dataIndex2=0;
	rewind(input);
	while(fgets(line,1000,input)!=NULL) {
		if(strcmp(line,"")!=0 && (line[0]!='\n')) {
                        data2[++dataIndex2]=atof(line);
		}
	}
	if(dataIndex2==1) { dataIndex2=2; data2[2]=data2[1]; }
	fclose(input);

	//do t test
	float t, prob;
//	tutest(data1,dataIndex1,data2,dataIndex2,&t,&prob);
	ttest(data1,dataIndex1,data2,dataIndex2,&t,&prob);
	printf("t value: %10.6f    prob: %10.6f\n",t,prob);
}
		
