?
Solved

Compile problem!!

Posted on 2007-10-20
54
Medium Priority
?
367 Views
Last Modified: 2010-04-15
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?
0
Comment
Question by:Cervan
  • 27
  • 26
54 Comments
 
LVL 7

Expert Comment

by:UrosVidojevic
ID: 20114308
1.) You need <math.h> because of sqrt function.
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
0
 
LVL 53

Accepted Solution

by:
Infinity08 earned 80 total points
ID: 20114330
A few notes :

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 ... ;)
0
 

Author Comment

by:Cervan
ID: 20117800
Thx a lot !! Oh,I have made a lot of silly mistake,but as I am a beginner,I think I need to be very careful next time.


#include <stdio.h>
#include<math.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("%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-d));
     j  = sqrt((a-e)*(a-e)+(b-f)*(b-f));  
     k  = sqrt((c-e)*(c-e)+(d-f)*(d-f));  
     l  = (i+j+k)/2;    
     m  = sqrt((l-i)*(l-j)*(l-k)*(l));
     n  = sqrt((a-g)*(a-g)+(b-h)*(b-h));  
     o  = sqrt((g-c)*(g-c)+(h-d)*(h-d));  
     p  = sqrt((g-e)*(g-e)+(h-f)*(h-f));  
     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??
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 53

Expert Comment

by:Infinity08
ID: 20117832
>> Thx a lot !! Oh,I have made a lot of silly mistake,but as I am a beginner,I think I need to be very careful next time.

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.
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 20117833
>> It cannot give the correct ans.

What input do you give ? And what output do you expect ?
0
 

Author Comment

by:Cervan
ID: 20117849
It gives out "The point D 1.000,1.000)lies outside triangle ABC.
But I expect it is inside the triangle!!
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 20117859
First, fix the problems with your code that I mentioned. Then verify that you enter the values without the ()'s and comma's as I said.

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.
0
 

Author Comment

by:Cervan
ID: 20118187
I have used heron's formula and distance formula.I have just changed the variables too.But I still cannot figure the ans out!
The program is :
#include <stdio.h>
#include<math.h>
int main(){
    float X1,Y1,X2,Y2,X3,Y3,X4,Y4,AB,AC,AD,ABC,ABD,ACD,BC,BCD,BD,CD,S1,S2,S3,S4;
    //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-Y2)*(Y1-Y2));
     AC  = sqrt((X1-X3)*(X1-X3)+(Y1-Y3)*(Y1-Y3));
     BC  = sqrt((X2-X3)*(X2-X3)+(Y2-Y3)*(Y2-Y3));
     S1   = (AB+AC+BC)/2;    
     ABC = sqrt((S1-AB)*(S1-AC)*(S1-BC)*(S1));    
     AD  = sqrt((X1-X4)*(X1-X4)+(Y1-Y4)*(Y1-Y4));  
     BD  = sqrt((X4-X2)*(X4-X2)+(Y4-Y2)*(Y4-Y2));  
     CD  = sqrt((X4-X3)*(X4-X3)+(Y4-Y3)*(Y4-Y3));  
     S2   = (AD+BD+CD)/2;
     ABD = sqrt((S2-AD)*(S2-AB)*(S2-BD)*(S2));
     S3  = (AD+AC+CD)/2;
     ACD = sqrt((S3-AD)*(S3-AC)*(S3-CD)*(S3));
     S4  = (BC+BD+CD)/2;
     BCD = sqrt((S4-BC)*(S4-CD)*(S4-BD)*(S4));
 
    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;
}
0
 

Author Comment

by:Cervan
ID: 20118203
the second sentence has been deleted.Thx!
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 20118235
The problem is that you can't rely on equality comparisons (using ==) when dealing with floats.

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.
0
 

Author Comment

by:Cervan
ID: 20124106
Well, can you explain a little bit for me as I don't really understand much on this.
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 20124902
A floating point value has a limited precision, and can't precisely represent all possible values. There will be rounding and precision errors for most operations that you do on floating points.

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 ?
0
 

Author Comment

by:Cervan
ID: 20155737
It makes more sensible to me.I get your point ,and I have tried as many times as I can add.But still I can't solve the problem,would you help me to due with the problem for me?I really feel hesitated!
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,AC,AD,ABC,ABD,ACD,BC,BCD,BD,CD,S1,S2,S3,S4;
       
    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)+(Y1-Y2)*(Y1-Y2)));
     AC  = sqrt(fabs((X1-X3)*(X1-X3)+(Y1-Y3)*(Y1-Y3)));
     BC  = sqrt(fabs((X2-X3)*(X2-X3)+(Y2-Y3)*(Y2-Y3)));
     S1  = (fabs(AB+AC+BC))/2;    
     ABC = sqrt(fabs((S1-AB)*(S1-AC)*(S1-BC)*(S1)));    
     AD  = sqrt(fabs((X1-X4)*(X1-X4)+(Y1-Y4)*(Y1-Y4)));  
     BD  = sqrt(fabs((X4-X2)*(X4-X2)+(Y4-Y2)*(Y4-Y2)));  
     CD  = sqrt(fabs((X4-X3)*(X4-X3)+(Y4-Y3)*(Y4-Y3)));  
     S2  = (fabs(AD+BD+CD))/2;
     ABD = sqrt(fabs((S2-AD)*(S2-AB)*(S2-BD)*(S2)));
     S3  = (fabs(AD+AC+CD))/2;
     ACD = sqrt(fabs((S3-AD)*(S3-AC)*(S3-CD)*(S3)));
     S4  = (fabs(BC+BD+CD))/2;
     BCD = sqrt(fabs((S4-BC)*(S4-CD)*(S4-BD)*(S4)));
 
    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;
}

0
 
LVL 53

Expert Comment

by:Infinity08
ID: 20155903
>> The program I changed is as follow:

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.
0
 

Author Comment

by:Cervan
ID: 20155931
Thank you very much for your clear explanations!!
0
 

Author Comment

by:Cervan
ID: 20156094
After I changed my program by your correction,then I enter the input and still give the incorrect output, why is it like that ( I still take the same input on the above as example)?
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 20156290
>> After I changed my program by your correction,then I enter the input and still give the incorrect output,

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

Author Comment

by:Cervan
ID: 20156528
The part I changed:    
    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);
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 20157561
Use something like this :

    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);
    }
0
 

Author Comment

by:Cervan
ID: 20161630
The problem is still here.It seems that I cannot use this function floatcompare.When I compile the program,it ponts out that I have syntax error on it on the code[float floatcompare (ABC, fabs(ABD + ACD + BCD));]
And if I don't add this line,it will then point out there is undefined reference.What can I do this time??
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 20161642
>> it on the code[float floatcompare (ABC, fabs(ABD + ACD + BCD));]

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 ...
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 20161643
Btw, can you explain what this will be used for ?
0
 

Author Comment

by:Cervan
ID: 20161664
Do you mean the program?It is used to find whether point D(input by user) lies inside the triangle.
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 20161667
>> Do you mean the program?It is used to find whether point D(input by user) lies inside the triangle.

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 ?
0
 

Author Comment

by:Cervan
ID: 20161688
small application can do
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 20161702
Never mind ... I just wanted to understand the bigger picture ... Why do you want to write this code ? What is the reason that you're making this application ?

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

Author Comment

by:Cervan
ID: 20161725
Actually if you don't mind ,you may try my program and figure out the error,I think this may fix it faster.
0
 

Author Comment

by:Cervan
ID: 20161730
I have modified my program with your suggestions.
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 20161788
>> I have modified my program with your suggestions.

And ?
0
 

Author Comment

by:Cervan
ID: 20161801
the error still appear in the output , I really feel a little bit nervous on it ,so complicated !!
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 20161803
What error are you talking about ? And what's your code now ?
0
 

Author Comment

by:Cervan
ID: 20161811
#include<math.h>
#include <stdio.h>

int main(){
    float X1,Y1,X2,Y2,X3,Y3,X4,Y4,AB,AC,AD,ABC,ABD,ACD,BC,BCD,BD,CD,S1,S2,S3,S4;
    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-Y2)*(Y1-Y2));
     AC  = sqrt((X1-X3)*(X1-X3)+(Y1-Y3)*(Y1-Y3));
     BC  = sqrt((X2-X3)*(X2-X3)+(Y2-Y3)*(Y2-Y3));
     S1  = (AB+AC+BC)/2;    
     ABC = sqrt((S1-AB)*(S1-AC)*(S1-BC)*(S1));    
     AD  = sqrt((X1-X4)*(X1-X4)+(Y1-Y4)*(Y1-Y4));  
     BD  = sqrt((X4-X2)*(X4-X2)+(Y4-Y2)*(Y4-Y2));  
     CD  = sqrt((X4-X3)*(X4-X3)+(Y4-Y3)*(Y4-Y3));  
     S2  = (AD+BD+CD)/2;
     ABD = sqrt((S2-AD)*(S2-AB)*(S2-BD)*(S2));
     S3  = (AD+AC+CD)/2;
     ACD = sqrt((S3-AD)*(S3-AC)*(S3-CD)*(S3));
     S4  = (BC+BD+CD)/2;
     BCD = sqrt((S4-BC)*(S4-CD)*(S4-BD)*(S4));
 
   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?
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 20161841
>>     float floatcompare (ABC, fabs(ABD + ACD + BCD));

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.
0
 

Author Comment

by:Cervan
ID: 20161856
sorry,I forget to tell you that error.That is also you pointed out that problem.Besides,I don't know what to add on function.
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 20161872
I'm sorry I don't understand what you're saying.


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-exchange.com/Programming/Languages/C/Q_22906376.html#20118235

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

Expert Comment

by:Infinity08
ID: 20161876
Also : did you really understand my explanation about comparing floats ? If you don't understand it, then please say so, and I'll be happy to clear up any confusion that still exists.
0
 

Author Comment

by:Cervan
ID: 20161908
Yes,somehow I don't really understand about floatcompare.so I don't know what to do for this function.I already know the use of it,but what I need to add for using it?
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 20161913
>> I already know the use of 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-exchange.com/Programming/Languages/C/Q_22906376.html#20118235

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

Author Comment

by:Cervan
ID: 20162001
Will I need to add a special header for it? I just learn a little bit function and I don't learn to use floatcompare, nowI just want to make it be more simple ,what should I do next?
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 20162104
the floatCompare function is a little function that I wrote myself, so you don't need to add a header for it ... Just the function definition I posted.

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-exchange.com/Programming/Languages/C/Q_22906376.html#20118235

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

Author Comment

by:Cervan
ID: 20162121
Yes,I remove that line ,but I keep using fabs,I would not get the correct ans,are there any other ways to do that?
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 20162137
Cervan ... can you limit yourself to doing exactly what I ask ? You seem to always change other things that I didn't tell you to.


>> 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.
0
 

Author Comment

by:Cervan
ID: 20162182
Okay,I will try not to be off track.But when I use this function(copy from your suggestion),the compiler point out that floatcompare is an undefined reference,what should I add in order to solve this,I am a little bit stupid,please tell me what is that function definition , I have already define float ,then how?
0
 

Author Comment

by:Cervan
ID: 20162197
My input is still as the same on above.It is sure that point D must be inside the triangle,but it always give out that it is outside the triangle, I am wondering whether my algorithm is wrong or not.
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 20162200
>> the compiler point out that floatcompare is an undefined reference

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-exchange.com/Programming/Languages/C/Q_22906376.html#20118235
0
 

Author Comment

by:Cervan
ID: 20162237
I just change the lowerest part,while the other reamains unchanged as above.
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;     }
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 20162249
Cervan : can you go back, and do exactly what I say ? It's not complicated :

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-exchange.com/Programming/Languages/C/Q_22906376.html#20118235


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

        http://www.experts-exchange.com/Programming/Languages/C/Q_22906376.html#20161811

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

Author Comment

by:Cervan
ID: 20162287
#include <stdio.h>
#include<math.h>

int main(){
    float X1,Y1,X2,Y2,X3,Y3,X4,Y4,AB,AC,AD,ABC,ABD,ACD,BC,BCD,BD,CD,S1,S2,S3,S4;
    //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-Y2)*(Y1-Y2));
     AC  = sqrt((X1-X3)*(X1-X3)+(Y1-Y3)*(Y1-Y3));
     BC  = sqrt((X2-X3)*(X2-X3)+(Y2-Y3)*(Y2-Y3));
     S1  = (AB+AC+BC)/2;    
     ABC = sqrt((S1-AB)*(S1-AC)*(S1-BC)*(S1));    
     AD  = sqrt((X1-X4)*(X1-X4)+(Y1-Y4)*(Y1-Y4));  
     BD  = sqrt((X4-X2)*(X4-X2)+(Y4-Y2)*(Y4-Y2));  
     CD  = sqrt((X4-X3)*(X4-X3)+(Y4-Y3)*(Y4-Y3));  
     S2  = (AD+BD+CD)/2;
     ABD = sqrt((S2-AD)*(S2-AB)*(S2-BD)*(S2));
     S3  = (AD+AC+CD)/2;
     ACD = sqrt((S3-AD)*(S3-AC)*(S3-CD)*(S3));
     S4  = (BC+BD+CD)/2;
     BCD = sqrt((S4-BC)*(S4-CD)*(S4-BD)*(S4));
   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;    
}

0
 

Author Comment

by:Cervan
ID: 20162297
To make it easier,might you write your and I wanna to compare what I have done wrong.I think it would do better for me to understand.
0
 

Author Comment

by:Cervan
ID: 20163507
can anyone help?
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 20163923
I see that you opened a new question for this ...

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

Start from the code you posted here :

        http://www.experts-exchange.com/Programming/Languages/C/Q_22906376.html#20161811

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-exchange.com/Programming/Languages/C/Q_22906376.html#20118235


Please don't do anything else than that.
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 20163926
Which part do you have a problem with ? You seem to insist on not following instructions, and just doing your own thing ... How can you expect to be helped that way ?
0
 

Author Comment

by:Cervan
ID: 20163967
I just due with too many problem!! Well,just move the another problem and close this.Thank you!
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 20163999
You're the only one that can close this one ;)
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.
Suggested Courses

807 members asked questions and received personalized solutions in the past 7 days.

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

Join & Ask a Question