-
October 15th, 2012, 01:58 PM
#1
Linear Interpolation C code for X and Y axis - compile errors
When trying to compile, I am receiving errors which I am assuming are pretty generic and common:
lin_interp.c:21: error: expected '=', ',', ';', 'asm' or '__attribute__' before '{' token
lin_interp.c:100: error: expected '{' at end of input
I am not very familiar with C code, so any help would be greatly appreciated. I am just trying to get the code to work for now. I have created a program similar to this which worked in C++, but the code isn't jiving in C.
Code:
// Lin_Interp.c : Defines the entry point for the console application.
//
#include "PACRXPlc.h" /* Include file applicable for all targets */
#include "ctkInitCBlock.h"
#include "string.h"
#include "math.h"
#include "stdlib.h"
#include <time.h>
#include <ctype.h>
#include <stdio.h>
/* Constants / #defines */
// Print-Out on console "XY LIMIT ARRAY" XYlim
int GefMain (T_WORD *Y0, /* Input Param 1 */
T_WORD *Y1, /* Input Param 2 */
T_WORD *Y2, /* Input Param 3 */
T_WORD *X0, /* Input Param 4 */
T_WORD *X1, /* Input Param 5 */
T_WORD *X2, /* Input Param 6 */
T_WORD *Z1) /* Output Param 7 */
int main (){
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;
// "Y Axis LIMIT FUNCTION - CORRECCTED Y ARRAY" TY
double TY[11]={0,10,20,30,40,50,60,70,80,90,100};
// "X Axis LIMIT FUNCTION - CORRECTED X ARRAY" TX
double TX[11]={0,10,20,30,40,50,60,70,80,90,100};
// "Lin Interp OP LIMIT FUNCTION - Lin Interp 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};
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
for (j=0;j<11;j++){
for (i=0;i<11;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;
}
return 0;
}
-
October 15th, 2012, 02:26 PM
#2
Re: Linear Interpolation C code for X and Y axis - compile errors
Code:
int GefMain (T_WORD *Y0, /* Input Param 1 */
T_WORD *Y1, /* Input Param 2 */
T_WORD *Y2, /* Input Param 3 */
T_WORD *X0, /* Input Param 4 */
T_WORD *X1, /* Input Param 5 */
T_WORD *X2, /* Input Param 6 */
T_WORD *Z1); /* Output Param 7 */
You also have to implement it if you intend to use it (otherwise just remove it)
Pay some attention to how you format the code, spaces and aligned {} improve the readability a lot.
Goto isn't exactly the first thing you should think on using but anyway here are some more compilation issues:
- The label has to be set after the variables.
- Neither cin/cout nor << exists in plain C.
- SPD isn't declared
- Neither X or Y are initialized before being used
Last edited by S_M_A; October 15th, 2012 at 02:58 PM.
-
October 15th, 2012, 04:33 PM
#3
Re: Linear Interpolation C code for X and Y axis - compile errors
Originally Posted by controlsguy
I am not very familiar with C code, so any help would be greatly appreciated. I am just trying to get the code to work for now. I have created a program similar to this which worked in C++, but the code isn't jiving in C.
Please note that C and C++ are two different languages with differing rules as to what is valid and what isn't valid. Trying to go back and forth between the two languages, especially when you don't have the experience of both, will just confuse you.
So which language are you really trying to learn and use, C or C++?
Regards,
Paul McKenzie
-
October 15th, 2012, 04:51 PM
#4
Re: Linear Interpolation C code for X and Y axis - compile errors
Originally Posted by Paul McKenzie
Please note that C and C++ are two different languages with differing rules as to what is valid and what isn't valid. Trying to go back and forth between the two languages, especially when you don't have the experience of both, will just confuse you.
So which language are you really trying to learn and use, C or C++?
Regards,
Paul McKenzie
Paul, sorry for the confusion - The code needs to be in "C". Thank you for your help.
-
October 15th, 2012, 06:39 PM
#5
Re: Linear Interpolation C code for X and Y axis - compile errors
Originally Posted by controlsguy
Paul, sorry for the confusion - The code needs to be in "C". Thank you for your help.
If that's the case, then you can't use C++. Your attempt is making my point concerning confusion between the two languages.
Why are you using cout and cin, which are C++ concepts? You should be using printf() and scanf(), respectively. If you need to write this in 'C', then get 'C' books and tutorials, not C++ tutorials/books. You should forget that C++ even exists.
Regards,
Paul McKenzie
-
October 15th, 2012, 06:45 PM
#6
Re: Linear Interpolation C code for X and Y axis - compile errors
Originally Posted by Paul McKenzie
If that's the case, then you can't use C++. Your attempt is making my point concerning confusion between the two languages.
Why are you using cout and cin, which are C++ concepts? You should be using printf() and scanf(), respectively. If you need to write this in 'C', then get 'C' books and tutorials, not C++ tutorials/books. You should forget that C++ even exists.
Regards,
Paul McKenzie
Thanks Paul. Will do.
I can't find any topics on the "<<" I am using. How do these normally work?
-
October 15th, 2012, 08:51 PM
#7
Re: Linear Interpolation C code for X and Y axis - compile errors
Originally Posted by controlsguy
Thanks Paul. Will do.
I can't find any topics on the "<<" I am using. How do these normally work?
Well, here may be more confusion between C and C++.
In C++, there is a concept called operator overloading. This means that you can take an operator, for example, the "+", "-", "<<", etc, and define different meanings for them, depending on the type of arguments that are used with the operators. If the "<<" is not overloaded, it is the "shift-left n bits" operator. However for streams, whenever a "<<" is used "<<" takes on a different meaning, which is "stream the data to x".
In 'C', there is no such thing as operator overloading. Therefore the << has one and only one meaning -- shift left n bits.
So if you mean "<<" with respect to C++ streams, then there is plenty of topics on this, since streams are a core topic in C++.
Regards,
Paul McKenzie
-
October 16th, 2012, 10:56 AM
#8
Re: Linear Interpolation C code for X and Y axis - compile errors
Originally Posted by Paul McKenzie
Well, here may be more confusion between C and C++.
In C++, there is a concept called operator overloading. This means that you can take an operator, for example, the "+", "-", "<<", etc, and define different meanings for them, depending on the type of arguments that are used with the operators. If the "<<" is not overloaded, it is the "shift-left n bits" operator. However for streams, whenever a "<<" is used "<<" takes on a different meaning, which is "stream the data to x".
In 'C', there is no such thing as operator overloading. Therefore the << has one and only one meaning -- shift left n bits.
So if you mean "<<" with respect to C++ streams, then there is plenty of topics on this, since streams are a core topic in C++.
Regards,
Paul McKenzie
Thanks for your feedback, Paul.
If this is the case, how can I replace these operators in my code with the equivalent in C?
-
October 16th, 2012, 11:16 AM
#9
Re: Linear Interpolation C code for X and Y axis - compile errors
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|