Thread: Revised Slope formula

  1. #1
    Registered User
    Join Date
    Oct 2001
    Posts
    44

    Revised Slope formula

    I think this should work, but i can get it to work. Anyone got any ideas on why it is not working..??
    ------------------------------------------------------------------------------

    #include <stdio.h>
    #include <conio.h>
    main()
    {
    int x1, y1, x2, y2, m ;

    printf ( "To find the slope of your line enter the following: \n" ) ;

    printf ( "The First set of points. eg. (x,y) \n" ) ;
    scanf ( "%d" , &x1 ) ;
    scanf ( "%d" , &y1 ) ;

    printf ( "The Second set of points. eg. (x,y) \n" ) ;
    scanf ( "%d" , &x2 ) ;
    scanf ( "%d" , &y2 ) ;

    m = ( y2 - y1 ) / (x2 - x1) ;

    printf ( "The slope is %d \n" ,m ) ;

    getch () ;
    }
    -----------------------------------------------------------------------------
    Dangerous Dave

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    28
    Try replacing your 2 scanf's for each points with one
    scanf("%d %d", &x1, &y1);
    Have a space between your numbers and you should be fine

  3. #3
    Ethereal Raccoon Procyon's Avatar
    Join Date
    Aug 2001
    Posts
    189
    You should also use a float, rather than an int, for the slope variable, and typecast (y2-y1) to a float.

  4. #4
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Also you may want to do a check for a divide by zero. Remember if your line is vertical then you have an undefined slope. I don't remember how C handles dividing by zero.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Slope to degrees
    By zMan in forum C Programming
    Replies: 3
    Last Post: 05-06-2003, 07:46 AM
  2. Need help with this formula -b/2a..Please read.
    By foofoo in forum C Programming
    Replies: 4
    Last Post: 06-04-2002, 11:59 PM
  3. Formula string conversion
    By Gr3g in forum C++ Programming
    Replies: 2
    Last Post: 04-12-2002, 08:28 PM
  4. Slope Formula
    By Dangerous Dave in forum C Programming
    Replies: 9
    Last Post: 10-08-2001, 12:25 PM
  5. Finished Slope Formula
    By Dangerous Dave in forum C Programming
    Replies: 2
    Last Post: 10-08-2001, 12:06 PM