Thread: Problem with taking inputs/calculating slope

  1. #1
    Registered User toadkiwi's Avatar
    Join Date
    Feb 2008
    Posts
    31

    Problem with taking inputs/calculating slope

    Hey all. Got a problem. I'm writing a program that takes the coordinates for 3 points and I'm supposed to identify whether or not the points in relation to eachother are collinear or not. For example, I have points

    (x1, y1) (x2, y2)(x3, y3)

    If the points were collinear, the slope between pt1 and pt2 would be the same as the slope between pt2 and pt3.

    My code so far gives me this error
    Code:
    collinear.c: In function `main':
    collinear.c:17: error: invalid lvalue in assignment
    And here's my code so far
    Code:
    #include <stdio.h>
    #include <math.h>
    
    main()
    {
    
    double x1;
    double y1;
    double x2;
    double y2;
    double x3;
    double y3;
    
    printf("Enter coordinates for 3 points--x1 y1 x2 y2 x3 y3\n");
            scanf("%lf %lf %lf %lf %lf %lf", &x1, &y1, &x2, &y2, &x3, &y3);
    
    if(((y2 - y1)/(x2 - x1)) = ((y3 - y2)/(x3 - x2)))
    {
            printf("The points entered are collinear\n");
    }
    
    else
    {
            printf("The points entered are not collinear\n");
    }
    
    }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Comparison is done using ==, not =.

    Also, this will not work with vertical lines (if x1==x2 or x2==x3).

  3. #3
    Registered User toadkiwi's Avatar
    Join Date
    Feb 2008
    Posts
    31
    Ahh, much thanks!

  4. #4
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    also note that main should be declared as
    int main(void)

    And your indentation style suffers...
    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
    Registered User toadkiwi's Avatar
    Join Date
    Feb 2008
    Posts
    31
    My indentation?

    Any suggestions?

  6. #6
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    Code:
    #include <stdio.h>
    #include <math.h>
    
    int main(void)
    {
    
    	double x1;
    	double y1;
    	double x2;
    	double y2;
    	double x3;
    	double y3;
    
    	printf("Enter coordinates for 3 points--x1 y1 x2 y2 x3 y3\n");
    	scanf("&#37;lf %lf %lf %lf %lf %lf", &x1, &y1, &x2, &y2, &x3, &y3);
    
    	if (((y2 - y1)/(x2 - x1) ) == ( (y3 - y2)/(x3 - x2)))
    	{
    		printf("The points entered are collinear\n");
    	}
    	else
    	{
    		printf("The points entered are not collinear\n");
    	}
    }
    Mainframe assembler programmer by trade. C coder when I can.

  7. #7
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    just copied into VS and pressed Alt+F8
    Code:
    #include <stdio.h>
    #include <math.h>
    
    main()
    {
    	
    	double x1;
    	double y1;
    	double x2;
    	double y2;
    	double x3;
    	double y3;
    	
    	printf("Enter coordinates for 3 points--x1 y1 x2 y2 x3 y3\n");
    	scanf("&#37;lf %lf %lf %lf %lf %lf", &x1, &y1, &x2, &y2, &x3, &y3);
    	
    	if(((y2 - y1)/(x2 - x1)) = ((y3 - y2)/(x3 - x2)))
    	{
            printf("The points entered are collinear\n");
    	}
    	
    	else
    	{
            printf("The points entered are not collinear\n");
    	}
    	
    }
    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

  8. #8
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Ups... you have mix of spaces and tabs
    Edited... was already beated... so i remove this intermediate state
    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

  9. #9
    Registered User toadkiwi's Avatar
    Join Date
    Feb 2008
    Posts
    31
    Nice and organized. Thanks!

  10. #10
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    Code:
    {
        //code
    }
    not
    Code:
    {
    //code
    }
    edit: beat twice (maybe thrice)! ARG!!

  11. #11
    Registered User Bajanine's Avatar
    Join Date
    Dec 2001
    Location
    The most peaks over 10,000 feet!
    Posts
    396
    You might want to check for division by 0!
    Favorite Quote:

    >For that reason someone invented C++.
    BLASPHEMY! Begone from my C board, you foul lover of objects, before the gods of C cast you into the void as punishment for your weakness! There is no penance for saying such things in my presence. You are henceforth excommunicated. Never return to this house, filthy heretic!



  12. #12
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by Bajanine View Post
    You might want to check for division by 0!
    Indeed, but don't write special case code for that. Instead make use of the following algebraic identity:
    Code:
    a / b == c / d   <==>   a * d == c * b
    This converts the divisions by zero into multiplications by zero, which as you know is not a problem.

    However in practical terms you still have another bug either way. The problem is to do with floating point inaccuracy and that fact that you can't compare values using ==.
    You should instead be usinfg fabs to test the values within an epsilon value of each other.
    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
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by toadkiwi View Post
    My indentation?

    Any suggestions?
    http://cpwiki.sf.net/Indentation
    Maybe this can help?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with for loop calling external function
    By lordbubonicus in forum C Programming
    Replies: 2
    Last Post: 10-13-2007, 10:54 AM
  2. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  3. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  4. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM
  5. Laptop Problem
    By Boomba in forum Tech Board
    Replies: 1
    Last Post: 03-07-2006, 06:24 PM