Hello Salem,
after your tips, I understand = and == and 't' and improved with if, else if and else command. So that the code now is very strict; only s or t in cmd works. Also used M_PI without declaring/define PI value (but I prefer to use define PI and use it, because of compilation problems).
But I really dont understand
PHP Code:
int lowsave = low;
// mess around with low
// get back the original
low = lowsave;
so that the repeated lines 87-94 can be removed
Code:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define MAX 10000
int main(int argc, char *argv[])
{
FILE *fs, *ft;
char *foldpoint, line[MAX],m,t;
int i, low, high, n[MAX], size,sum,dp;
double fp,v,b;
if (argc != 7) {
puts("Improper number of arguments\nType rawfilename outputfilename foldingpoint velocity baseline t(or)s");
exit(0);
}
fs = fopen(argv[1], "r");
if (fs == NULL) {
puts("Cannot open source file");
exit(0);
}
ft = fopen(argv[2], "w");
if (ft == NULL) {
puts("Cannot open target file");
exit(0);
}
foldpoint = argv[3];
sscanf(foldpoint, "%lf", &fp);
printf("Folding point= %lf\n", fp);
sscanf(argv[4], "%lf", &v);
printf("Drive velocity= %lf\n", v);
sscanf(argv[5], "%lf", &b);
printf("Base line counts= %lf\n", b);
sscanf(argv[6], "%c", &m);{
if (m=='t') {
printf("Source motion= Triangular/Constant acceleration motion\n");
}
else if (m=='s') {
printf("Source motion= Sinus motion\n");
}
else {
puts("Type either t or s; t for triangular/constant acceleration motion,\ns for sine motion");
exit(0);
}
}
i = 0;
while (fgets(line, sizeof(line), fs)) {
sscanf(line, "%d", &n[i]);
i++;
if (i == (int)fp) {
printf("%lf is %s", fp, line);
}
}
size = i;
if (fp == (int)fp) {
low = (int)fp;
high = low;
}
else {
low = (int)fp;
high = low + 1;
}
sum=0;
low--;
high--;
i = 0;
while (low >= 0 && high < size) {
sum=sum+((n[low]-n[high])*(n[low]-n[high]));
i++;
low--;
high++;
dp=i;
}
printf("Number of data points= %d\n", dp);
printf("Sum of the sqaured differences= %d\n", sum);
if (fp == (int)fp) {
low = (int)fp;
high = low;
}
else {
low = (int)fp;
high = low + 1;
}
low--;
high--;
i = 0;
while (low >= 0 && high < size) {
if (m=='t'){
fprintf(ft, "%lf %lf\n", ((2*i)-dp+1)*v/(dp-1), (n[low]+n[high])*100/b);
i++;
low--;
high++;
}
else {
fprintf(ft, "%lf %lf\n", v*cos(i*M_PI/(dp-1)), (n[low]+n[high])*100/b);
i++;
low--;
high++;
}
}
fclose(fs);
fclose(ft);
return 0;
}