# Thread: help: function, find area of triangle

1. ## help: function, find area of triangle

simply finding the area of a triangle this is how i want to do it and the compiler says its all good to go but it fails after i enter the three values, and says the problems in the lines shown in an error message window, any help appreciated, thanks.

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

void my_func(float *R, float *P, float *A, float *B, float *C)
{
*P = ((*A+*B+*C)/2);        /*PROBLEM IN THIS LINE*/
*R = sqrt((*P)*(P-A)*(P-B)*(P-C));
}

main()
{
float R, P, A, B, C;

printf("Enter 3 non integer values:");
scanf("%f, %f, %f\n", &A, &B, &C);
my_func(&P, &R, &A, &B, &C);      /*PROBLEM IN THIS LINE*/
printf("Area = %f\n",R);

return(0);
}```

2. for me, problem is in this line
sqrt((*P)*(P-A)*(P-B)*(P-C));

P-A - both are pointers

Why do you need to pass A,B,C by pointers?

3. ok i changed it but now the problem is within the compiling

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

void my_func(float *R, float P, float A, float B, float C)
{
P = ((A+B+C)/2);
*R = sqrt((P)*(P-A)*(P-B)*(P-C));
}

main()
{
float R,P, A, B, C;

printf("Enter 3 non integer values:");
scanf("%f, %f, %f\n", &A, &B, &C);
my_func(&P, R, A, B, C);
printf("Area = %f\n",&R); /*PROBLEM HERE*/

return(0);
}```

4. you have misplaced P and R
if you don't need P in main - make it local
if you need just R - make it return value
printf awaits var not pointer to var

5. > printf("Area = %f\n",&R);
You don't use & when printing things.

Also, which compiler are you using?
If it's a gcc based compiler (like mingw or dev-c++), then add these options to the compiler command line (or IDE)
gcc -W -Wall -ansi -pedantic -O2

It will tell you many things about your code, including when you make a mess of calling printf and scanf style functions.

6. im trying!, how can i return the statement? i only know how to return a statement if it involves 1 calculation.

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

void my_func(float *R, float A, float B, float C)
{
float P;
P = ((A+B+C)/2);
*R = sqrt((P)*(P-A)*(P-B)*(P-C));
}

main()
{
float R, A, B, C;

printf("Enter 3 non integer values:");
scanf("%f, %f, %f\n", &A, &B, &C);
my_func(&R, A, B, C);/*PROBLEM HERE*/
printf("Area = %f\n",R);

return(0);
}```

7. Code:
```float my_func(float A, float B, float C)
{
float result;
float P;
P = ((A+B+C)/2);
result = sqrt((P)*(P-A)*(P-B)*(P-C));
return result;
}```
Edit: didn't notice P in there

8. i know that bit i just dont know what to put in the main body?

Code:
` printf("Area = %f\n",/*HERE*/);`
normally it would be just my_func() but that wouldnt work in this situation.

9. Originally Posted by Niz
i know that bit i just dont know what to put in the main body?

Code:
` printf("Area = %f\n",/*HERE*/);`
normally it would be just my_func() but that wouldnt work in this situation.
it would

10. but it says 3 arguments are required and if i put in

my_func(float A, float B, float C)

it says float found where operator expected

so here it is so far, pls bear with me im trying

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

float my_func(float A, float B, float C)
{
float P, R;
P = ((A+B+C)/2);
R= sqrt((P)*(P-A)*(P-B)*(P-C));
return R;
}

main()
{
float A, B, C;

printf("Enter 3 non integer values:");
scanf("%f, %f, %f\n", &A, &B, &C);

printf("Area = %f\n",my_func(A, B, C)); /*PROBLEM HERE*/

return(0);
}```

11. I don't know if you are trying the program just to practice your functions and such but either way...

Code:
```	float a,b,c,s,area;

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

s=(a+b+c)/2;

area=sqrt(s*(s-a)*(s-b)*(s-c));

printf("area is %f",area);```

12. yeh im just working on using functions im just practising but even your method returns no value i get 0 as the area

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

main()
{
float a,b,c,s,area;
printf("Enter 3 non-integer values:");
scanf("%f %f %f",&a,&b,&c);

s=(a+b+c)/2;

area=sqrt((s)*(s-a)*(s-b)*(s-c));

printf("area is %f",area);

return(0);
}```

13. Weird that exact code works perfect on my machine.
For input of a,b,c you are using values that actually make a real triangle and separated by spaces? Though even enter each time works on mine. Commas give an error but not area of zero.

Code:
`same as above`

14. thanks yeh it does work and so did my old code theres was something wrong with the software/compiler it actually crashed on me just now. Now I restarted it and now the old and your new code (the same) work, thanks a bunch, ive been working on this for 3hrs now, thanks

if anyone can solve my problem initially using a function to do this please help but i dont think im going to get anymore help

15. should be
int main();

and
return 0;
- there is no need in breakets, just like return R;

also there is no need in \n in scanf
and you may want to check the return value
Code:
```if(scanf("%f, %f, %f", &A, &B, &C) != 3)
//not all 3 value are entered correctly
else
//process the input```