I've written my first program in c, which is a translation of another program I wrote in pascal.
It won't compile!!! Typically the error message is a parse error. I've got rid of most of them , but there are about 5-10 stubborn ones that I can't resolve, desipte two days' worth of googling.
If anyone would be willing to take a look and point me in the right direction I'd be very grateful.
Below is the program and the list of errors returned by the compiler.
The program gives a numerical solution to a differential equation using a choice of two methods: Euler method and RK4 method. To see the structure of it, simply scroll to the bottom and read int main()
and it will be obvious straight away.
If you want to try compiling this then let me know
reetep29@hotmail.com and I can email it to you. Same goes for the source file and the errorlog.txt.
Many thanks, reetep.
#include <catam.h>
float X_0[2] = {-0.0008,0.0006};
int A[2][2] = {1,-3,
-4,2};
float x[2]; /*main variables x1,x2*/
float steplength;
int iterations;
void ArrayLinComb(int dim, float coeff1, float array1[dim], float coeff2, float array2[dim], float ObjectArray[dim])
/*Calculates given linear combination of arrays: ie does ObjectArray[i]=coeff1*array1[i] + coeff2*array2[i] for all i=0...dim-1 */
{
int i;
for(i=0;i<dim;i=i+1)
ObjectArray[i]=coeff1*array1[i] + coeff2*array2[i];
}
void Get_Ax(float v[2]) /*takes a vector x and premultiplies it by matrix A. Ax is then returned*/
{
float temp[2];
temp[0] = A[0][0]*v[0] + A[0][1]*v[1];
temp[1] = A[1][0]*v[0] + A[1][1]*v[1];
v[0]=temp[0];
v[1]=temp[1];
}
void GetParameters() /*prompts user for input of steplength and xmax*/
{
char dummychar;
re_GetP:
printf("Please enter the following parameters:\n\n");
printf("Step length h = ");
fflush(stdout);
scanf("%f",&steplength);
if (steplength <= 0)
{
printf("Please enter a step length h > 0.");
goto re_GetP;
}
printf("Number of steps desired = ");
fflush(stdout);
scanf("%d",&iterations);
if (iterations<0)
{printf("Please enter a value >= 0. Try again.");
goto re_GetP;
}
printf("done now with get.");
fflush(stdout);
} /* end procedure GetParameters */
void Do_RK() /*performs algorithm for Runge-Kutta method, stores results*/
{
int i,j; /*dummy counter for 'for' statement*/
float unitarray[2] = {1,1}; /*needed for use in ArrayLinComb*/
float tempvar[2] = {0,0};
float k_1[2],k_2[2],k_3[2],k_4[2]; /*vector variables for RK method*/
FILE *fp;
FILE *gp;
fp = fopen("RK2Dx.txt","w");
gp = fopen("RK2Dy.txt","w");
fprintf(fp,"Doing RK with steplength %f and %d iterations\n\n",steplength,iterations);
fprintf(gp,"Doing RK with steplength %f and %d iterations\n\n",steplength,iterations);
ArrayLinComb(2,1,X_0,0,unitarray,x); /* initialise x,y at start points. Equivalent to x[i]=X_0[i] for all i */
for(i=0;i <= iterations;i=i+1) /* This loop records current value of x,y then iterates the calculation and repeats */
{
fprintf(fp,"%f\n",x[0]);
fprintf(gp,"%f\n",x[1]);
ArrayLinComb(2,1,x,0,unitarray,tempvar); /* read as "tempvar=x" ie we really just need x here in next line but tempvar used to be consistent with following three chunks */
Get_Ax(tempvar);
ArrayLinComb(2,steplength,tempvar,0,unitarray,k_1); /* read as k_1 = steplength*tempvar ie all elements of array multiplied by steplength */
ArrayLinComb(2,0.5,k_1,1,x,tempvar);
Get_Ax(tempvar);
ArrayLinComb(2,steplength,tempvar,0,unitarray,k_2); /* as above but now k_2 = steplength*tempvar */
ArrayLinComb(2,0.5,k_2,1,x,tempvar);
Get_Ax(tempvar);
ArrayLinComb(2,steplength,tempvar,0,unitarray,k_3); /*as above but now k_3 = steplength*tempvar */
ArrayLinComb(2,1,k_3,1,x,tempvar);
Get_Ax(tempvar);
ArrayLinComb(2,steplength,tempvar,0,unitarray,k_4); /* as above but now k_4 = steplength*k_4 */
for(j=0;j<2;j=j+1)
x[j] = ( x[j] + (k_1[j] + 2*k_2[j] +2*k_3[j] + k_4[j])/6 );
}
fclose(fp);
fclose(gp);
printf("Solution (using RK method) complete.");
fflush(stdout);
/*delay(3000);*/
} /*end procedure Do_RK*/
int main() /*main program*/
{
GetParameters();
Do_RK();
return 0;
} /*end main program*/
Now the error log:
diff.c: In function `GetParameters':
diff.c:87: parse error before `;'
diff.c: At top level:
diff.c:93: parse error before `if'
diff.c:97: conflicting types for `Xmax'
diff.c:6: previous declaration of `Xmax'
diff.c:97: parse error before `;'
diff.c:97: warning: data definition has no type or storage class
diff.c:100: parse error before string constant
diff.c:100: warning: data definition has no type or storage class
diff.c:101: parse error before string constant
diff.c:101: warning: data definition has no type or storage class
diff.c:102: parse error before string constant
diff.c:102: warning: data definition has no type or storage class
diff.c:104: parse error before string constant
diff.c:104: warning: data definition has no type or storage class
diff.c:105: parse error before string constant
diff.c:105: warning: data definition has no type or storage class
diff.c:106: parse error before `('
diff.c:107: parse error before string constant
diff.c:107: warning: data definition has no type or storage class
diff.c: In function `Get_Local_Error':
diff.c:130: parse error before `;'
diff.c: At top level:
diff.c:147: parse error before `}'
diff.c: In function `main':
diff.c:267: warning: assignment makes integer from pointer without a cast