# Thread: Can't get "return()" to work.Problem in "if" part

1. ## Can't get "return()" to work.Problem in "if" part

hi,
My program's all function are running fine except the "position()" function.In this function i have some problem in if part.
the result always print 0 as answer.
Here is the code :
Code:
```#include<stdio.h>

main()
{
int q;
int x1,y1, x2, y2,x3,y3;
float distance(int a1,int b1,int a2,int b2);
printf("Enter three points cooredinates(x1,y1),(x2,y2),(x3,y3): ");
scanf("(%d,%d),(%d,%d),(%d,%d)",&x1,&y1,&x2,&y2,&x3,&y3);
float area(int x1,int y1,int x2,int y2,int x3,int y3);
/*distance();*/
q = position( x1, y1, x2, y2, x3, y3);
printf("position : %d",q);
return 0;
}

position(int x1,int y1,int x2,int y2,int x3,int y3)
{
int x,y,p,q,r,s;
printf("Enter the point's cooredinates (x,y): ");
scanf(" (%d,%d)",&x,&y);
p = area(x1, y1, x2, y2, x3, y3);
q = area(x, y, x2, y2, x3, y3);
r = area(x1, y1, x, y, x3, y3);
s = area(x1, y1, x2, y2, x, y);
if ( p == q+r+s)/* <=======problem*/
{
return(1);
}
else
{
return(0);
}
}

float area(int j1,int k1,int j2,int k2,int j3,int k3)
{
float a,l,b,h;

l = distance(j1,k1,j2,k2);
b = distance(j2,k2,j3,k3);
h = distance(j1,k1,j3,k3);
a = l*b*h*1/2;
return(a);
}

float distance(int a1,int b1,int a2,int b2)
{
float s;
s = sqrt(pow(a2-a1,2) + pow(b2-b1,2));
return(s);
}```

2. That code won't even compile, so you shouldn't even be able to get output from it.

Try deleting the object and executable files, and rebuild from scratch. If using an IDE, do a Build/Clean before a complete rebuild.

Then correct the compilation errors before trying to run it.

Your basic problem is that all your functions need to be declared before code that uses them. If you don't do that, the compiler makes assumptions about what the functions are (what arguments they accept, return type). The assumptions are different from the actual functions.

If you're going to use math functions (sqrt(), pow()) it is necessary to #include <math.h> first.

Also, x*x is usually considered a better approach to computing x squared, than pow(x,2).

3. thank you ,i did what you said and it worked .But couldn't pin point what was the main problem "the math.h library" or "the declaration" or "the previous object file"
but hey its working ,thanx a lot for the quick reply and if you can make the program shorter or more efficient let me know.
My new code :

Code:
```#include<stdio.h>
#include<math.h>

int position(int x1,int y1,int x2,int y2,int x3,int y3);
float distance(int a1,int b1,int a2,int b2);
float area(int x1,int y1,int x2,int y2,int x3,int y3);

main()
{
int q;
int x1,y1, x2, y2,x3,y3;
float distance(int a1,int b1,int a2,int b2);
printf("Enter three points cooredinates(x1,y1),(x2,y2),(x3,y3): ");
scanf("(%d,%d),(%d,%d),(%d,%d)",&x1,&y1,&x2,&y2,&x3,&y3);
float area(int x1,int y1,int x2,int y2,int x3,int y3);
/*distance();*/
q = position( x1, y1, x2, y2, x3, y3);
printf("position : %d",q);
return 0;
}

position(int x1,int y1,int x2,int y2,int x3,int y3)
{
int x,y,p,q,r,s;
printf("Enter the point's cooredinates (x,y): ");
scanf(" (%d,%d)",&x,&y);
p = area(x1, y1, x2, y2, x3, y3);
q = area(x, y, x2, y2, x3, y3);
r = area(x1, y1, x, y, x3, y3);
s = area(x1, y1, x2, y2, x, y);
if ( p == q+r+s)
{
return(1);
}
else
{
return(0);
}
}

float area(int j1,int k1,int j2,int k2,int j3,int k3)
{
float a,l,b,h;

l = distance(j1,k1,j2,k2);
b = distance(j2,k2,j3,k3);
h = distance(j1,k1,j3,k3);
a = l*b*h*1/2;
return(a);
}

float distance(int a1,int b1,int a2,int b2)
{
float s;
s = sqrt((a2-a1)*(a2-a1) + (b2-b1)*(b2-b1));
return(s);
}```

4. You forgot the return type in this prototype, didn't you?
Code:
`position(int x1,int y1,int x2,int y2,int x3,int y3)`
You return an integer, thus the return type should be...you know the answer to this.

Note that return in a function does not need parenthesis around the return data. For example
Code:
`return (0);`
is equivalent to this
Code:
` return 0;`
, but of course this is not a mistake.

I would also did not use variable q in your main(). I would place the function right inside the printf().

