help: function, find area of triangle

This is a discussion on help: function, find area of triangle within the C Programming forums, part of the General Programming Boards category; simply finding the area of a triangle this is how i want to do it and the compiler says its ...

  1. #1
    Niz
    Niz is offline
    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 09:14 AM.

  2. #2
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    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?
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  3. #3
    Niz
    Niz is offline
    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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    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
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,497
    > 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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  6. #6
    Niz
    Niz is offline
    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
    Niz
    Niz is offline
    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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    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
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  10. #10
    Niz
    Niz is offline
    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
    Niz
    Niz is offline
    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 11:30 AM.

  14. #14
    Niz
    Niz is offline
    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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    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 12:11 PM.
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

Page 1 of 2 12 LastLast
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, 01:31 PM
  2. Troubleshooting Input Function
    By SiliconHobo in forum C Programming
    Replies: 14
    Last Post: 12-05-2007, 06:18 AM
  3. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 06: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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21