Thread: help: function, find area of triangle

  1. #1
    Registered User
    Join Date
    Jan 2007
    Posts
    14

    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);
    }
    Last edited by Niz; 01-17-2007 at 10:14 AM.

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    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?
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    Registered User
    Join Date
    Jan 2007
    Posts
    14
    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. #4
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    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
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > 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.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Registered User
    Join Date
    Jan 2007
    Posts
    14
    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. #7
    The larch
    Join Date
    May 2006
    Posts
    3,573
    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. #8
    Registered User
    Join Date
    Jan 2007
    Posts
    14
    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. #9
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote 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
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  10. #10
    Registered User
    Join Date
    Jan 2007
    Posts
    14
    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. #11
    Registered User
    Join Date
    Jan 2007
    Posts
    9
    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. #12
    Registered User
    Join Date
    Jan 2007
    Posts
    14
    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. #13
    Registered User
    Join Date
    Jan 2007
    Posts
    9
    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
    Last edited by richfatsanta; 01-17-2007 at 12:30 PM.

  14. #14
    Registered User
    Join Date
    Jan 2007
    Posts
    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. #15
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    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
    Last edited by vart; 01-17-2007 at 01:11 PM.
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Recursive Triangle Function
    By w2look in forum C Programming
    Replies: 14
    Last Post: 11-13-2010, 02:31 PM
  2. Troubleshooting Input Function
    By SiliconHobo in forum C Programming
    Replies: 14
    Last Post: 12-05-2007, 07:18 AM
  3. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 07:18 AM
  4. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM
  5. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM