Linear equation/interpolation - basic compile errors
I am trying to create C++ code that allows a linear equation (interpolator?) to take in a value and produce an output based on a table that is specified.
The problem I am having is the GE compiler uses libraries not available in Visual studio and visa versa…
Screen shot of error…” error: expected ‘=’ ‘,’’;’’asm’…ect.
This is the code I am working with. Just set up a flat XY trying to link inputs and outputs to X0 – Xn and Y0 – Yn.
// Lin_Interp.c : Defines the entry point for the console application.
//
#include "PACRXPlc.h" /* Include file applicable for all targets */
#include "string.h"
#include "math.h"
#include "stdlib.h"
#include <time.h>
#include <ctype.h>
#include <stdio.h>
/* Constants / #defines */
T_INT16 *X, T_INT16 *X0, T_INT16 *X1, T_INT16 *X2, T_INT16 *Y0, T_INT16 *Y1, T_INT16 *Y2, T_INT16 *Z1)
int Gefmain =(){
label1:
int i=0,j=0,X0=0,Y0=0,X1=0,Y1=0;
double X,Y,Fxy=0,Fxy0=0,FxyOld=0,Fxy1=0;
// "Lin Interp Y OPERATING LIMIT FUNCTION - CORRECTED Y ARRAY" TY
double TY[11]={0,10,20,30,40,50,60,70,80,90,100,};
// "Lin Interp X OPERATING LIMIT FUNCTION – CORRECTED X ARRAY" TX
double TX[11]={0,10,20,30,40,50,60,70,80,90,100};
// "LIN Interp OPERATING LIMIT FUNCTION - XY LIMIT ARRAY" XYlim
double XYlim[11][11]={0.0,10.0,20.0,30.0,40.0,50.0,60.0,70.0,80.0,90.0,100.0,
0.0,10.0,20.0,30.0,40.0,50.0,60.0,70.0,80.0,90.0,100.0,
0.0,10.0,20.0,30.0,40.0,50.0,60.0,70.0,80.0,90.0,100.0,
0.0,10.0,20.0,30.0,40.0,50.0,60.0,70.0,80.0,90.0,100.0,
0.0,10.0,20.0,30.0,40.0,50.0,60.0,70.0,80.0,90.0,100.0,
0.0,10.0,20.0,30.0,40.0,50.0,60.0,70.0,80.0,90.0,100.0,
0.0,10.0,20.0,30.0,40.0,50.0,60.0,70.0,80.0,90.0,100.0,
0.0,10.0,20.0,30.0,40.0,50.0,60.0,70.0,80.0,90.0,100.0,
0.0,10.0,20.0,30.0,40.0,50.0,60.0,70.0,80.0,90.0,100.0,
0.0,10.0,20.0,30.0,40.0,50.0,60.0,70.0,80.0,90.0,100.0
0.0,10.0,20.0,30.0,40.0,50.0,60.0,70.0,80.0,90.0,100.0};
// Print-Out on console "XY LIMIT ARRAY" XYlim
for (j=0;j<12;j++){
for (i=0;i<10;i++){
cout<< XYlim[i][j] << " "; //Display the curve
}
cout << endl;
}
// Input X, ensure it is in the range.
cout<<endl<< "Please enter X value:";
cin>>X;
while (X >100.0 || X<0.0){
cout<<endl<< "Your X value is either greater than 100.0 or smaller than 0.0, Please re-enter again:";
cin>>X;
}
// Input Y, ensure it is in the range.
cout<<endl<< "Please enter Y value:";
cin>>Y;
while (Y >100.0 || Y<0.0){
cout<<endl<< "Your Y value is either greater than 100 or smaller than 0, Please re-enter again:";
cin>>Y;
}
cout<<endl<< " X="<<X<< ". Y="<<Y<<endl<<endl;
//Select the correct X0&Y0, X1&Y1
for (i=0;i<10;i++){
if (X >= TX[i]) X0=i;
else break;
}
X1=X0+1;
for (j=0;j<12;j++){
if (Y >= SPD[j]) Y0=j;
else break;
}
Y1=Y0+1;
cout<< " XYlim["<<X0<<"]["<<Y0<<"]="<<XYlim[X0][Y0]<<endl;
cout<< " XYlim["<<X1<<"]["<<Y0<<"]="<<XYlim[X1][Y0]<<endl;
cout<< " XYlim["<<X0<<"]["<<Y1<<"]="<<XYlim[X0][Y1]<<endl;
cout<< " XYlim["<<X1<<"]["<<Y1<<"]="<<XYlim[X1][Y1]<<endl<<endl;
//calculate F(X,Y), and print-out on console.
//Using one equation to calculate F(X,Y). The problem with this one equation method is if X0 = X1, then most of the equation's products go to zero.
//To see this problem, enter an known X value like 48. Then enter a Y value that is not known or on the curve. Example would be X=48 and Y=92.5
//The 3-equation result will be the correct value of 9.1815, the single equation result will be the result of X=48 and Y=90 or 8.672
FxyOld=XYlim[X0][Y0]+((X-TX[X0])/(TX[X1]-TX[X0])*(XYlim[X1][Y0]-XYlim[X0][Y0]))+
((Y-TY[Y0])/(TY[Y1]-TY[Y0])*(X-TX[X0])/(TX[X1]-TX[X0])*(XYlim[X1][Y1]-XYlim[X0][Y1]-XYlim[X1][Y0]+XYlim[X0][Y0]));
//Using 3 equations to calculate F(X,Y). Using this method prevents the problem that occurs when X0 = X1 and Y0 does not equal Y1.
Fxy0=XYlim[X0][Y0]+((X-TX[X0])/(TX[X1]-TX[X0])*(XYlim[X1][Y0]-XYlim[X0][Y0]));
Fxy1=XYlim[X0][Y1]+((X-TX[X0])/(TX[X1]-TX[X0])*(XYlim[X1][Y1]-XYlim[X0][Y1]));
Fxy=Fxy0+((Y-TY[Y0])/(TY[Y1]-TY[Y0])*(Fxy1-Fxy0));
cout<< " Fxy["<<X<<"]["<<Y<<"]= "<<Fxy<<" (3-equation result)"<<endl<<endl;//Three equation result to screen
cout<< " FxyOld["<<X<<"]["<<Y<<"]= "<<FxyOld<<" (1-equation result)"<<endl<<endl; //Single equation result to screen
goto label1; //Start program over
return 0;
{
Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are, by
definition, not smart enough to debug it.
- Brian W. Kernighan
Re: Linear equation/interpolation - basic compile errors
Originally Posted by controlsguy
I am trying to create C++ code that allows a linear equation (interpolator?) to take in a value and produce an output based on a table that is specified.
The problem I am having is the GE compiler uses libraries not available in Visual studio and visa versa…
Screen shot of error…” error: expected ‘=’ ‘,’’;’’asm’…ect.
1) Use code tags when posting code. Your code is practically unreadable.
2)
Code:
double XYlim[11][11];
//...
for (j=0;j<12;j++) {
for (i=0;i<10;i++) {
cout<< XYlim[i][j] << " "; //Display the curve
}
}
What happens when j == 11? You are then accessing the array with an out-of-bounds index.
3)
Code:
goto label1; //Start program over
Get rid of this, and use proper looping constructs (while, do-while, etc.)
Regards,
Paul McKenzie
Last edited by Paul McKenzie; October 13th, 2012 at 04:36 PM.
Re: Linear equation/interpolation - basic compile errors
Truly sorry about that guys. See below. Thank you for your help.
Code:
// Lin_Interp.c : Defines the entry point for the console application.
//
#include "PACRXPlc.h" /* Include file applicable for all targets */
#include "string.h"
#include "math.h"
#include "stdlib.h"
#include <time.h>
#include <ctype.h>
#include <stdio.h>
/* Constants / #defines */
T_INT16 *X, T_INT16 *X0, T_INT16 *X1, T_INT16 *X2, T_INT16 *Y0, T_INT16 *Y1, T_INT16 *Y2, T_INT16 *Z1)
int Gefmain =(){
label1:
int i=0,j=0,X0=0,Y0=0,X1=0,Y1=0;
double X,Y,Fxy=0,Fxy0=0,FxyOld=0,Fxy1=0;
// "Lin Interp Y OPERATING LIMIT FUNCTION - CORRECTED Y ARRAY" TY
double TY[11]={0,10,20,30,40,50,60,70,80,90,100,};
// "Lin Interp X OPERATING LIMIT FUNCTION – CORRECTED X ARRAY" TX
double TX[11]={0,10,20,30,40,50,60,70,80,90,100};
// "LIN Interp OPERATING LIMIT FUNCTION - XY LIMIT ARRAY" XYlim
double XYlim[11][11]={0.0,10.0,20.0,30.0,40.0,50.0,60.0,70.0,80.0,90.0,100.0,
0.0,10.0,20.0,30.0,40.0,50.0,60.0,70.0,80.0,90.0,100.0,
0.0,10.0,20.0,30.0,40.0,50.0,60.0,70.0,80.0,90.0,100.0,
0.0,10.0,20.0,30.0,40.0,50.0,60.0,70.0,80.0,90.0,100.0,
0.0,10.0,20.0,30.0,40.0,50.0,60.0,70.0,80.0,90.0,100.0,
0.0,10.0,20.0,30.0,40.0,50.0,60.0,70.0,80.0,90.0,100.0,
0.0,10.0,20.0,30.0,40.0,50.0,60.0,70.0,80.0,90.0,100.0,
0.0,10.0,20.0,30.0,40.0,50.0,60.0,70.0,80.0,90.0,100.0,
0.0,10.0,20.0,30.0,40.0,50.0,60.0,70.0,80.0,90.0,100.0,
0.0,10.0,20.0,30.0,40.0,50.0,60.0,70.0,80.0,90.0,100.0
0.0,10.0,20.0,30.0,40.0,50.0,60.0,70.0,80.0,90.0,100.0};
// Print-Out on console "XY LIMIT ARRAY" XYlim
for (j=0;j<12;j++){
for (i=0;i<10;i++){
cout<< XYlim[i][j] << " "; //Display the curve
}
cout << endl;
}
// Input X, ensure it is in the range.
cout<<endl<< "Please enter X value:";
cin>>X;
while (X >100.0 || X<0.0){
cout<<endl<< "Your X value is either greater than 100.0 or smaller than 0.0, Please re-enter again:";
cin>>X;
}
// Input Y, ensure it is in the range.
cout<<endl<< "Please enter Y value:";
cin>>Y;
while (Y >100.0 || Y<0.0){
cout<<endl<< "Your Y value is either greater than 100 or smaller than 0, Please re-enter again:";
cin>>Y;
}
cout<<endl<< " X="<<X<< ". Y="<<Y<<endl<<endl;
//Select the correct X0&Y0, X1&Y1
for (i=0;i<10;i++){
if (X >= TX[i]) X0=i;
else break;
}
X1=X0+1;
for (j=0;j<12;j++){
if (Y >= SPD[j]) Y0=j;
else break;
}
Y1=Y0+1;
cout<< " X0="<<X0<< ". X1="<<X1<<" i="<<i<<endl;
cout<< " Y0="<<Y0<< ". Y1="<<Y1<<" j="<<j<<endl<<endl;
cout<< " KGV_X0["<<X0<<"]="<<TX[X0]<< " KGV_X1["<<X1<<"]="<<TX[X1]<<endl;
cout<< " SPD_Y0["<<Y0<<"]="<<TY[Y0]<<" SPD_Y1["<<Y1<<"]="<<TY[Y1]<<endl<<endl;
cout<< " XYlim["<<X0<<"]["<<Y0<<"]="<<XYlim[X0][Y0]<<endl;
cout<< " XYlim["<<X1<<"]["<<Y0<<"]="<<XYlim[X1][Y0]<<endl;
cout<< " XYlim["<<X0<<"]["<<Y1<<"]="<<XYlim[X0][Y1]<<endl;
cout<< " XYlim["<<X1<<"]["<<Y1<<"]="<<XYlim[X1][Y1]<<endl<<endl;
//calculate F(X,Y), and print-out on console.
//Using one equation to calculate F(X,Y). The problem with this one equation method is if X0 = X1, then most of the equation's products go to zero.
//To see this problem, enter an known X value like 48. Then enter a Y value that is not known or on the curve. Example would be X=48 and Y=92.5
//The 3-equation result will be the correct value of 9.1815, the single equation result will be the result of X=48 and Y=90 or 8.672
FxyOld=XYlim[X0][Y0]+((X-TX[X0])/(TX[X1]-TX[X0])*(XYlim[X1][Y0]-XYlim[X0][Y0]))+
((Y-TY[Y0])/(TY[Y1]-TY[Y0])*(X-TX[X0])/(TX[X1]-TX[X0])*(XYlim[X1][Y1]-XYlim[X0][Y1]-XYlim[X1][Y0]+XYlim[X0][Y0]));
//Using 3 equations to calculate F(X,Y). Using this method prevents the problem that occurs when X0 = X1 and Y0 does not equal Y1.
Fxy0=XYlim[X0][Y0]+((X-TX[X0])/(TX[X1]-TX[X0])*(XYlim[X1][Y0]-XYlim[X0][Y0]));
Fxy1=XYlim[X0][Y1]+((X-TX[X0])/(TX[X1]-TX[X0])*(XYlim[X1][Y1]-XYlim[X0][Y1]));
Fxy=Fxy0+((Y-TY[Y0])/(TY[Y1]-TY[Y0])*(Fxy1-Fxy0));
cout<< " Fxy["<<X<<"]["<<Y<<"]= "<<Fxy<<" (3-equation result)"<<endl<<endl;//Three equation result to screen
cout<< " FxyOld["<<X<<"]["<<Y<<"]= "<<FxyOld<<" (1-equation result)"<<endl<<endl; //Single equation result to screen
goto label1; //Start program over
return 0;
{
Also, remember that your code has runtime and logic errors, as I stated previously. So even if you get your program to compile, you have to address those issues.
Bookmarks