2.) You forgot ; after many lines.

3.) Instead of (a-b)(c-d) multiplying should look like (a-b)*(c-d)

4.) Two times you've written SQRT instead of sqrt

Solved

Posted on 2007-10-20

It's just a little problem,but I can't find it out.

The program is here:

#include <stdio.h>

int main(){

float a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v;

//a,b,c,d,e,f,g,h are numbers of coordinate.

printf("Please input 4 pairs of coordinates.\n\n");

printf("Point A (x y):");

scanf("%d%d",&a,&b);

printf("Point B (x y):");

scanf("%d%d",&c,&d);

printf("Point C (x y):");

scanf("%d%d",&e,&f);

printf("Point D (x y):");

scanf("%d%d",&g,&h);

i = sqrt((a-c)*(a-c)+(b-d)*(b-d)) //i is line AB

j = sqrt((a-e)*(a-e)+(b-f)*(b-f)) //j is line AC

k = sqrt((c-e)*(c-e)+(d-f)*(d-f)) //k is line BC

l = (i+j+k)/2

m = SQRT((l-i)(l-j)(l-k)(l))//m is area of ABC

n = sqrt((a-g)*(a-g)+(b-h)*(b-h)) //n is line AD

o = sqrt((g-c)*(g-c)+(h-d)*(h-d)) //o is line BD

p = sqrt((g-e)*(g-e)+(h-f)*(h-f)) //p is line CD

q = (n+i+o)/2

r = SQRT((q-n)(q-i)(q-o)(q))//r is area of ABD

s = (n+j+p)/2

t = SQRT((s-n)(s-j)(s-p)(s))//t is area of ACD

u = (k+o+p)/2

v = SQRT((u-k)(u-p)(u-o)(u))//v is area of BCD

if ( m = r + t + v )

printf("The point D (%.3fg,%.3fh)lies inside triangle ABC.\n");

if (m < r + t + v )

printf("The point D (%.3fg,%.3fh)lies outside triangle ABC.\n");

It's just a syntax error,can someone help?

The program is here:

#include <stdio.h>

int main(){

float a,b,c,d,e,f,g,h,i,j,k,l,m,

//a,b,c,d,e,f,g,h are numbers of coordinate.

printf("Please input 4 pairs of coordinates.\n\n");

printf("Point A (x y):");

scanf("%d%d",&a,&b);

printf("Point B (x y):");

scanf("%d%d",&c,&d);

printf("Point C (x y):");

scanf("%d%d",&e,&f);

printf("Point D (x y):");

scanf("%d%d",&g,&h);

i = sqrt((a-c)*(a-c)+(b-d)*(b-

j = sqrt((a-e)*(a-e)+(b-f)*(b-

k = sqrt((c-e)*(c-e)+(d-f)*(d-

l = (i+j+k)/2

m = SQRT((l-i)(l-j)(l-k)(l))//

n = sqrt((a-g)*(a-g)+(b-h)*(b-

o = sqrt((g-c)*(g-c)+(h-d)*(h-

p = sqrt((g-e)*(g-e)+(h-f)*(h-

q = (n+i+o)/2

r = SQRT((q-n)(q-i)(q-o)(q))//

s = (n+j+p)/2

t = SQRT((s-n)(s-j)(s-p)(s))//

u = (k+o+p)/2

v = SQRT((u-k)(u-p)(u-o)(u))//

if ( m = r + t + v )

printf("The point D (%.3fg,%.3fh)lies inside triangle ABC.\n");

if (m < r + t + v )

printf("The point D (%.3fg,%.3fh)lies outside triangle ABC.\n");

It's just a syntax error,can someone help?

54 Comments

2.) You forgot ; after many lines.

3.) Instead of (a-b)(c-d) multiplying should look like (a-b)*(c-d)

4.) Two times you've written SQRT instead of sqrt

1) You're missing some code at the end of the main :

return 0;

}

Add that.

2) In C, you can't use // for comments ... Use the /* this is a comment */ style comments instead !

3) You forgot a lot of terminating ;'s ... Each line of code has to be terminated by one !!

4) You use SQRT, but that is not defined. Use sqrt instead (notice the lower case letters - remember that C is case sensitive, so SQRT is not the same as sqrt), and add this include at the top of the code :

#include <math.h>

5) Something like this :

(l-i)(l-j)(l-k)(l)

doesn't make sense ... You need to add some operators, probably multiplication and addition as in the other lines ?

(l-i) * (l-j) + (l-k) * (l)

6) In this line :

if ( m = r + t + v )

you probably meant to use ==. == is for comparing values, = is for assigning values ...

7) You're using %d to read a float ... :

scanf("%d%d",&a,&b);

You should use %f instead :

scanf("%f %f",&a,&b);

Notice that I also added a space between the two %f's ... You want to read separate values ;)

8) You are using variable names that don't mean anything (a, b, c etc.). You should give them meaningful names, like size, amplitude, etc. ... It will make your code a lot easier to read and understand. Btw, when declaring variables, you should also initialize them to avoid problems later. So, instead of :

float a;

Use :

float a = 0.0;

9) Properly indenting your code will also help you a lot in writing proper code, and eliminating bugs.

Make these modifications, and your code should compile. Post the result here, so I can take a look at it to verify that nothing was missed.

Then it's just a question of debugging the calculations to see if they actually do what you wanted them to do ... ;)

#include <stdio.h>

#include<math.h>

int main(){

float a,b,c,d,e,f,g,h,i,j,k,l,m,

//a,b,c,d,e,f,g,h are numbers of coordinate.

printf("Please input 4 pairs of coordinates.\n\n");

printf("Point A (x y):");

scanf("%f%f",&a,&b);

printf("Point B (x y):");

scanf("%f%f",&c,&d);

printf("Point C (x y):");

scanf("%f%f",&e,&f);

printf("Point D (x y):");

scanf("%f%f",&g,&h);

i = sqrt((a-c)*(a-c)+(b-d)*(b-

j = sqrt((a-e)*(a-e)+(b-f)*(b-

k = sqrt((c-e)*(c-e)+(d-f)*(d-

l = (i+j+k)/2;

m = sqrt((l-i)*(l-j)*(l-k)*(l)

n = sqrt((a-g)*(a-g)+(b-h)*(b-

o = sqrt((g-c)*(g-c)+(h-d)*(h-

p = sqrt((g-e)*(g-e)+(h-f)*(h-

q = (n+i+o)/2;

r = sqrt((q-n)*(q-i)*(q-o)*(q)

s = (n+j+p)/2;

t = sqrt((s-n)*(s-j)*(s-p)*(s)

u = (k+o+p)/2;

v = sqrt((u-k)*(u-p)*(u-o)*(u)

if ( m = fabs(r + t + v) )

printf("The point D (%.3f,%.3f)lies inside triangle ABC.\n",g,h);

if (m < fabs(r + t + v) )

printf("The point D (%.3f,%.3f)lies outside triangle ABC.\n",g,h);

if ( fabs(m)==0 || fabs(r)==0 || fabs(t)==0 || fabs(v)==0 )

printf("The point D (%.3f,%.3f)lies on triangle ABC.\n",g,h);

return 0;

}

But then I find a problem.

If I input (0,0),(3,0)(0.4)(1,1)as A,B,C,D points,

It cannot give the correct ans.

howcome??

There's no problem ... we've all made those kind of mistakes. It's just a matter of learning from them, and try not to make them again ;)

>> If I input (0,0),(3,0)(0.4)(1,1)as A,B,C,D points,

Do you input it with the ()'s and comma's ? You should just input the values, separated by a space or a newline.

>> if ( m = fabs(r + t + v) )

Did you mean to use == here ? See comment number 6 in my previous post.

>> scanf("%f%f",&a,&b);

You forgot to put a space between the two %f's ... See comment number 7 in my previous post.

Also, consider comments 8 and 9 from my previous post. They're not needed to make the code run, but they will help you in the long run to make sense of the code, to understand what it does, and to make less mistakes when modifying code.

But I expect it is inside the triangle!!

Finally : what algorithm did you base your code on ? As I said : the variable names you used make it very difficult to understand what you are trying to do ... Choose meaningful variable names, and everything will become clearer.

The program is :

#include <stdio.h>

#include<math.h>

int main(){

float X1,Y1,X2,Y2,X3,Y3,X4,Y4,AB

//a,b,c,d,e,f,g,h are numbers of coordinate.

printf("Please input 4 pairs of coordinates.\n\n");

printf("Point A (x y):");

scanf("%f %f",&X1,&Y1);

printf("Point B (x y):");

scanf("%f %f",&X2,&Y2);

printf("Point C (x y):");

scanf("%f %f",&X3,&Y3);

printf("Point D (x y):");

scanf("%f %f",&X4,&Y4);

AB = sqrt((X1-X2)*(X1-X2)+(Y1-Y

AC = sqrt((X1-X3)*(X1-X3)+(Y1-Y

BC = sqrt((X2-X3)*(X2-X3)+(Y2-Y

S1 = (AB+AC+BC)/2;

ABC = sqrt((S1-AB)*(S1-AC)*(S1-B

AD = sqrt((X1-X4)*(X1-X4)+(Y1-Y

BD = sqrt((X4-X2)*(X4-X2)+(Y4-Y

CD = sqrt((X4-X3)*(X4-X3)+(Y4-Y

S2 = (AD+BD+CD)/2;

ABD = sqrt((S2-AD)*(S2-AB)*(S2-B

S3 = (AD+AC+CD)/2;

ACD = sqrt((S3-AD)*(S3-AC)*(S3-C

S4 = (BC+BD+CD)/2;

BCD = sqrt((S4-BC)*(S4-CD)*(S4-B

if ( ABC == fabs(ABD + ACD + BCD) )

printf("The point D (%.3f,%.3f)lies inside triangle ABC.\n",X4,Y4);

if (ABC < fabs(ABD + ACD + BCD) )

printf("The point D (%.3f,%.3f)lies outside triangle ABC.\n",X4,Y4);

if ( fabs(ABC)==0 || fabs(ABD)==0 || fabs(ACD)==0 || fabs(BCD)==0 )

printf("The point D (%.3f,%.3f)lies on triangle ABC.\n",X4,Y4);

return 0;

}

Two float values obtained using different calculations that should be equal are not necessarily equal, because a float has a limited precision, and during the calculations, rounding errors, and precision errors will occur, making it less likely that the final two values will be equal.

So, instead of using == to compare, you should use a precision margin. Something like :

value1 - margin <= value2 <= value1 + margin

So, if value2 is close to value1 within a given margin, we consider the two values equal.

Practically, in code, you can use a function like this for example :

int floatCompare(float f1, float f2) {

float margin = 0.0001;

if (f1 < f2 - margin) return -1; /* smaller */

else if (f1 > f2 + margin) return 1; /* greater */

else return 0; /* equal */

}

The value I chose for margin is just an example - you'll have to choose a value appropriate for your uses.

So, for example :

float f1, f2; /* <--- containing some values */

float f3 = sqrt(f1) * 5.0;

float f4 = sqrt(f1 * 25.0);

although logically, f3 should contain the same value as f4, that isn't necessarily so, because they were calculated in a different way, and might have been subjected to different rounding and precision errors. So, this is not guaranteed to print "equal" :

if (f3 == f4) printf("equal");

One of the values could contain 1.234567, while the other might contain 1.234568.

To get around this limitation, you can't compare float values directly using ==. You have to take into account a margin of error. Let's take the two earlier values 1.234567 and 1.234568. We'll consider them equal if the difference between them is smaller than 0.00001 :

if (fabs(f3 - f4) < 0.00001) printf("equal");

The function I posted in my previous post will do something similar.

Does that make more sense ?

The program I changed is as follow:

#include <stdio.h>

#include<math.h>

int main(){

float X1,Y1,X2,Y2,X3,Y3,X4,Y4,AB

printf("Please input 4 pairs of coordinates.\n\n");

printf("Point A (x y):");

scanf("%f %f",&X1,&Y1);

printf("Point B (x y):");

scanf("%f %f",&X2,&Y2);

printf("Point C (x y):");

scanf("%f %f",&X3,&Y3);

printf("Point D (x y):");

scanf("%f %f",&X4,&Y4);

AB = sqrt(fabs((X1-X2)*(X1-X2)+

AC = sqrt(fabs((X1-X3)*(X1-X3)+

BC = sqrt(fabs((X2-X3)*(X2-X3)+

S1 = (fabs(AB+AC+BC))/2;

ABC = sqrt(fabs((S1-AB)*(S1-AC)*

AD = sqrt(fabs((X1-X4)*(X1-X4)+

BD = sqrt(fabs((X4-X2)*(X4-X2)+

CD = sqrt(fabs((X4-X3)*(X4-X3)+

S2 = (fabs(AD+BD+CD))/2;

ABD = sqrt(fabs((S2-AD)*(S2-AB)*

S3 = (fabs(AD+AC+CD))/2;

ACD = sqrt(fabs((S3-AD)*(S3-AC)*

S4 = (fabs(BC+BD+CD))/2;

BCD = sqrt(fabs((S4-BC)*(S4-CD)*

if ( (fabs(ABD + ACD + BCD)) < ABC )

printf("The point D (%.3f,%.3f)lies inside triangle ABC.\n",X4,Y4);

if (ABC < fabs(ABD + ACD + BCD) )

printf("The point D (%.3f,%.3f)lies outside triangle ABC.\n",X4,Y4);

if ( fabs(ABC)==0 || fabs(ABD)==0 || fabs(ACD)==0 || fabs(BCD)==0 )

printf("The point D (%.3f,%.3f)lies on triangle ABC.\n",X4,Y4);

return 0;

}

I see that you added a lot of fabs's ... That wasn't necessary.

You only need to modify the comparisons (<, == etc.). When comparing two floats, you shouldn't use this :

if (float1 == float2)

but instead it's better to use :

if (fabs(float1 - float2) < 0.00001)

(with 0.00001 depending on the precision you desire).

So, for example, this :

if ( ABC == fabs(ABD + ACD + BCD) )

would become :

if ( fabs(ABC - fabs(ABD + ACD + BCD)) < 0.00001 )

or even better : using the function I posted earlier :

if (0 == floatCompare(ABC, fabs(ABD + ACD + BCD)))

Do the same for the other comparisons ...

Btw, all those fabs's you added in the rest of the code : you can remove them again - they're not needed.

Did you change all comparisons ? Can you show your code ?

if ( fabs(ABC - fabs(ABD + ACD + BCD)) < 0.00001 )

printf("The point D (%.3f,%.3f)lies inside triangle ABC.\n",X4,Y4);

if ( fabs(ABC - fabs(ABD + ACD + BCD)) > 0.00001 )

printf("The point D (%.3f,%.3f)lies outside triangle ABC.\n",X4,Y4);

if ( fabs(ABC)==0 || fabs(ABD)==0 || fabs(ACD)==0 || fabs(BCD)==0 )

printf("The point D (%.3f,%.3f)lies on triangle ABC.\n",X4,Y4);

if (floatCompare(ABC, fabs(ABD + ACD + BCD)) == 0) {

printf("The point D (%.3f,%.3f) lies inside triangle ABC.\n", X4, Y4);

}

else if (floatCompare(ABC, fabs(ABD + ACD + BCD)) < 0) {

printf("The point D (%.3f,%.3f) lies outside triangle ABC.\n", X4, Y4);

}

if (!floatCompare(fabs(ABC), 0.0) || !floatCompare(fabs(ABD), 0.0) || !floatCompare(fabs(ACD), 0.0) || !floatCompare(fabs(BCD), 0.0)) {

printf("The point D (%.3f,%.3f) lies on triangle ABC.\n", X4, Y4);

}

And if I don't add this line,it will then point out there is undefined reference.What can I do this time??

What line is that ? I don't remember showing such a line :)

>> And if I don't add this line,it will then point out there is undefined reference.

Check in one of my earlier posts, where I defined the floatCompare function ... You'll have to add that to your code of course if you want to use it ...

I mean ... what are you gonna use it for ... Is it part of something else ? Or do you just want a small application that can do that ? Or something else ?

Btw, did you have the time to make the modifications I suggested ?

#include <stdio.h>

int main(){

float X1,Y1,X2,Y2,X3,Y3,X4,Y4,AB

float floatcompare (ABC, fabs(ABD + ACD + BCD));

printf("Please input 4 pairs of coordinates.\n\n");

printf("Point A (x y):");

scanf("%f %f",&X1,&Y1);

printf("Point B (x y):");

scanf("%f %f",&X2,&Y2);

printf("Point C (x y):");

scanf("%f %f",&X3,&Y3);

printf("Point D (x y):");

scanf("%f %f",&X4,&Y4);

AB = sqrt((X1-X2)*(X1-X2)+(Y1-Y

AC = sqrt((X1-X3)*(X1-X3)+(Y1-Y

BC = sqrt((X2-X3)*(X2-X3)+(Y2-Y

S1 = (AB+AC+BC)/2;

ABC = sqrt((S1-AB)*(S1-AC)*(S1-B

AD = sqrt((X1-X4)*(X1-X4)+(Y1-Y

BD = sqrt((X4-X2)*(X4-X2)+(Y4-Y

CD = sqrt((X4-X3)*(X4-X3)+(Y4-Y

S2 = (AD+BD+CD)/2;

ABD = sqrt((S2-AD)*(S2-AB)*(S2-B

S3 = (AD+AC+CD)/2;

ACD = sqrt((S3-AD)*(S3-AC)*(S3-C

S4 = (BC+BD+CD)/2;

BCD = sqrt((S4-BC)*(S4-CD)*(S4-B

if (floatCompare(ABC, fabs(ABD + ACD + BCD)) == 0) {

printf("The point D (%.3f,%.3f) lies inside triangle ABC.\n", X4, Y4);

}

else if (floatCompare(ABC, fabs(ABD + ACD + BCD)) < 0) {

printf("The point D (%.3f,%.3f) lies outside triangle ABC.\n", X4, Y4);

}

if (!floatCompare(fabs(ABC), 0.0) || !floatCompare(fabs(ABD), 0.0) || !floatCompare(fabs(ACD), 0.0) || !floatCompare(fabs(BCD), 0.0)) {

printf("The point D (%.3f,%.3f) lies on triangle ABC.\n", X4, Y4);

}

return 0;

}

The error is that it is syntax error,howcome?

What's this line doing there ?

And where is the floatCompare function that I asked you to add ?

>> The error is that it is syntax error,howcome?

When posting an error, please always show the exact error. A "syntax error" can be just about anything and doesn't give me any clue as to what the problem is.

Remove this line :

float floatcompare (ABC, fabs(ABD + ACD + BCD));

It has no use.

And add the FloatCompare function definition that I posted earlier above your main :

http://www.experts-exchang

Do you know what a function is and how to use it ?

You really need to understand ... If something is not clear, don't hesitate to ask.

>> but what I need to add for using it?

As I said :

Remove this line :

float floatcompare (ABC, fabs(ABD + ACD + BCD));

It has no use.

And add the FloatCompare function definition that I posted earlier above your main :

http://www.experts-exchang

Do you know what a function is and how to use it ?

If you don't understand what the function does and how it works, then don't add it until you do. There's no point in using code that you don't understand. Ask for clarifications where something is not clear, please ... I don't know what you still have a problem with.

>> what should I do next?

Did you do this :

Remove this line :

float floatcompare (ABC, fabs(ABD + ACD + BCD));

It has no use.

And add the FloatCompare function definition that I posted earlier above your main :

http://www.experts-exchang

Do you know what a function is and how to use it ?

>> I would not get the correct ans

What answer do you expect ? And what answer do you get ? I'm not psychic ;)

>> are there any other ways to do that?

If you check earlier in this thread, I've shown a few ways already of comparing floats. The one with the floatCompare function is just the easiest and nicest imo.

Can you show your code ? Did you take care not to mix up floatcompare with floatCompare ?

>> please tell me what is that function definition

The one I posted in my earlier post http://www.experts-exchang

int floatcompare(ABC, fabs(ABD + ACD + BCD));{

if (floatCompare(ABC , fabs(ABD + ACD + BCD)) == 0) {

printf("The point D (%.3f,%.3f) lies inside triangle ABC.\n", X4, Y4);

}

else if (floatCompare(ABC, fabs(ABD + ACD + BCD)) < 0) {

printf("The point D (%.3f,%.3f) lies outside triangle ABC.\n", X4, Y4);

}

if (!floatCompare(fabs(ABC), 0.0) || !floatCompare(fabs(ABD), 0.0) || !floatCompare(fabs(ACD), 0.0) || !floatCompare(fabs(BCD), 0.0)) {

printf("The point D (%.3f,%.3f) lies on triangle ABC.\n", X4, Y4);

}

}

return 0; }

1) Remove this line :

float floatcompare (ABC, fabs(ABD + ACD + BCD));

It has no use.

2) And add the FloatCompare function definition that I posted earlier above your main :

http://www.experts-exchang

Please don't do anything else than that. Start from the code you posted here :

http://www.experts-exchang

and make the two modifications I mentioned, and only those.

#include<math.h>

int main(){

float X1,Y1,X2,Y2,X3,Y3,X4,Y4,AB

//X1,Y1,X2,Y2,X3,Y3,X4,Y4 are coordinates.

//ABC,ABD,ACD,BCD are areas of triangles.

//AB,AC,AD,BC,BD,CD are lines.

printf("Please input 4 pairs of coordinates.\n\n");

printf("Point A (x y):");

scanf("%f %f",&X1,&Y1);

printf("Point B (x y):");

scanf("%f %f",&X2,&Y2);

printf("Point C (x y):");

scanf("%f %f",&X3,&Y3);

printf("Point D (x y):");

scanf("%f %f",&X4,&Y4);

AB = sqrt((X1-X2)*(X1-X2)+(Y1-Y

AC = sqrt((X1-X3)*(X1-X3)+(Y1-Y

BC = sqrt((X2-X3)*(X2-X3)+(Y2-Y

S1 = (AB+AC+BC)/2;

ABC = sqrt((S1-AB)*(S1-AC)*(S1-B

AD = sqrt((X1-X4)*(X1-X4)+(Y1-Y

BD = sqrt((X4-X2)*(X4-X2)+(Y4-Y

CD = sqrt((X4-X3)*(X4-X3)+(Y4-Y

S2 = (AD+BD+CD)/2;

ABD = sqrt((S2-AD)*(S2-AB)*(S2-B

S3 = (AD+AC+CD)/2;

ACD = sqrt((S3-AD)*(S3-AC)*(S3-C

S4 = (BC+BD+CD)/2;

BCD = sqrt((S4-BC)*(S4-CD)*(S4-B

int floatcompare(ABC, fabs(ABD + ACD + BCD));{

if (floatCompare(ABC , fabs(ABD + ACD + BCD)) == 0) {

printf("The point D (%.3f,%.3f) lies inside triangle ABC.\n", X4, Y4);

}

else if (floatCompare(ABC, fabs(ABD + ACD + BCD)) < 0) {

printf("The point D (%.3f,%.3f) lies outside triangle ABC.\n", X4, Y4);

}

if (!floatCompare(fabs(ABC), 0.0) || !floatCompare(fabs(ABD), 0.0) || !floatCompare(fabs(ACD), 0.0) || !floatCompare(fabs(BCD), 0.0)) {

printf("The point D (%.3f,%.3f) lies on triangle ABC.\n", X4, Y4);

}

}

return 0;

}

I also see that you still didn't do as I said :

Start from the code you posted here :

http://www.experts-exchang

and make these two modifications, and only those :

1) Remove this line :

float floatcompare (ABC, fabs(ABD + ACD + BCD));

It has no use.

2) And add the FloatCompare function definition that I posted earlier above your main :

http://www.experts-exchang

Please don't do anything else than that.

By clicking you are agreeing to Experts Exchange's Terms of Use.

Title | # Comments | Views | Activity |
---|---|---|---|

Having a file "symlink" to a memory location (Windows) | 6 | 153 | |

How to create empty file in SAS? | 3 | 293 | |

Problem in finding output of a program | 11 | 86 | |

Setting nameservers after res_init fails doing res_query | 2 | 41 |

Join the community of 500,000 technology professionals and ask your questions.

Connect with top rated Experts

**20** Experts available now in Live!