I need average for my program. And I'm using it multiple time in my program.
I'm doing this in PVSS:
So say average float is 2.0013005 (FOR EXAMPLE) but the number in the end can be higher... so my program takes this average and gives me 2.0013 in the log file. After it uses this shorter version for other calculations.instead of my original 2.0013005 (It cuts it and well that should NOT be a problem, but because of the arithmetics I use. This gap adds on with each datapoint and then it results in the slight difference in the end.
MY QUESTION IS, DO YOU KNOW HOW CAN I PREVENT MY PROGRAM FORM GIVING ME A SHORTER NUMBER INSTEAD OF A FULL ONE?
CODE
main ()
{
string s, path_name;
file f;
int year1, month1, day1, hour1, min1, sec1, i, N; // value MOVED to dyn_int
dyn_float A; //
time ts, TS1, TS2;
float MD, SD, average, setting, value, sum;
path_name = path_nameG.text;
s = TS1_panel.text;
sscanf (s, "%d.%d.%d %d:%d:%d", year1, month1, day1, hour1, min1, sec1);
TS1 = makeTime (year1, month1, day1, hour1, min1, sec1);
s = TS2_panel.text;
sscanf (s, "%d.%d.%d %d:%d:%d", year1, month1, day1, hour1, min1, sec1);
TS2 = makeTime (year1, month1, day1, hour1, min1, sec1);
//Button.enabled = FALSE;
f = fopen (path_name, "r"); // Have to read in help. parameters here.
//fgets (f, s);
while (feof(f)==0) { ///////// (f) --------- in brackets changed to path_name.
fgets (s, 100, f); // INT COUNT MUST BE PROVIDED!
sscanf (s, "%d.%d.%d %d:%d:%d %f %f", year1, month1, day1, hour1, min1, sec1, setting, value); /// SHOULD BE "f" INSTEAD OF "s"
//DebugN (year1, month1, day1, hour1, min1, sec1, setting, value);
ts = makeTime (year1, month1, day1, hour1, min1, sec1);
//TS1 - Initial timestamp
// TS2 - Final timestamp
if ((ts >= TS1) && (ts <= TS2)) {
// DebugN (ts);
dynAppend (A, value); //////////// VALUE!!!!!!!!!!!!!!!!111111111 is used in sscanf TOO!
}
}
int N = dynlen (A);
sum = 0;
for (int i = 1; i <= N; i++)
{
sum += A[i];
// sum = sum + A[i]
}
average = sum/N; // AVERAGE
DebugN (average);
// Standard Deviation ..........................................
for (int i = 1; i <= N; i++) {
//SD=0; //DELE!
SD += (A[i] - average)*(A[i] - average);
//sum += A[i];
}
SD = SD/N;
SD = sqrt(SD);
TV.text (SD);
// if ((TS2 < TS1) || (TS2 = TS1)) { // Time Stamp Comparsion TEMP!
// TV.text (" Wrong TS input");
// return 0;
//}
}
This post has been edited by jayman9: 2 Oct, 2007 - 12:55 PM