So Below is my program in its entirety. The clock() functions are giving me some very strange results. I compile it in gcc on a unix box. the clock() function at first(line 167) I print start time immediately after declaring it and the result is an incredibly long number that just cant be right.
second, after I print the result(line 191) both the endTime Variable and just printing clock() results in it equaling the value of the variable A.
now more fun, if I move that print statement at line 191 where i print the result below where I print clock(), both clock() and endtime prints as 0.
I'm very confusd and have been tinkering and asking folks for help for several hours. anyone here have any ideas?
Code:
/*/////////////////////////////////////////////////////////////////////////////
// Linear Curve Fitting - This program fits a line to the data points in the
// file provided on the command line (one data point per line of text in the
// file).
//////////////////////////////////////////////////////////////////////////////*/
#include <stdio.h>
#include <stdlib.h>
#include "data.h"
//#include <string.h>
#include <time.h>
//////////////////////////////////////////////////////////////////////////////
// Class declaration for determining the "best" line for a given set of X-Y
// data points.
//////////////////////////////////////////////////////////////////////////////
// X data list
double* Data_X;
// Y data list
double* Data_Y;
/*The length of the list*/
int Data_len = 0;
/*The number of elements in the lists*/
int Data_elems = 0;
clock_t startTime,endTime;
// The constant 'B'
double B;
// The coefficient to the linear term 'A'
double A;
// Flag indicating that the coefficients have been computed
int CoefficientsComputed;
/*/////////////////////////////////////////////////////////////////////////////
// AddPoint() - Accepts a single point and adds it to the Array, Resizing if necessary
/////////////////////////////////////////////////////////////////////////////*/
void AddPoint(double X, double Y)
{
/*If the array memmory has not been allocated, Data_len will be 0, so allocate and insert first elements*/
if (Data_len == 0){
Data_X = (double*)malloc(sizeof(double)* 3);
Data_Y = (double*)malloc(sizeof(double)* 3);
Data_X[0] = X;
Data_Y[0] = Y;
Data_len = 3;
Data_elems = 1;
} /*if*/
/*if the number of elements = the size of the array, double the size of the array*/
else if(Data_len == Data_elems){
Data_len *= 2;
Data_X = realloc(Data_X, (sizeof(double) * Data_len));
Data_Y = realloc(Data_Y, (sizeof(double) * Data_len));
/*add the X and Y to their respective arrays and increment the counter*/
Data_X[Data_elems] = X;
Data_Y[Data_elems] = Y;
Data_elems++;
}
/*Add the elements to the array and increment the elemnt variable.*/
else{
Data_X[Data_elems] = X;
Data_Y[Data_elems] = Y;
Data_elems++;
}
} // AddPoint()
/*/////////////////////////////////////////////////////////////////////////////
// GetNumberOfPoints() - Returns the number of points collected
/////////////////////////////////////////////////////////////////////////////*/
int GetNumberOfPoints()
{
return Data_elems;
} // GetNumberOfPoints()
/*/////////////////////////////////////////////////////////////////////////////
// ComputeCoefficients() - Calculate the value of the linear coefficient
// 'A' and the constant term 'B' in Y = A * X + B
/////////////////////////////////////////////////////////////////////////////*/
void ComputeCoefficients()
{
// Declare and initialize sum variables
double S_XX = 0.0;
double S_XY = 0.0;
double S_X = 0.0;
double S_Y = 0.0;
// Iterators
// Compute the sums
int lcv_X = 0;
while (lcv_X <= Data_elems)
{
S_XX += (Data_X[lcv_X]) * (Data_X[lcv_X]);
S_XY += (Data_X[lcv_X]) * (Data_Y[lcv_X]);
S_X += (Data_X[lcv_X]);
S_Y += (Data_Y[lcv_X]);
// Iterate
lcv_X++;
} // while()
// Compute the constant
B = (((S_XX * S_Y) - (S_XY * S_X)) / ((Data_elems * S_XX) - (S_X * S_X)));
// Compute the linear coefficient
A = (((Data_elems * S_XY) - (S_X * S_Y)) / ((Data_elems * S_XX) - (S_X * S_X)));
// Indicate that the Coefficients have been computed
CoefficientsComputed = 1;
} // ComputeCoefficients()
//////////////////////////////////////////////////////////////////////////////
// GetConstant() - Calculate the value of the constant 'B' in Y = A * X + B
//////////////////////////////////////////////////////////////////////////////
double GetConstant(void)
{
if (CoefficientsComputed == 0)
{
ComputeCoefficients();
} // if()
return B;
} // GetConstant()
//////////////////////////////////////////////////////////////////////////////
// GetLinearCoefficient() - Calculate the value of the linear coefficient
// 'A' in Y = A * X + B
//////////////////////////////////////////////////////////////////////////////
double GetLinearCoefficient(void)
{
if (CoefficientsComputed == 0)
{
ComputeCoefficients();
} // if()
return A;
} // GetLinearCoefficient()
//////////////////////////////////////////////////////////////////////////////
// Main program to fit a line to the data.
//////////////////////////////////////////////////////////////////////////////
int main(int argc, char *argv[])
{
// Check that a command line argument was provided
//printf("%i", argc);
if (argc == 2)
{
CoefficientsComputed = 0;
// Variables to hold the constant and linear coefficients of the line
//double A, B;
//temp variable to find location of tab and split the string
//char * pch;
//element that the tab char is
int tabLoc;
double X, Y;
// Temporary variables to hold data read from file
char temp1[sizeof(double) *3];
//start and end times
//int a;
FILE *inputFile;
startTime = clock();
//(void) time(&startTime);
printf("%f\n",startTime);
inputFile = fopen(argv[1],"r");
///printf("%iWOO\n", 1);
//a = fscanf(inputFile, "%f\t%f", X,Y);
// While a data point is returned, add it to the list
while (fgets(temp1,1000,inputFile) != NULL)
{
sscanf(temp1,"%lf %lf",&X,&Y);
//pch = strchr(temp1," ");
//tabLoc = pch; //- temp1 + 1;
// printf(temp1);
//printf("\n");
// Add the data point
AddPoint(X, Y);
//a =
} /* while() */
// Save the constant value and the linear coefficent
A = GetLinearCoefficient();
B = GetConstant();
// Print out the line that fits the data set.
endTime = clock();
printf( "The line is: Y = %.4f * X + %.4f\n" ,A, B );
printf("%fEND\n",endTime);
printf("%fstart\n",clock());
printf("%f\n",(double)CLOCKS_PER_SEC);
printf("%f\n",(double)GetNumberOfPoints());
printf( "Time per calculation = %.30f mSec", ((double)(endTime) / (double)CLOCKS_PER_SEC / (double)GetNumberOfPoints()) * 1000.0);
} // if()
else
{
// Display program usage information
printf( "Usage: %s <filename>",argv[0]);
} // if...else()
return(0);
} // main()