Like Tree3Likes
  • 1 Post By std10093
  • 1 Post By vart
  • 1 Post By iMalc

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

This is a discussion on Can't get "return()" to work.Problem in "if" part within the C Programming forums, part of the General Programming Boards category; hi, My program's all function are running fine except the "position()" function.In this function i have some problem in if ...

  1. #1
    F#ck me Freddy!!
    Join Date
    Sep 2013
    Location
    jaipur
    Posts
    79

    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. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,436
    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).
    Right 98% of the time, and don't care about the other 3%.

  3. #3
    F#ck me Freddy!!
    Join Date
    Sep 2013
    Location
    jaipur
    Posts
    79
    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. #4
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    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().
    coder1 likes this.
    Code - functions and small libraries I use


    Its 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " Harold Abelson

  5. #5
    F#ck me Freddy!!
    Join Date
    Sep 2013
    Location
    jaipur
    Posts
    79
    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. #6
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    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
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  7. #7
    11DE784A SirPrattlepod's Avatar
    Join Date
    Aug 2013
    Posts
    485
    Quote Originally Posted by vart View Post
    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. #8
    F#ck me Freddy!!
    Join Date
    Sep 2013
    Location
    jaipur
    Posts
    79
    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. #9
    F#ck me Freddy!!
    Join Date
    Sep 2013
    Location
    jaipur
    Posts
    79
    And thanx sirprattlepod for clearing things out and for another formula

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

  11. #11
    11DE784A SirPrattlepod's Avatar
    Join Date
    Aug 2013
    Posts
    485
    Oops left off the division by 2 in my code. It should be:

    area = abs(t1 - t2) / 2;

  12. #12
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,308
    Quote Originally Posted by SirPrattlepod View Post
    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;
    SirPrattlepod likes this.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  13. #13
    11DE784A SirPrattlepod's Avatar
    Join Date
    Aug 2013
    Posts
    485
    Quote Originally Posted by iMalc View Post
    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.
    Last edited by SirPrattlepod; 09-10-2013 at 01:07 AM.

  14. #14
    Stoned Witch Barney McGrew's Avatar
    Join Date
    Oct 2012
    Location
    astaylea
    Posts
    420
    The Second Amendment!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 07-17-2012, 09:02 AM
  2. nbin=fopen("input.txt","a"); doesn't work?
    By Adam Rinkleff in forum C Programming
    Replies: 2
    Last Post: 06-23-2011, 02:57 PM
  3. passing strings from functions as the "return" part
    By fraktal in forum C Programming
    Replies: 8
    Last Post: 12-13-2005, 12:38 AM
  4. "itoa"-"_itoa" , "inp"-"_inp", Why some functions have "
    By L.O.K. in forum Windows Programming
    Replies: 5
    Last Post: 12-08-2002, 07:25 AM
  5. "CWnd"-"HWnd","CBitmap"-"HBitmap"...., What is mean by "
    By L.O.K. in forum Windows Programming
    Replies: 2
    Last Post: 12-04-2002, 06:59 AM

Tags for this Thread


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