5. Thank you for your reply. .however i am not sure my program is working properly.
i gave input of first three points as : (1,4),(5,7),(9,4)
And it only gives "1" as return when point is (5,7) or (1,4) or (9,4)
and for others its 0 as return value.cant figure out what is the problem (bytheway i corrected that declaration part and q part)

6. you should check return value of scanf - to see how many of passed variables were really initialized.

Why you think that lbh/2 will give you the area of triangle?

I see formula for area using 3 sides
S = sqrt(p(p - a)(p - b)(p - c))

Area of a triangle given three sides - Heron's Formula - Math Open Reference

7. Originally Posted by vart
you should check return value of scanf - to see how many of passed variables were really initialized.

Why you think that lbh/2 will give you the area of triangle?

I see formula for area using 3 sides
S = sqrt(p(p - a)(p - b)(p - c))

Area of a triangle given three sides - Heron's Formula - Math Open Reference

That's correct; ie. the OP's formula is not correct. 1/2(b/h) would be ok for a right triangle but not general triangles.

There's a few methods for calculating the area of a triangle including the one you've quoted, but I'd probably use one of the methods that don't involve a square root, therefore avoiding the math libraray as well as complicated calcuations. I think that if I were the op I'd got with the so-called "shoelace formula" mainly because it can be easily extended to compute the area of any polygon -- not just triangles. Using this method/formula and the coordinates (ax, ay), (bx, by) and (cx, cy) the area of the triangle is:

t1 = ax * by + bx * cy + ax * by;
t2 = bx * ay + cx * by;
area = abs(t1 - t2);

Additionally, the OP should be checking that the 3 coordinates are actually a triangle before calculating the area for it

8. Hey ,thanx for the quick reply guys.the problem was indeed with the formula.I changed it with heron's and now it works fine.thanx a lot ,have a good day guys
here is the new code :

Code:
```#include<stdio.h>
#include<math.h>

int position(int x1,int y1,int x2,int y2,int x3,int y3);
float distance(int a1,int b1,int a2,int b2);
float area(int x1,int y1,int x2,int y2,int x3,int y3);

main()
{
int x1,y1, x2, y2,x3,y3;
float distance(int a1,int b1,int a2,int b2);
printf("Enter three points cooredinates(x1,y1),(x2,y2),(x3,y3): ");
scanf("(%d,%d),(%d,%d),(%d,%d)",&x1,&y1,&x2,&y2,&x3,&y3);
float area(int x1,int y1,int x2,int y2,int x3,int y3);
printf("position : %d",position( x1, y1, x2, y2, x3, y3));
return 0;
}

int position(int x1,int y1,int x2,int y2,int x3,int y3)
{
int x,y,p,q,r,s,t;
printf("Enter the point's cooredinates (x,y): ");
scanf(" (%d,%d)",&x,&y);
p = area(x1, y1, x2, y2, x3, y3);
q = area(x, y, x2, y2, x3, y3);
r = area(x1, y1, x, y, x3, y3);
s = area(x1, y1, x2, y2, x, y);
t = r+q+s;
if ( p == t)
{
return 1;
}
else
{
return 0;
}
}

float area(int j1,int k1,int j2,int k2,int j3,int k3)
{
float a,l,b,h,p;

l = distance(j1,k1,j2,k2);
b = distance(j2,k2,j3,k3);
h = distance(j1,k1,j3,k3);
p = (l+b+h)/2;
a = sqrt(p*(p - l)*(p - b)*(p - h));
return(a);
}

float distance(int a1,int b1,int a2,int b2)
{
float s;
s = sqrt((a2-a1)*(a2-a1)*(b2-b1)*(b2-b1));
return(s);
}```

9. And thanx sirprattlepod for clearing things out and for another formula

10. you have function declarations before main - so you do not need to repeat them inside main on lines 11 and 14

also main is missing return type FAQ > main() / void main() / int main() / int main(void) / int main(int argc, char *argv[]) - Cprogramming.com

11. Oops left off the division by 2 in my code. It should be:

area = abs(t1 - t2) / 2;

12. Originally Posted by SirPrattlepod
t1 = ax * by + bx * cy + ax * by;
t2 = bx * ay + cx * by;
area = abs(t1 - t2) / 2;
Going by the link you provided, you also missed off one of the terms, and got one of them wrong. I.e.
Code:
```t1 = ax * by + bx * cy + cx * ay;
t2 = bx * ay + cx * by + ax * cy;
area = abs(t1 - t2) / 2;```

13. Originally Posted by iMalc
Going by the link you provided, you also missed off one of the terms, and got one of them wrong. I.e.
Code:
```t1 = ax * by + bx * cy + cx * ay;
t2 = bx * ay + cx * by + ax * cy;
area = abs(t1 - t2) / 2;```
That's correct. You win the prize for solving my intentional error.

I place these errors in sample code to make sure the person reads the link and understands the material themselves. Yep.

14. The Second Amendment!